Buffers in EXPLAIN: shared hits, reads, temp blocks and what they cost
EXPLAIN (ANALYZE, BUFFERS) adds block counts to every node. PostgreSQL reads data in 8 kB pages by default, so a buffer number multiplied by 8 gives kilobytes. The counts separate blocks found in the shared buffer cache from blocks that had to be read from the operating system, and temporary blocks used for spills. That separation is often the difference between a plan that is fast on a warm cache and slow in production.
The common labels are shared hit for a block found in the shared buffers, shared read for one fetched from the OS, shared dirtied for one modified, shared written for one written back, and temp read and temp written for temporary files. Local buffers appear for temporary tables and some internal operations. Heap fetches appear on index-only scans and count visits to the heap that the visibility map could not avoid.
| Counter | Meaning | Cost signal |
|---|---|---|
| shared hit | Block found in shared buffers | Cheap, but still CPU to copy |
| shared read | Block read from the OS | Expensive, especially remote storage |
| shared dirtied | Block modified by this statement | Write amplification, checkpoint pressure |
| temp read/written | Blocks in temporary spill files | Very expensive, fix work_mem or the plan |
| heap fetches | Heap visits during an index-only scan | Vacuum and visibility map health |
Hits are not free
A high hit count means the cache saved a disk read, not that the work was free. Each hit still copies a page and runs the executor's per-row logic. A query with ten million hits can spend seconds in CPU without a single read. When both hit and read counts are high, the plan is touching a lot of data; the question becomes whether it needs to touch that much, which is an index or predicate question rather than a cache question.
On the other hand, a high read count with a low hit count on a query that runs often suggests the working set does not fit. The fix may be a smaller index, a better predicate, more memory, or accepting the I/O because the query is genuinely large. Do not raise shared_buffers reflexively; the operating system page cache also caches files, and PostgreSQL relies on it.
Temp blocks are the loudest signal
Temp read and temp written mean the executor created temporary files. Sorts and hashes spill when their memory allowance is exceeded, and the blocks are written then read back. A plan with temp blocks is doing disk I/O that a different plan or a larger work_mem could avoid. The analyzer flags any temp block as high and shows the sort method and disk size next to it.
Raising work_mem is a per-operation allowance, not a global pool, so a busy server with many concurrent sorts can multiply it quickly. A safer change is often an index that returns rows in the required order, removing the sort, or a predicate that reduces the row count before the sort. Use the buffer numbers to decide which operation spilled, then fix that operation.
I/O timing and the OS
With track_io_timing = on, PostgreSQL can report time spent reading and writing blocks on each node. That distinguishes a plan that reads many blocks from fast cache from one that reads fewer blocks from slow storage. The setting adds a small overhead per I/O call, so it is often enabled only for diagnostics or on replicas. MySQL has its own instrumentation and performance schema tables for the same purpose.
Buffer counts describe PostgreSQL's view. The operating system may serve a read from its own page cache without touching the disk, which PostgreSQL still counts as a read. That is why a cold-cache test and a warm-cache test can show the same read count but very different times. When comparing runs, state whether the cache was warm and whether the storage was local or network.
A short checklist
- Sum shared reads on the leaf nodes to see the real I/O volume.
- Compare reads with hits on repeated runs; a stable high read count means the set does not fit.
- Treat any temp read or written block as a spill worth fixing.
- On an index-only scan, check heap fetches; many mean vacuum is behind.
- Enable track_io_timing when the difference between block count and time is puzzling.
- Record cache and storage conditions with every measurement.
The analyzer shows the raw Buffers text for each node in its detail row and flags temp I/O as a high finding. It does not know your cache size or storage latency, so it cannot say whether 40,000 reads are acceptable. That judgement needs the workload and the hardware, which is exactly the context a plan alone cannot carry.
Time tells you how slow; buffers tell you why. Read them together, and always note whether the cache was warm.
Sources
- PostgreSQL documentation — EXPLAIN BUFFERS
- PostgreSQL documentation — Memory configuration
- PostgreSQL documentation — Monitoring statistics
Questions
What is the difference between shared hit and shared read?
A hit means the block was already in PostgreSQL's shared buffer cache and no operating-system read was needed. A read means it was not, so PostgreSQL asked the OS for it. Reads are much slower, especially on network storage. A high read count on a repeated query means the working set does not fit the cache.
Why do buffers appear per node?
Each node reports the blocks it touched while producing its rows, including blocks read by its children only if the child is not shown separately. In practice, sum the buffers of the leaf nodes to see the I/O, and use parent numbers to see how much of it the parent caused.
What does temp read and temp written mean?
The query used temporary files, usually for a sort or a hash that exceeded work_mem. Temp written counts blocks written to those files; temp read counts blocks read back. Both are disk I/O and usually the most expensive line in a plan.
The analyzer copies each node's Buffers line into its detail row and flags temp reads and writes.
Open the analyzerWritten against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.
Back to the guides