EXPLAIN ANALYZE and BUFFERS
estimates next to actuals, loops, shared hit vs read, and the Index Searches line new in postgres 18.
- address
- everything.execution.explain_analyze
- part
- part 9, how a query runs
- chapter
- chapter 5 of 13
- reading time
- 9 min
plain EXPLAIN guesses. EXPLAIN ANALYZE runs the query and prints, next to every guess, what really happened: actual rows and loops for every node, and since postgres 18 the buffers each node touched, without being asked.
two things trip everyone up: actual rows is per loop, and the statement really runs, DELETE included.
the picture
you know how a plan can look perfectly sensible and still take a minute? the plan isn't lying, it's guessing. EXPLAIN ANALYZE prints the forecast next to the weather.
the forecast is the estimate: rows the planner expected each node to return. the weather is what came out when the query actually ran. read them side by side, and the node where they part ways is usually where the plan went wrong.
here are paris customers in france with their order counts, on shop with an index on orders.customer_id:
- 1SELECT c.name, count(*) AS orders
- 2FROM customers AS c
- 3JOIN orders AS o ON o.customer_id = c.id
- 4WHERE c.city = 'Paris' AND c.country = 'FR'
- 5GROUP BY c.id
- GroupAggregate (cost=63.01..63.11 rows=2 width=24) (actual rows=66.00 loops=1)
- Group Key: c.id
- Buffers: shared hit=121 read=36
- -> Sort (cost=63.01..63.04 rows=10 width=16) (actual rows=369.00 loops=1)
- Sort Key: c.id
- Sort Method: quicksort Memory: 29kB
- Buffers: shared hit=121 read=36
- -> Nested Loop (cost=0.29..62.84 rows=10 width=16) (actual rows=369.00 loops=1)
- Buffers: shared hit=121 read=36
- -> Seq Scan on customers c (cost=0.00..54.00 rows=2 width=16) (actual rows=66.00 loops=1)
- Filter: ((city = 'Paris'::text) AND (country = 'FR'::bpchar))
- Rows Removed by Filter: 1934
- Buffers: shared read=24
- -> Index Only Scan using orders_customer_id_idx on orders o (cost=0.29..4.37 rows=5 width=4) (actual rows=5.59 loops=66)
- Index Cond: (customer_id = c.id)
- Heap Fetches: 0
- Index Searches: 66
- Buffers: shared hit=121 read=12
#1start here. the scan keeps the customers in paris and in france: 66 of 2,000. the planner expected 2, and every node above it inherits that 33x miss.
why 2? the planner knows 3.3% of customers live in paris and roughly as many in france, and it multiplies the two as if they had nothing to do with each other. every parisian is in france, so the real answer is 66. that's the independence assumption, and the fix (CREATE STATISTICS) is in extended statistics. this chapter is about reading the evidence.
run it
CREATE INDEX ON orders (customer_id);
EXPLAIN (ANALYZE, TIMING OFF, SUMMARY 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' AND c.country = 'FR'
GROUP BY c.id;every node line has a second parenthesis now, (actual rows=66.00 loops=1), and a Buffers: line under it. the 2 the planner expected changed the whole shape: for 2 customers, looking up their orders one at a time in the index (a nested loop) is cheaper than hashing all of them. for 66 it's still fine. for 6,600 it wouldn't be, and that's how a misestimate turns into a slow query (when estimates go wrong).
loops: rows are per loop
look at the index only scan under the Nested Loop: actual rows=5.59 loops=66. it ran 66 times, once for each customer the scan above it found, and 5.59 is the average per run, not the total.
-> Index Only Scan using orders_customer_id_idx on orders o
(actual rows=5.59 loops=66)the estimate is per loop too: rows=5 on that line means 5 per customer, not 5 in all. and the decimals are new pg 18+: before postgres 18, actual rows was rounded to a whole number, so an inner scan that found a row half the time printed rows=0, and rows × loops gave you nothing.
buffers, by default pg 18+
the Buffers: lines count pages, not rows: every 8 KB page a node asked the buffer pool for. hit means it was already in shared buffers, read means postgres had to ask the operating system for it. in postgres 18, EXPLAIN ANALYZE prints them whether you ask or not (BUFFERS OFF turns them off).
the same lookup, run twice in a row on a fresh copy of the database:
- 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
- 2SELECT name FROM customers WHERE id IN (1, 500, 1000)
- Index Scan using customers_pkey on customers (actual rows=3.00 loops=1)
- Index Cond: (id = ANY ('{1,500,1000}'::integer[]))
- Index Searches: 3
- Buffers: shared hit=2 read=7
the first run on a fresh database: 7 of the 9 pages it needs aren't in shared buffers yet.
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT name FROM customers WHERE id IN (1, 500, 1000);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT name FROM customers WHERE id IN (1, 500, 1000);the same 9 pages both times. the first run has to read most of them in; the second finds them all. the Planning: section under the first plan is the planner's own reading (catalog pages, the index's metapage), and it's gone the second time because the backend caches what it learned. this is why "it was slow the first time" is so often true, and why a benchmark has to say whether it ran warm or cold.
the other new line, Index Searches: 3 pg 18+, counts how many times the scan went down the b-tree from the root. one IN list, three keys, three descents. a plain id = 42 says 1. it's the number to look at when an index scan is slower than it should be: a nested loop's inner index scan says one search per loop (66 in the plan above), and skip scan shows up as one per value it skips to.
it really runs
EXPLAIN ANALYZE doesn't simulate anything. it executes the statement and throws the rows away instead of sending them to you. for a SELECT that's harmless. for this, it isn't:
EXPLAIN ANALYZE DELETE FROM orders WHERE status = 'cancelled';
SELECT count(*) FROM orders WHERE status = 'cancelled';wrap it in a transaction and roll it back:
BEGIN;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
DELETE FROM orders WHERE status = 'cancelled';
ROLLBACK;
SELECT count(*) FROM orders WHERE status = 'cancelled';| countint8 |
|---|
| 317 |
all 317 still there. the plan is worth a look too: Delete on orders returned 0 rows (it returns nothing without RETURNING), its buffers include 84 pages it dirtied, and the last line is the foreign key's cascade, a trigger that fired once per deleted order. that trigger is often where a slow DELETE spends its time, and plain EXPLAIN can't show it. (the rollback undoes the rows, not the work: the xid was used and the dead versions stay until VACUUM.)
what it doesn't count
- sending the rows. EXPLAIN ANALYZE throws the result away, so turning rows into text and shipping them over the network isn't in it.
SERIALIZEpg 17+ adds that step back and says how big the result was. - the clock, unless you ask. by default every node is timed (
actual time=first..last: milliseconds to its first row and to its last, per loop). reading the clock twice per row costs something, so on a big plan TIMING OFF runs faster and still gives rows and loops. every EXPLAIN ANALYZE in this book has TIMING OFF, so the output on the page is the output you get.
EXPLAIN (ANALYZE, SERIALIZE, COSTS OFF, BUFFERS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM customers;191kB of text for 2,000 customers, that the client would have had to receive.
keylines
- EXPLAIN ANALYZE runs the query and prints actual rows and loops next to every estimate.
- actual rows (and time) are per loop. the total is rows × loops.
- postgres 18 prints buffers by default: hit was in shared buffers, read came from the OS.
- Index Searches (postgres 18) counts trips down the b-tree, one per loop for an inner index scan.
- it really executes DML. wrap it in BEGIN ... ROLLBACK.