r/SQL • • 26d ago

PostgreSQL Trying to make sense of query plans. Looking for opinions on how I draw them

I've been trying to figure out the best way to make sense of a query, how it affects the database and whether there is room to improve it. I'm a visual person. If something has a diagram I understand it faster, and I wanted the same thing when I work with a database at work or on side projects. I want to see how the data is being retrieved or updated without working it out from the EXPLAIN text.

I've came up with following visual rule:

  • Each scan, join and sort is a bar. The band between two bars is as wide as the number of rows flowing through. Width is 1.2 + 0.55 x (log10 rows)², capped between 1.5px and 34px. Linear made anything under 100,000 rows a hairline, and plain log made 1,000 and 1,000,000 look too close.
  • When the planner's estimate is off from the actual count by more than 2×, the band gets a dashed line showing the estimate. At 10x it gets a label. Postgres has no opinion on 10×, that number is mine.
  • Each scan gets a wheel showing how much of the table's pages it touched and in what order, based on pg_stats.correlation with a 0.8 cutoff for "in order".
  • To get the analyzed plan it re-runs the query inside BEGIN and ROLLBACK with a 5 minute statement_timeout. Writes stop at the estimate unless you opt in.

There's in depth rulebook here.

I would really appreciate some feedback here, I kind of struggling what's the best way to visualize this. Any idea/critique here is most appreciated. Thanks

34 Upvotes

11 comments sorted by

4

u/ArielCoding 26d ago

Looks like the bands stop getting wider at some point, and they maybe look the same size, try sizing the bands relative to the biggest step in each query, so the largest one is always full width and everything else scales from there.

1

u/duoroo 25d ago

Fair point, currently its maxed out at around 50 million rows. honestly when i initially built it, it was relative, but the problem I faced was with comparing two different plans, since its not 1 to 1 now. Maybe i'll give an option in settings to toggle between those.

And thanks for the suggestion :)

1

u/MrRoy_SQL 26d ago

this looks really nice, the log-squared width is a smart fix for the hairline problem. few things i'd think about:

  1. loops. for nodes inside a nested loop, postgres shows rows per loop, so make sure the band width uses rows x loops. otherwise the inner side of a nested loop looks tiny when it's actually the expensive part
  2. make the estimate check symmetric (max of est/actual and actual/est). underestimates are usually the dangerous ones since that's what makes the planner pick a nested loop, so it's worth flagging those even more clearly
  3. width shows rows, not time. the slowest node often isn't the fattest band, so some way to highlight the node with the most self time (its time minus its children's) would help a lot
  4. on the BEGIN/ROLLBACK for writes: rollback doesn't undo everything. sequences still advance and anything a trigger does outside the db still happens, plus you hold locks for up to 5 min. opt-in is the right call, i'd just say that clearly in the UI

3

u/duoroo 26d ago
  1. Loops are already in there, the band is actual rows x loops. the inner index scan in that screenshot ran 207k times and the width is drawn from the total, loop count is on the label too.
  2. The estimate check is symmetric right now (10x off either way, finding says more or fewer) but i don’t treat underestimates any harsher. the nested loop angle hadn’t occurred to me, going to rank those higher.
  3. yeah width is rows only. should’ve mentioned in the post, there’s a time strip at the top split by self time (minus children), the hottest nodes go red and the inspector opens on the biggest one by default.
  4. the confirm dialog lists sequences, trigger side effects and locks, but doesnt say how long locks can be held. adding that

On the underestimates, would you want a different colour for them or is sorting them to the top enough?

Thanks again for the feedback sir. much appreciated.

1

u/MrRoy_SQL 26d ago

ah nice, sounds like you'd already thought of most of it, the self time strip is exactly what i was after. for underestimates i'd do both: sort them to the top, and give them a different colour or icon on the band itself, since most people look at the diagram first and only read the findings list when something stands out. bonus if it says "underestimated → probably why it picked a nested loop" right there. cool project, good luck with it buddy.

1

u/duoroo 26d ago

That makes sense, i’ll do both then and thanks for the help, really appreciate it.

1

u/MrRoy_SQL 26d ago

No problem buddy…feel free to let me know if that really helps

1

u/Significant_Tune9219 26d ago

The dashed band when the estimate is off is the right signal, but a bad estimate usually starts at one node and then every parent looks wrong too. I'd mark only the deepest node where actual and estimate cross your 2x line, and leave the parents solid. Otherwise the diagram shouts at the join when the scan's selectivity was the real problem. One caution on the re-run: EXPLAIN ANALYZE inside BEGIN and ROLLBACK still executes the statement, so a function with a side effect is not undone by the rollback. The plan can also differ from production if that session's work_mem isn't what the app uses, so I'd print those settings next to the picture.

1

u/duoroo 26d ago

Good catch on the dashed lines. the findings list already only flags the node where it starts but i never did the same in the drawing, so right now it lights up the whole chain up to the join. gonna try leaving the parents solid

didn’t think about functions in a select. those count as reads so they get re-run without any prompt rn, need to fix that. thanks for pointing it out

work_mem shows in the side panel but nothing else from the settings does. will put whatever’s changed from default in the header. does this sound good to you?