Statistics and Selectivity: Why the Planner Guesses Wrong
To pick a plan, the planner has to guess how many rows each step will return.
Those guesses come from statistics that ANALYZE builds from a sample of your data. If the statistics are stale, the guesses drift. If columns are correlated, the guesses can be wrong even when the statistics are fresh. A bad guess can lead to a bad plan, such as a nested loop where a hash join was needed.
Pro members see the rest of this lesson
- A 4-step state diagram: Collect → Estimate → Multiply → Correct
- The full mechanism: what PostgreSQL does internally, in source terms
- 3 SQL queries you can run, each labelled by how it was verified
- 1 transcript captured on PostgreSQL 17.10
- The closing insight, the mistake it prevents, and what it changes in your work
Check it against the source3 citations in postgres/postgres · file, symbol and line verified on REL_17_STABLE
Primary symbol: analyze_rel · line 111
Primary symbol: eqsel · line 228
Primary symbol: clauselist_selectivity · line 100
Anchored to postgres/postgres on REL_17_STABLE and cross-checked against the manual for PostgreSQL 15–18. Every query here was run and its output captured on a throwaway PostgreSQL 17.10 lab; corrections are noted inline. §14.2 Statistics Used by the Planner in the official docs →
Connected
Where this lesson sits
What comes before, after, and alongside it.
Part of these pathways
Same learning track
Finished a free lesson?
Pro opens the rest of the engine course
You felt one mechanism. Pro is the full bodies, interview depth, and the tracks that build on this session.