News for hackers. Stuff that matters.
Re:What's new in the Postgres 16 query planner (a.k.a. optim... by wiradikusuma on Thursday February 08, 2024 @11:28PM (#39311396)
I use this tool to visualize my queries: https://explain.dalibo.com/ (there's also https://www.pgexplain.dev/, last time the output was less nice, but now both look the same)
Re:What's new in the Postgres 16 query planner (a.k.a. optim... by klysm on Thursday February 08, 2024 @11:51PM (#39311529)
The tool is great and I use it, but I don't really have a deep enough understanding to know how to fix issues in my approach from what looks bad in the plan.
Re:What's new in the Postgres 16 query planner (a.k.a. optim... by davidrowley on Friday February 09, 2024 @12:05AM (#39311611)
It's pretty hard to tell if a plan is good or bad from EXPLAIN without using the ANALYZE option. With EXPLAIN ANALYZE you can see where the time is being spent, so can you get an idea of which part of the plan you should focus on.

To know if it's a bad plan, it does take quite a bit of knowledge as you need to know what alternative plans could have been used instead. It takes quite a bit of time to learn that stuff. You need to know what PostgreSQL is capable of. Some computer science knowledge helps here as you'll know, for example, when a hash join is a good way to join a table vs a nested loop.

As for fixing plan you've identified as bad, that also takes quite a bit of experience. If you understand the EXPLAIN ANALYZE output well, that's a good start. Looking for places where the estimated rows differ from the actual can be key. Having an understanding of how Postgres performed the row estimations helps. That's not something that comes easily without looking at the source code, unfortunately. Understanding tools that you have to change the plan is useful. Perhaps that's CREATE STATISTICS, or adjusting the stats targets on existing single column stats. Or maybe creating a new index. Having a test environment that allows you to experiment is very useful too.

Re:What's new in the Postgres 16 query planner (a.k.a. optim... by cnity on Friday February 09, 2024 @04:18AM (#39313016)
To be honest, you can get quite a lot of mileage out of EXPLAIN by focusing on which indices the plan selected (or did not select). For this reason it's really worth understanding your indices and what value they add (or remove!).
Re:What's new in the Postgres 16 query planner (a.k.a. optim... by dewey on Friday February 09, 2024 @01:27AM (#39312008)
There's also https://www.pgmustard.com, which gives you a bit more hints and information on the possible optimizations.
Re:What's new in the Postgres 16 query planner (a.k.a. optim... by uudecoded on Friday February 09, 2024 @12:52AM (#39311821)
I read your profile and see that you are a CTO of a fintech. Given that, by what method do you navigate that tool's [explain.dalibo.com] assertion of "It is recommended not to send any critical or sensitive information"?

Is there an explain plan sanitizer that is helpful for this situation?

Re:What's new in the Postgres 16 query planner (a.k.a. optim... by Defman on Friday February 09, 2024 @12:58AM (#39311854)
You can download the whole visualizer as a simple html file and use it this way. No need to obfuscate or sanitize anything at all.

https://github.com/dalibo/pev2

Re:What's new in the Postgres 16 query planner (a.k.a. optim... by uudecoded on Friday February 09, 2024 @01:11AM (#39311925)
Thank you for sharing - I misunderstood - this is a javascript visualizer, and has no additional analytic capability beyond visualization, or did I miss something?
Re:What's new in the Postgres 16 query planner (a.k.a. optim... by Defman on Friday February 09, 2024 @01:32AM (#39312052)
Yeah, you’re right. This is the visualizer that’s linked in the parent comment. I just wanted to point out that you can use it locally without sending your explain plan to anyone. Sorry for the confusion, I’ll edit my original comment.
Re:What's new in the Postgres 16 query planner (a.k.a. optim... by derhuerst on Friday February 09, 2024 @06:38AM (#39313809)
I do the same, but because it became tedious to insert the query & query plan manually, I wrote pev2-cli [1].

[1] https://github.com/derhuerst/pev2-cli

Re:What's new in the Postgres 16 query planner (a.k.a. optim... by williamdclt on Friday February 09, 2024 @05:00AM (#39313279)
Whatever the domain, a query isn't necessarily critical or sensitive. It only is if it contains personal information (eg querying by a bank account number or a name), or if the query itself is part of your competitive advantage (unlikely)