EXPLAIN vs. EXPLAIN ANALYZE: Reading the Planner

What EXPLAIN Shows

EXPLAIN asks the query planner to produce its execution plan without running the query. It outputs a tree of plan nodes, each annotated with:

AnnotationMeaning
cost=start..totalPlanner's estimated cost in arbitrary cost units (not milliseconds). start = cost to return first row; total = cost to return all rows.
rows=NEstimated number of rows output by this node.
width=NEstimated average row size in bytes.

Cost units are defined by GUCs — the planner picks the plan with the lowest estimated total cost:

GUCDefaultRepresents
seq_page_cost1.0Baseline: one sequential 8KB page read
random_page_cost4.0Cost of a random page read. Set to 1.1 on SSDs.
cpu_tuple_cost0.01Cost to process one row
cpu_index_tuple_cost0.005Cost to process one index entry
cpu_operator_cost0.0025Cost to apply one operator

What EXPLAIN ANALYZE Adds

EXPLAIN ANALYZE actually executes the query and adds real measurements:

FieldMeaning
actual time=start..totalWall-clock milliseconds, real execution time
actual rows=NRows actually returned
loops=NHow many times this node executed — costs and rows are per-loop
EXPLAIN ANALYZE
SELECT o.id, u.email
FROM orders o
JOIN users u ON u.id = o.user_id
WHERE o.status = 'pending'
  AND o.created_at > now() - interval '7 days';

Hash Join  (cost=1240.00..8900.00 rows=50 width=36)
           (actual time=45.2..312.8 rows=9400 loops=1)
  Hash Cond: (o.user_id = u.id)
  ->  Seq Scan on orders o  (cost=0.00..7200.00 rows=50 width=20)
                             (actual time=0.1..280.0 rows=9400 loops=1)
        Filter: ((status = 'pending') AND (created_at > ...))
        Rows Removed by Filter: 990600
  ->  Hash  (cost=620.00..620.00 rows=50000 width=24)
             (actual time=38.1..38.1 rows=50000 loops=1)
        Buckets: 65536  Batches: 1
        ->  Seq Scan on users u  ...

What this output reveals: The planner estimated rows=50 for the orders scan. Actual was 9,400 — a 188× underestimate. Because the planner thought only 50 rows would come from orders, it chose Hash Join (cheap to build a small hash). With 9,400 rows actually flowing through, this is sub-optimal. Rows Removed by Filter: 990,600 confirms a full table scan through 1M rows — an index on (status, created_at) would fix this. See the Index Types guide for which index shape actually fits this predicate.

Why Estimates Go Wrong

Root CauseMechanismFix
Stale statisticspg_statistic not updated since last ANALYZEANALYZE table; or tune autovacuum_analyze_scale_factor
Non-uniform distributionA few dominant values; histogram has low resolutionALTER TABLE t ALTER COLUMN c SET STATISTICS 500; ANALYZE;
Correlated columnsPlanner treats WHERE city='NYC' AND zip='10001' as independent; multiplies selectivities → overestimates selectivityCREATE STATISTICS s1 ON city, zip FROM addresses; ANALYZE;
Function in WHEREWHERE lower(email) = 'x' — no stats on the transformed valueFunctional index + expression statistics
-- Extended statistics: tell the planner these columns are correlated (Postgres 10+)
CREATE STATISTICS orders_status_date ON status, created_at FROM orders;
ANALYZE orders;