PostgreSQL Query Planner

Cluster focuses on PostgreSQL's query planner reliability, frustrations with unexpected plan changes, lack of hints, and tools like EXPLAIN ANALYZE for optimization.

📉 Falling 0.4x Databases
3,777
Comments
20
Years Active
5
Top Authors
#2484
Topic ID

Activity Over Time

2007
4
2008
13
2009
34
2010
50
2011
115
2012
76
2013
115
2014
100
2015
142
2016
165
2017
228
2018
202
2019
308
2020
278
2021
351
2022
421
2023
367
2024
489
2025
296
2026
23

Keywords

EXPLAIN e.g CBO JIT OLTP SQL alibabacloud.com PG LLVM MSSQL query planner plan queries hints sql optimizer join postgres optimal

Sample Comments

rpbiwer2 May 16, 2024 View on HN

This seems like the type of thing that a sophisticated query planner in 2024 would be able to figure out on its own? Maybe as a non database expert I'm expecting too much?

meritt Jun 19, 2015 View on HN

No. Learn to trust the query planner. It's a good thing. When you want explicitness, take a look at the explain plan.

rustyconover Apr 16, 2020 View on HN

PostgreSQL does offer insights into the plan. Just put EXPLAIN before your query.

kamranjon Jan 16, 2021 View on HN

Use EXPLAIN ANALYZE and save yourself some time. It's not that difficult to figure out what the planner is doing and tweak it. I think SQL is the best we got but I'd love to hear what you think is a better alternative.

n4r9 Oct 4, 2019 View on HN

I've had similar experiences. We're you able to generate the actual query plan in each case and see if it had been done differently?

erulabs Sep 22, 2023 View on HN

The alternative is to write good queries! Alternatively, a better query planner that has a more liberal approach to "I know what you want, not what you're asking for". A great article on this re: Planetscale's approach is at https://vitess.io/blog/2021-11-02-why-write-new-planner/

mike_hock Aug 30, 2023 View on HN

Yes, and Postgres remains staunchly opposed to planner hints because the planner knows better! It always computes the optimal query plan!The "optimal query plan" changes at the drop of a hat, as you can see in this case. Absolutely trivial syntax changes result in a completely different query, sometimes turning a sequential scan into an index-only scan or vice versa. So, 100x difference in query time, it doesn't just do that for small tables.

wolf550e Aug 5, 2012 View on HN

SQL has EXPLAIN PLAN. SQL without EXPLAIN PLAN is useless, for exactly this reason: you have no idea how it will perform.

iaabtpbtpnn Mar 9, 2022 View on HN

This is very useful! Postgres is opinionated about its lack of hints. And while I don't love hints as a development method (better to figure out why the planner made a suboptimal choice and correct the statistics accordingly), sometimes it's necessary to be sure that a query running in production isn't suddenly going to change from a fast plan to a slow one unpredictably.

barrkel Oct 18, 2016 View on HN

If you think "needs upstream fix", then no database is suitable for use, because they all choose poor plans in many edge cases. This is why most databases have extensions that let you hint or force index use (not Postgres, however; a mistake with mitigation), or support parenthesising your joins to force an evaluation order (this is an indirect way of forcing index use or join order, a mitigation), etc.Query planning is something where a poor choice can have serious performance rami