Sub topic 11
Reading BUFFERS output
Assume you can write SELECT queries, understand basic indexes, and have used EXPLAIN ANALYZE. This chapter focuses on PostgreSQL’s BUFFERS option. Buffer information helps you see how much data a plan node accessed, not just how long it took.
What buffer counts mean
PostgreSQL reads table and index pages through its shared buffer cache. A page is a fixed-size block of database storage, usually 8 KiB.
Run a query with:
EXPLAIN (ANALYZE, BUFFERS)
SELECT c.name, count(*) AS order_count
FROM customers AS c
JOIN orders AS o ON o.customer_id = c.id
WHERE o.created_at >= DATE '2026-01-01'
GROUP BY c.name
ORDER BY order_count DESC
LIMIT 10;A plan may contain lines such as:
| Counter | Meaning |
|---|---|
| `shared hit` | Pages found in PostgreSQL’s shared buffer cache |
| `shared read` | Pages PostgreSQL had to read into the shared cache |
| `shared dirtied` | Cached pages changed by the operation |
| `shared written` | Cached pages written to storage during the operation |
| `temp read` | Pages read from temporary files |
| `temp written` | Pages written to temporary files |
shared means the page belongs to a regular table or index. local counters apply to temporary tables. temp counters usually indicate work files created for operations such as a large sort or hash.
A shared hit is a logical buffer access. It does not mean the query read nothing. It means PostgreSQL found the page in memory instead of fetching it from the relation’s storage file.
A shared read means the page was absent from PostgreSQL’s cache at that moment. It may have come from an operating-system cache rather than a physical disk, so do not treat every read as a disk operation.
Finding I/O pressure in a plan
Look at the nodes with the largest buffer counts. A sequential scan with many shared read blocks may be reading a large part of a table. That can be reasonable when the query needs many rows. An index scan with many random reads may be slower than a sequential scan, especially when the matching rows are spread across many table pages.
High shared hit counts can also matter. They show repeated or broad access to cached pages, which consumes memory bandwidth and cache space even when no storage read occurs.
Temporary buffers often point to memory pressure in a sort or hash operation. For example, a sort that cannot remain in memory may write and reread temporary pages. Check whether the query can use a useful index, reduce its input rows earlier, or use an appropriate work_mem setting.
A practical comparison
Run the same statement twice in a stable test environment. The first run may show more shared read blocks because the needed pages are cold. The second may show more shared hit blocks. If execution remains slow with mostly hits, the bottleneck may be CPU, row processing, a join, or a sort rather than storage.
Use buffer output together with estimated and actual rows. A node processing far more rows than expected can cause every later node to touch more pages. Fixing the estimate or filtering earlier may reduce total buffer activity.
Exercise
A query reports 50,000 shared hit blocks, 0 shared read blocks, and takes several seconds. Does this prove that storage I/O is the bottleneck?
Show answer
No. The pages were already in PostgreSQL’s cache. Investigate CPU time, row counts, joins, sorts, and repeated processing. Buffer hits still represent work, but they do not prove a physical storage read.