Sub topic 11
Reading BUFFERS output
This chapter assumes you can read a plan tree and the actual time, rows and loops fields from EXPLAIN ANALYZE. It also assumes you know that PostgreSQL stores tables and indexes in 8 kB pages, called blocks. Timings vary with load and caching. Buffer counts mostly do not, so they are a steadier measure of how much work a node did.
What BUFFERS adds
EXPLAIN (ANALYZE, BUFFERS) adds a Buffers: line to each plan node. The line counts the 8 kB blocks the node touched, grouped by where they came from and what happened to them.
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
WHERE customer_id = 42;A typical result on a cold cache:
Seq Scan on orders (cost=0.00..18334.00 rows=52 width=96) (actual time=0.9..180.4 rows=50 loops=1)
Filter: (customer_id = 42)
Rows Removed by Filter: 999950
Buffers: shared hit=64 read=8270
Planning Time: 0.1 ms
Execution Time: 180.6 msHere shared hit=64 read=8270 means the node touched 8,334 blocks. That is 8,334 × 8 kB, about 65 MB, to return 50 rows. The number explains the 180 ms better than the time alone does.
In PostgreSQL 18 and later, BUFFERS is on by default with ANALYZE. In earlier versions you must ask for it.
The counters
Each counter counts blocks, not rows or bytes.
| Counter | Meaning |
|---|---|
| `shared hit` | The block was already in PostgreSQL's shared buffer cache (`shared_buffers`). |
| `shared read` | The block was not in the cache, so PostgreSQL asked the operating system for it. |
| `shared dirtied` | The query changed a block that was clean in the cache. |
| `shared written` | The query had to write a dirty block out to make room. |
| `local hit/read/dirtied/written` | The same counters for temporary tables, which use session-private memory. |
| `temp read/written` | Blocks of temporary files used when a sort or hash spills past `work_mem`. |
Counters with a value of zero are left out, so a line like Buffers: shared hit=12 means the other counters were zero.
Hit versus read
A read is not necessarily a disk read. PostgreSQL only knows that the block was missing from shared_buffers. The operating system's page cache may still have served it from RAM. If a read is fast, the OS cache probably had it. If it is slow, it probably came from storage.
A hit is also not a count of distinct blocks. It counts each time a block was found in the cache and used. An index scan that visits the same table page 30 times records 30 hits for that page.
Cold and warm cache
Run the same query twice and compare. The first run fills the cache. The second shows the steady state.
-- first run
Buffers: shared hit=64 read=8270
Execution Time: 180.6 ms
-- second run
Buffers: shared hit=8334
Execution Time: 61.2 msThe total, 8,334 blocks, is the same in both runs. Only the split between hit and read changed. The total tells you how much work the plan does. The split tells you how much of that work waited on I/O at the time you ran it.
Two rules follow from this:
- When you compare two versions of a query, compare total blocks. A fix that cuts 8,334 blocks to 12 is real, even if cache state muddies the timings.
- When you diagnose a query that is slow only sometimes, look at the
readcount. Highreadin production and highhiton your laptop is a common reason for "it was fast when I tested it."
Seeing I/O time
Block counts do not say how long the reads took. To see that, turn on track_io_timing:
SET track_io_timing = on;
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
WHERE customer_id = 42;Changing this setting normally needs superuser rights or a granted privilege. With it on, each node that did reads adds a line such as:
Buffers: shared hit=64 read=8270
I/O Timings: shared read=121.350The value is in milliseconds and is the total time spent waiting for block reads. In this example, about 121 ms of a 180 ms scan was I/O wait. The rest was CPU work, such as applying the filter to a million rows.
Timing every read has a small cost on systems with slow clocks. The pg_test_timing tool shows whether your system is affected.
Buffers are cumulative
A node's Buffers: line includes the blocks used by its child nodes. This differs from what you might expect, so read parents carefully.
Hash Join (actual time=40.1..210.8 rows=9800 loops=1)
Buffers: shared hit=120 read=9310
-> Seq Scan on orders o (actual time=0.02..95.0 rows=500000 loops=1)
Buffers: shared read=8270
-> Hash (actual time=39.8..39.8 rows=10000 loops=1)
Buffers: shared hit=120 read=1040
-> Seq Scan on customers c (actual time=0.01..22.0 rows=10000 loops=1)
Buffers: shared hit=120 read=1040The join reports 9,430 blocks in total. The two scans account for 8,270 + 1,160 = 9,430 of them. The join itself touched no blocks of its own. To find a node's own work, subtract its children's counts from its count.
Loops
With loops greater than 1, the buffer counts are totals across all loops, not per-loop averages. This is the opposite of actual time and rows, which are per-loop averages.
Nested Loop (actual time=0.05..48.0 rows=1000 loops=1)
Buffers: shared hit=4010 read=60
-> Seq Scan on customers c (actual time=0.01..0.3 rows=1000 loops=1)
Buffers: shared hit=10
-> Index Scan using orders_customer_id_idx on orders o (actual time=0.01..0.04 rows=1 loops=1000)
Index Cond: (customer_id = c.id)
Buffers: shared hit=4000 read=60The inner index scan shows hit=4000 for 1,000 loops, or about 4 blocks per lookup: two or three index pages and one table page. Dividing by loops gives that per-lookup cost. A total of 4,060 blocks looks small until you notice that a hash join might read each table once and touch fewer blocks overall.
Temporary blocks and spills
The temp read and temp written counters show when an operation did not fit in work_mem:
Sort (actual time=410.2..512.7 rows=1000000 loops=1)
Sort Key: created_at
Sort Method: external merge Disk: 20480kB
Buffers: shared hit=8334, temp read=2560 written=2560The 2,560 blocks of 8 kB equal the 20,480 kB the sort wrote to disk. A sort that stays in memory reports Sort Method: quicksort Memory: ... and has no temp counters. A hash that overflows shows Batches greater than 1 along with temp blocks. Raising work_mem for the session and re-running confirms the diagnosis:
SET work_mem = '64MB';
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
ORDER BY created_at;Be careful with large values. work_mem applies per sort or hash operation, so one query with several of them can use a multiple of the setting, and many sessions multiply that again.
Dirtied and written blocks
Writes can show up in a plain SELECT:
Buffers: shared hit=100 read=8200 dirtied=8200PostgreSQL sets hint bits on rows the first time it checks whether they are committed. That marks the page dirty. This happens after a large load and goes away on later runs. written appears when a backend has to flush a dirty block itself to free a buffer, which suggests the background writer and checkpoints are not keeping up.
Planning buffers
From PostgreSQL 13, the buffers used while planning are reported separately, near the end of the output:
Planning:
Buffers: shared hit=210 read=18
Planning Time: 3.4 msPlanning reads system catalog pages. A cold cache or a query over many partitions can make this noticeable. If planning time is a large share of execution time, check this line.
A reading checklist
- Find the node with the largest total block count after subtracting its children.
- Check whether the blocks are
hitorread. Largereadcounts mean the timing depends on cache state, so re-run the query before you judge it. - For nodes with
loopsabove 1, divide the buffer count byloopsto get the per-iteration cost. - Look for
tempcounters and fix spills before tuning anything else in that node. - Compare the total block count before and after your change.
Exercise
An Index Scan node shows loops=500 and Buffers: shared hit=1480 read=20. Its parent Nested Loop shows Buffers: shared hit=1490 read=20. About how many blocks did each index lookup touch, and how many blocks did the nested loop itself use?
Show answer
The scan touched 1,500 blocks in total (1,480 + 20). Dividing by 500 loops gives about 3 blocks per lookup. The parent reports 1,510 blocks, but 1,500 come from the index scan. The other child, the outer scan, accounts for the remaining 10 blocks, so the nested loop itself used none.
Exercise
You run a query twice. The first run shows Buffers: shared hit=10 read=5000 and takes 900 ms. The second shows shared hit=5010 and takes 40 ms. Did the second run do less work? What should you measure to judge a query change?
Show answer
No. Both runs touched 5,010 blocks. The second was faster because the blocks were already cached, so the reads turned into hits. To judge a change, compare the total block count (hit + read) across versions, and use the read count and I/O Timings to understand cache effects.
Exercise
A Sort node shows "Sort Method: external merge Disk: 16384kB" and Buffers: temp read=2048 written=2048. Confirm that these two numbers agree, and name one way to avoid the spill.
Show answer
2,048 blocks × 8 kB = 16,384 kB, so they agree. The sort can be kept in memory by raising work_mem for that session above about 16 MB (with some headroom), by sorting fewer rows or columns, or by using an index that already returns rows in the needed order.