Lesson 26 · Senior PostgreSQL backend engineering
Planner misestimates and cardinality
Why Postgres picks the wrong plan, and how row-count thinking explains more than “the planner is dumb.”
Your win: explain why planner mistakes usually start with row-count mistakes, and describe the senior debugging path from misestimate to a better plan.
The problem: a plan built on the wrong picture of the data
When engineers say “Postgres chose a bad plan,” what they usually mean is “Postgres believed the data shape was different from reality.” That is a much better starting point for debugging, because now you can ask: which estimate was wrong, and what plan choice followed from it?
Where misestimates come from
- stale or coarse statistics
- skewed value distributions
- column correlation the planner cannot model well enough
- complex predicates, expressions, or joins
- parameter-sensitive queries where one input value is tiny and another is huge
The important habit is to stop treating the planner like a black box. It is following a model. Your job is to find where the model no longer matches reality.
EXPLAIN ANALYZE.
Planner statistics + Using EXPLAIN
These are the official sources behind the estimate-versus-actual workflow. Read them with one question in mind: “What did the planner think would happen?”
Check yourself (from memory)
Q1. Many bad plans start with…
Sources. Planner Statistics; Using EXPLAIN.