The idea · Lab 3.1.2Every plan choice this whole certificate is built on rests on one thing: the planner's estimate of how many rows a condition will match. Get that estimate right, and the planner reliably picks a good plan. Get it badly wrong, and even a technically-correct plan choice can turn out disastrous, because it was chosen for a workload that does not actually exist.
The classic way this goes wrong is two correlated columns filtered together. By default, PostgreSQL's statistics assume every column is independent of every other one, and multiplies their individual selectivities together to estimate a combined filter's selectivity. When two columns are actually tightly linked — like a city and the zip code that always goes with it — that independence assumption is simply false, and the resulting estimate can be off by a large, measurable factor. This lab reproduces exactly that gap, then closes it with one statistics object.
Reading the Planner's Own Data
SELECT attname, n_distinct, most_common_vals FROM pg_stats WHERE tablename = 'addresses';
n_distinct is the planner's estimate of how many distinct values a column has (a small positive number is an absolute count; a negative fraction means "distinct values scale with table size"). most_common_vals lists the specific values ANALYZE found frequently enough to track individually, which is exactly what a filter like city = 'Springfield' relies on for an accurate estimate.
Why Two Independently-Accurate Columns Still Produce a Bad Combined Estimate
Both city and zip_code can have perfectly accurate individual statistics, and the combined estimate for city = 'Springfield' AND zip_code = '01000' can still be badly wrong — because the default estimate multiplies each column's individual selectivity together, which is only correct if the columns are actually independent. A city and its own zip code are the opposite of independent: knowing one almost entirely determines the other.
Telling the Planner About the Correlation
CREATE STATISTICS addr_stat (dependencies) ON city, zip_code FROM addresses;
ANALYZE addresses;
This builds extended statistics specifically capturing how much knowing one column's value narrows down the other — exactly the relationship the default per-column statistics cannot represent at all.
Raising Detail for One Skewed Column
ALTER TABLE addresses ALTER COLUMN city SET STATISTICS 500;
This is a different fix for a different problem: not correlation between columns, but a single column whose value distribution is too detailed for the default sampling (100 buckets) to capture accurately — common for a column with many values where a handful are extremely frequent and the rest are rare.
Key concepts
The Independence Assumption
PostgreSQL's default per-column statistics estimate a combined multi-condition filter by multiplying each condition's individual selectivity together, which is mathematically correct only when the columns involved are statistically independent. Real-world correlated columns — a city and its zip code, a country and its currency — violate this assumption constantly, and the resulting estimate error scales with how strong the real correlation actually is.
CREATE STATISTICS (dependencies)
Builds extended statistics that specifically measure functional dependency between two or more columns — how much knowing one column's value narrows down the plausible values of another. Once built and refreshed with ANALYZE, the planner consults these extended statistics for filters spanning the same columns, instead of falling back to the independence assumption alone.
What to remember
- The planner's default estimate for a multi-condition filter multiplies each condition's individual selectivity together, which is only correct when the columns are statistically independent
- Two columns can each have perfectly accurate individual statistics and still produce a badly wrong combined estimate when they are correlated in reality
- CREATE STATISTICS ... (dependencies) measures functional dependency between named columns directly, closing exactly this gap
- CREATE STATISTICS only registers the request — a follow-up ANALYZE is required to actually compute and populate it
- ALTER TABLE ... SET STATISTICS solves a different problem: insufficient sampling detail for one skewed column, not correlation between columns