Cookbook recipe

Fix bad row estimates with extended statistics

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

The planner badly misjudges rows when two columns are correlated (city and postal code, status and type). CREATE STATISTICS teaches it the correlation. Diagnose it Spot the misestimate, then create multivariate stats: CREATE STATISTICS s_geo (dependencies,…

Investigation Path

The planner badly misjudges rows when two columns are correlated (city and postal code, status and type). CREATE STATISTICS teaches it the correlation.

Diagnose it

Spot the misestimate, then create multivariate stats:

CREATE STATISTICS s_geo (dependencies, ndistinct)
  ON city, postal_code FROM addresses;
ANALYZE addresses;

Why it happens

By default PostgreSQL assumes columns are independent, so it multiplies selectivities. When columns are functionally dependent, that product is far too small — leading to nested-loop plans that explode at runtime.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes