skip to content

reading EXPLAIN

a tree you read from the inside out. cost, rows, width, and what each number actually means.

address
everything.execution.reading_explain
part
part 9, how a query runs
chapter
chapter 4 of 13
reading time
9 min

EXPLAIN prints a tree of plan nodes, and you read it from the inside out: the most indented lines run first, and rows flow up to the top. every node line carries four estimates: startup cost, total cost, rows and width.

none of them are measured. they're the planner's guesses, in made-up units, and that's fine once you know what each one means.

the picture

you know the wall of arrows EXPLAIN gives you, where the first line is the last thing that happens? kinda cursed. it reads fine once you stop reading it top to bottom.

think of a recipe with sub-recipes. the card says "plate the 5 best dishes", and under it, indented, "sort the dishes", and under that "cook each customer's order", and under that two lines: "chop the onions" and "boil the stock". nobody starts at the plating. you start at the most indented line (the leaves), and each step hands its result up to the line above it (its parent).

here's the query this page takes apart: the five customers in paris with the most orders.

sqldb=shop
EXPLAIN
SELECT c.name, count(*) AS orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE c.city = 'Paris'
GROUP BY c.id
ORDER BY orders DESC, c.id
LIMIT 5;
output, from postgres 18.6
EXPLAIN
Limit (cost=262.54..262.55 rows=5 width=24) -> Sort (cost=262.54..262.70 rows=66 width=24) Sort Key: (count(*)) DESC, c.id -> HashAggregate (cost=260.78..261.44 rows=66 width=24) Group Key: c.id -> Hash Join (cost=49.83..259.13 rows=330 width=16) Hash Cond: (o.customer_id = c.id) -> Seq Scan on orders o (cost=0.00..183.00 rows=10000 width=4) -> Hash (cost=49.00..49.00 rows=66 width=16) -> Seq Scan on customers c (cost=0.00..49.00 rows=66 width=16) Filter: (city = 'Paris'::text)

seven nodes. every -> is a child of the line above it with less indentation, and the lines under a node with no arrow (Filter:, Hash Cond:, Sort Key:) are that node's details. now as a tree, with the text beside it:

numbers =
query.sql
  1. 1SELECT c.name, count(*) AS orders
  2. 2FROM customers AS c
  3. 3JOIN orders AS o ON o.customer_id = c.id
  4. 4WHERE c.city = 'Paris'
  5. 5GROUP BY c.id
  6. 6ORDER BY orders DESC, c.id
  7. 7LIMIT 5
the same plan, as EXPLAIN prints it
  1. Limit (cost=262.54..262.55 rows=5 width=24) (actual rows=5.00 loops=1)
  2. -> Sort (cost=262.54..262.70 rows=66 width=24) (actual rows=5.00 loops=1)
  3. Sort Key: (count(*)) DESC, c.id
  4. Sort Method: top-N heapsort Memory: 17kB
  5. -> HashAggregate (cost=260.78..261.44 rows=66 width=24) (actual rows=66.00 loops=1)
  6. Group Key: c.id
  7. Batches: 1 Memory Usage: 32kB
  8. -> Hash Join (cost=49.83..259.13 rows=330 width=16) (actual rows=369.00 loops=1)
  9. Hash Cond: (o.customer_id = c.id)
  10. -> Seq Scan on orders o (cost=0.00..183.00 rows=10000 width=4) (actual rows=10000.00 loops=1)
  11. -> Hash (cost=49.00..49.00 rows=66 width=16) (actual rows=66.00 loops=1)
  12. Buckets: 1024 Batches: 1 Memory Usage: 7kB
  13. -> Seq Scan on customers c (cost=0.00..49.00 rows=66 width=16) (actual rows=66.00 loops=1)
  14. Filter: (city = 'Paris'::text)
  15. Rows Removed by Filter: 1934
Seq Scan on customers cread #1 of 7
startup cost
0.00
spent before the first row can come out.
total cost
49.00
to hand over every row. it includes the children. arbitrary units, not ms.
rows
66
the rows it expects to return, per loop.
width
16 B
the average size of one of those rows, in bytes: only the columns the nodes above still need.

#1start here. the Seq Scan on customers reads 24 pages, 2,000 rows, and its Filter keeps the 66 in paris. cost 0.00..49.00: nothing to do before its first row, 49 to finish.

node 1/7
real output, postgres 18.6 (PGlite 0.5.12) · reading-explain
hover a node, in the tree or in the text, and its lines light up in the other. step through it in reading order, from the scan of customers up to the Limit. switch to actuals to see what really came out of each node when it ran.

so the order it really runs in is the reverse of the order it's printed in: scan customers, hash them, stream orders past them, group, sort, cut. (the last two are the logical order you already know, GROUP BY before ORDER BY before LIMIT, showing through.)

predict the output
the whole query: what does postgres think it costs?
text
Limit  (cost=262.54..262.55 rows=5 width=24)
  ->  Sort  (cost=262.54..262.70 rows=66 width=24)
  ...

four numbers per line

every node line ends in (cost=startup..total rows=n width=w). one at a time:

total cost is what the planner thinks it costs to run this node to the end, including all of its children. the unit is arbitrary: reading one page in order costs seq_page_cost, which is 1.0, and everything else is priced relative to that. it isn't milliseconds, and it never will be.

it's not magic either. here's the scan of customers, priced by hand from the catalog:

sqldb=shop
EXPLAIN SELECT * FROM customers WHERE city = 'Paris';
 
SELECT relpages, reltuples,
       relpages * current_setting('seq_page_cost')::float8 AS page_cost,
       reltuples * current_setting('cpu_tuple_cost')::float8 AS row_cost,
       reltuples * current_setting('cpu_operator_cost')::float8 AS filter_cost
FROM pg_class
WHERE relname = 'customers';
output, from postgres 18.6
#1 · EXPLAIN
Seq Scan on customers (cost=0.00..49.00 rows=66 width=61) Filter: (city = 'Paris'::text)
#2 · SELECT 1
relpagesint4reltuplesfloat4page_costfloat8row_costfloat8filter_costfloat8
24200024205
(1 row)
the seq scan's 49.00, term by term: pages from pg_class.relpages, rows from reltuples, each times a cost setting. the filter costs a quarter of what handling a row does.

every node has a formula like that. the rest of them, and the knobs, are the cost model.

startup cost is the part of the total spent before the node can return its first row. a scan can hand over its first row right away, so its startup cost is 0.00. a Sort can't: the smallest row might be the last one it reads. so a Sort's startup cost is everything below it plus the sorting, and only the handing out is left for the rest. drawn on one axis, it looks like this:

each node's cost as a span, from startup (its first row) to total (its last). the dark spans are nodes that read all of their input before returning anything: the Hash, the HashAggregate and the Sort. they start late and end almost at once.

that's why startup cost matters. with a LIMIT on top, postgres prefers plans that can stop early, and a plan full of Sorts and Hashes can't. (the iterator model is about exactly that.)

rows is how many rows the node expects to return, not to read. the Seq Scan on customers reads 2,000 rows and expects to return 66. the estimate comes from statistics about the column: Paris is one of its most common values, and ANALYZE wrote down how common:

sqldb=shop
SELECT most_common_freqs[array_position(most_common_vals::text::text[], 'Paris')] AS paris
FROM pg_stats
WHERE tablename = 'customers' AND attname = 'city';
output, from postgres 18.6
SELECT 1
parisfloat4
0.033
(1 row)

0.033 of 2,000 rows is 66. how the planner gets to that number for any WHERE is estimating rows. for a node that runs more than once, it's per run (loops).

width is the average size of one returned row, in bytes, counting only the columns the nodes above still need. not the table's row size:

sqldb=shop
EXPLAIN SELECT * FROM customers;
EXPLAIN SELECT id FROM customers;
output, from postgres 18.6
#1 · EXPLAIN
Seq Scan on customers (cost=0.00..44.00 rows=2000 width=61)
#2 · EXPLAIN
Seq Scan on customers (cost=0.00..44.00 rows=2000 width=4)

same scan, same 44.00, width 61 versus 4. in the paris plan, customers comes out 16 bytes wide (an id of 4 and a name of 12 on average) and orders comes out 4 wide, since all the join needs from orders is customer_id. width times rows is roughly the memory a Hash or a Sort is going to want, which is why the planner tracks it.

quick check
the Hash Join line says rows=330. what is 330?

when you don't care about cost

half the time you want the shape, not the numbers. COSTS OFF drops them:

sqldb=shop
EXPLAIN (COSTS OFF)
SELECT c.name, count(*) AS orders
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE c.city = 'Paris'
GROUP BY c.id
ORDER BY orders DESC, c.id
LIMIT 5;
output, from postgres 18.6
EXPLAIN
Limit -> Sort Sort Key: (count(*)) DESC, c.id -> HashAggregate Group Key: c.id -> Hash Join Hash Cond: (o.customer_id = c.id) -> Seq Scan on orders o -> Hash -> Seq Scan on customers c Filter: (city = 'Paris'::text)

that's the form most of this part uses. the other options worth knowing: ANALYZE runs the query and adds what really happened (the next chapter), VERBOSE adds the output columns of every node, SETTINGS lists the planner settings you changed, and FORMAT JSON prints the same tree as json, which is what tools like this page's figures read.

one level deeperthe Limit's cost, worked out

a Limit doesn't do anything itself, it stops early. so its cost is a slice of its child's: the child's startup cost, plus the fraction of the child's remaining cost that it will consume. it takes 5 of the 66 rows the Sort expects, so:

262.54 + (262.70 − 262.54) × 5 / 66 = 262.55

that interpolation assumes rows come out evenly over the child's run, which is fine for a scan and generous for a Sort, but a Sort's startup cost already holds nearly everything, so the slice is tiny either way.

keylines

  • read EXPLAIN inside out: the most indented nodes run first, rows flow up.
  • cost=startup..total, in units where one sequential page read is 1.0.
  • every node's cost includes its children, so the top line is the whole query.
  • rows is what a node returns (per loop), width is the average bytes per row it returns.
  • a high startup cost means "has to read everything first": Sort, Hash, aggregates.