← Back to PostgreSQL, page 3

postgresql-0041 · Medium mid

Read EXPLAIN ANALYZE output: compare estimated and actual rows to spot a bad estimate.

EXPLAIN ANALYZE runs the query and shows, for each plan node, the planner's estimated rows next to actual rows and loops. A large gap means the statistics or assumptions are wrong, often from correlated columns or stale stats, and leads to bad join choices. ANALYZE refreshes statistics, and CREATE STATISTICS captures correlations between columns.

Full answer

Setup for the example:

sql
CREATE TABLE addresses (id int, city text, pincode text);
INSERT INTO addresses SELECT g, c.city, c.pin FROM generate_series(1, 20000) g
  CROSS JOIN LATERAL (SELECT (ARRAY['Pune','Delhi','Chennai','Kolkata'])[1 + g % 4] AS city) x
  CROSS JOIN LATERAL (SELECT CASE x.city WHEN 'Pune' THEN '411001' WHEN 'Delhi' THEN '110001' WHEN 'Chennai' THEN '600001' ELSE '700001' END AS pin) p
  CROSS JOIN LATERAL (SELECT x.city, p.pin) c;
ANALYZE addresses;
sql
EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, COSTS ON) SELECT id FROM addresses WHERE city = 'Pune' AND pincode = '411001';

Output:

text
Seq Scan on addresses  (cost=0.00..423.00 rows=1250 width=4) (actual rows=5000 loops=1)
  Filter: ((city = 'Pune'::text) AND (pincode = '411001'::text))
  Rows Removed by Filter: 15000
sql
CREATE STATISTICS addr_city_pin (dependencies) ON city, pincode FROM addresses; ANALYZE addresses; EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, COSTS ON) SELECT id FROM addresses WHERE city = 'Pune' AND pincode = '411001';

PostgreSQL returns:

text
Seq Scan on addresses  (cost=0.00..423.00 rows=5000 width=4) (actual rows=5000 loops=1)
  Filter: ((city = 'Pune'::text) AND (pincode = '411001'::text))
  Rows Removed by Filter: 15000

City and pincode are perfectly correlated: every Pune row has 411001. Without extended statistics, the planner multiplies the two selectivities (a quarter times a quarter) and expects about 1,250 rows, while 5,000 actually match, as the 'rows=' figures in the first plan show. After CREATE STATISTICS with dependencies and a fresh ANALYZE, the estimate moves to about 5,000. Cost figures are printed because the estimates live there; they can differ slightly between machines, but the estimated-versus-actual comparison is the point. In real plans, look for the lowest node where estimates go wrong.

sql
CREATE TABLE addresses (id int, city text, pincode text);
INSERT INTO addresses SELECT g, c.city, c.pin FROM generate_series(1, 20000) g
  CROSS JOIN LATERAL (SELECT (ARRAY['Pune','Delhi','Chennai','Kolkata'])[1 + g % 4] AS city) x
  CROSS JOIN LATERAL (SELECT CASE x.city WHEN 'Pune' THEN '411001' WHEN 'Delhi' THEN '110001' WHEN 'Chennai' THEN '600001' ELSE '700001' END AS pin) p
  CROSS JOIN LATERAL (SELECT x.city, p.pin) c;
ANALYZE addresses;

EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, COSTS ON) SELECT id FROM addresses WHERE city = 'Pune' AND pincode = '411001';

CREATE STATISTICS addr_city_pin (dependencies) ON city, pincode FROM addresses; ANALYZE addresses; EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, COSTS ON) SELECT id FROM addresses WHERE city = 'Pune' AND pincode = '411001';

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • Compare estimated rows with actual rows
  • Correlated columns cause underestimates
  • CREATE STATISTICS captures dependencies
  • ANALYZE refreshes statistics
  • Fix the lowest misestimated node first

Common mistakes

  • Reading only total time and ignoring row estimates
  • Running EXPLAIN ANALYZE on writes without a rollback

Follow-ups

  • What do ndistinct and mcv statistics add?
  • How does default_statistics_target matter?

Tips

  • Point at rows= values