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:
| Annotation | Meaning |
|---|---|
cost=start..total | Planner's estimated cost in arbitrary cost units (not milliseconds). start = cost to return first row; total = cost to return all rows. |
rows=N | Estimated number of rows output by this node. |
width=N | Estimated average row size in bytes. |
Cost units are defined by GUCs — the planner picks the plan with the lowest estimated total cost:
| GUC | Default | Represents |
|---|---|---|
seq_page_cost | 1.0 | Baseline: one sequential 8KB page read |
random_page_cost | 4.0 | Cost of a random page read. Set to 1.1 on SSDs. |
cpu_tuple_cost | 0.01 | Cost to process one row |
cpu_index_tuple_cost | 0.005 | Cost to process one index entry |
cpu_operator_cost | 0.0025 | Cost to apply one operator |
What EXPLAIN ANALYZE Adds
EXPLAIN ANALYZE actually executes the query and adds real measurements:
| Field | Meaning |
|---|---|
actual time=start..total | Wall-clock milliseconds, real execution time |
actual rows=N | Rows actually returned |
loops=N | How 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=50for the orders scan. Actual was 9,400 — a 188× underestimate. Because the planner thought only 50 rows would come fromorders, 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,600confirms 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 Cause | Mechanism | Fix |
|---|---|---|
| Stale statistics | pg_statistic not updated since last ANALYZE | ANALYZE table; or tune autovacuum_analyze_scale_factor |
| Non-uniform distribution | A few dominant values; histogram has low resolution | ALTER TABLE t ALTER COLUMN c SET STATISTICS 500; ANALYZE; |
| Correlated columns | Planner treats WHERE city='NYC' AND zip='10001' as independent; multiplies selectivities → overestimates selectivity | CREATE STATISTICS s1 ON city, zip FROM addresses; ANALYZE; |
| Function in WHERE | WHERE lower(email) = 'x' — no stats on the transformed value | Functional 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;