skip to content

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:

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' AND c.country = 'FR'
  5. 5GROUP BY c.id
the same plan, as EXPLAIN prints it
  1. GroupAggregate (cost=63.01..63.11 rows=2 width=24) (actual rows=66.00 loops=1)
  2. Group Key: c.id
  3. Buffers: shared hit=121 read=36
  4. -> Sort (cost=63.01..63.04 rows=10 width=16) (actual rows=369.00 loops=1)
  5. Sort Key: c.id
  6. Sort Method: quicksort Memory: 29kB
  7. Buffers: shared hit=121 read=36
  8. -> Nested Loop (cost=0.29..62.84 rows=10 width=16) (actual rows=369.00 loops=1)
  9. Buffers: shared hit=121 read=36
  10. -> Seq Scan on customers c (cost=0.00..54.00 rows=2 width=16) (actual rows=66.00 loops=1)
  11. Filter: ((city = 'Paris'::text) AND (country = 'FR'::bpchar))
  12. Rows Removed by Filter: 1934
  13. Buffers: shared read=24
  14. -> 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)
  15. Index Cond: (customer_id = c.id)
  16. Heap Fetches: 0
  17. Index Searches: 66
  18. Buffers: shared hit=121 read=12
Seq Scan on customers cread #1 of 5
actual rows
66
what it really returned, per loop. the estimate was 2: 33× off.
loops
1
how many times it was started. rows × loops is the total.

#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.

node 1/5
real output, postgres 18.6 (PGlite 0.5.12) · explain-analyze
switch between estimates and actuals, and step through it from the scan up. the scan of customers expected 2 rows and got 66: a 33x miss, and every node above it was planned for 2 customers. the inner index scan ran 66 times (loops), once per customer.

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

sqldb=shop · session ea
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;
output, from postgres 18.6
#1 · CREATE INDEX
#2 · EXPLAIN
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 Planning: Buffers: shared hit=118 read=18 dirtied=1

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.

loops
66
actual rows (per loop)
5.59
rows in total
369
loop 1orders per customer, one bar per looploop 66
real output, postgres 18.6 (PGlite 0.5.12) · explain-analyze
the 66 runs of the inner index scan, one bar per paris customer, as tall as the orders it found for them. EXPLAIN prints only the average. the total is rows × loops: the 369 rows the Nested Loop returned.
predict the output
the inner scan says rows=5.59 loops=66. how many rows did it return in total?
text
->  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:

run =
query.sql
  1. 1EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
  2. 2SELECT name FROM customers WHERE id IN (1, 500, 1000)
what postgres printed
  1. Index Scan using customers_pkey on customers (actual rows=3.00 loops=1)
  2. Index Cond: (id = ANY ('{1,500,1000}'::integer[]))
  3. Index Searches: 3
  4. Buffers: shared hit=2 read=7
pages read from the OS (Buffers: shared read)
7 pages
0 pages

the first run on a fresh database: 7 of the 9 pages it needs aren't in shared buffers yet.

real output, postgres 18.6 (PGlite 0.5.12) · buffers
one lookup of three customers, first run and second run. the bar counts the pages that had to come from the operating system (read); the rest were already in shared buffers (hit). same 9 pages both times.
sqldb=shop
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);
output, from postgres 18.6
#1 · EXPLAIN
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 Planning: Buffers: shared hit=78 read=5 dirtied=1
#2 · EXPLAIN
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=9

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:

predict the output
no BEGIN. after this, how many cancelled orders are left?
sql
EXPLAIN ANALYZE DELETE FROM orders WHERE status = 'cancelled';
SELECT count(*) FROM orders WHERE status = 'cancelled';

wrap it in a transaction and roll it back:

sqldb=shop
BEGIN;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF)
DELETE FROM orders WHERE status = 'cancelled';
ROLLBACK;
 
SELECT count(*) FROM orders WHERE status = 'cancelled';
output, from postgres 18.6
#1 · BEGIN
#2 · EXPLAIN
Delete on orders (actual rows=0.00 loops=1) Buffers: shared hit=717 read=86 dirtied=84 -> Seq Scan on orders (actual rows=317.00 loops=1) Filter: (status = 'cancelled'::text) Rows Removed by Filter: 9683 Buffers: shared read=83 Planning: Buffers: shared hit=27 read=3 Trigger for constraint order_items_order_id_fkey: calls=317
#3 · ROLLBACK
#4 · SELECT 1
countint8
317
(1 row)

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. SERIALIZE pg 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.
sqldb=shop
EXPLAIN (ANALYZE, SERIALIZE, COSTS OFF, BUFFERS OFF, TIMING OFF, SUMMARY OFF)
SELECT * FROM customers;
output, from postgres 18.6
EXPLAIN
Seq Scan on customers (actual rows=2000.00 loops=1) Serialization: output=191kB format=text

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.