Sub topic 7
Spotting I/O-heavy queries with buffer counters
Assume you know basic SQL and can query pg_stat_statements. You should also understand that PostgreSQL reads data in fixed-size units called blocks. A buffer is an area of memory that holds a database block. PostgreSQL tries to serve repeated reads from its shared buffer cache instead of reading storage again.
pg_stat_statements keeps cumulative buffer counters for each normalized query. These counters help you find queries that cause the most buffer activity, but they do not show a timeline or one row for every execution.
Shared buffer hits and reads
The two most useful counters are:
| Counter | Meaning |
|---|---|
| `shared_blks_hit` | Shared blocks found in PostgreSQL's shared buffers |
| `shared_blks_read` | Shared blocks that PostgreSQL had to read into shared buffers |
A hit is usually cheaper than a read. A read can involve the operating system's file cache or physical storage, so shared_blks_read does not prove that a disk device was accessed. It does show that PostgreSQL did not already have the block in its own shared buffers.
The counters are cumulative for the current statistics lifetime. A query called thousands of times can have more total reads than a query that scans a large table once. Always examine calls and execution time alongside buffer counts.
Rank queries by shared reads
This query lists statements that caused the most shared blocks to be read:
SELECT queryid,
calls,
shared_blks_hit,
shared_blks_read,
round(
100.0 * shared_blks_hit
/ nullif(shared_blks_hit + shared_blks_read, 0),
1
) AS hit_pct,
left(query, 80) AS query
FROM pg_stat_statements
ORDER BY shared_blks_read DESC
LIMIT 10;The hit_pct expression calculates the percentage of shared block accesses served from shared buffers. nullif prevents division by zero for a statement with no recorded shared block activity.
A high shared_blks_read value can indicate a large scan, an inefficient join, missing or ineffective indexes, or a workload that does not fit in memory. It is a signal for investigation, not proof of a particular cause.
A high hit percentage does not automatically mean that the query is efficient. A query can repeatedly process the same blocks, consume substantial CPU time, or return very few rows after examining many blocks.
Interpret counters per call
Use per-call values when statements have very different call counts:
SELECT queryid,
calls,
round(shared_blks_read::numeric / nullif(calls, 0), 1)
AS read_blocks_per_call,
round(shared_blks_hit::numeric / nullif(calls, 0), 1)
AS hit_blocks_per_call,
round(mean_exec_time::numeric, 2) AS mean_ms,
left(query, 80) AS query
FROM pg_stat_statements
WHERE calls > 0
ORDER BY read_blocks_per_call DESC
LIMIT 10;A query with many reads per call may be a scan-heavy statement. A query with modest reads per call but millions of calls may still dominate the system. Compare both per-call and aggregate rankings before choosing what to optimize.
Follow the signal to a plan
After selecting a representative query, run it with realistic parameter values:
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
WHERE customer_id = 42;BUFFERS reports hits and reads for that execution and for plan nodes. Compare those results with the cumulative statistics. Look for sequential scans that examine many blocks, joins that repeatedly read large inputs, or an index plan that still performs many heap fetches.
Exercise
A statement has 10,000 calls, 10,000,000 shared buffer hits, and 20,000 shared buffer reads. Is its hit percentage high, and does that prove the statement is efficient?
Show answer
Its hit percentage is about 99.8%, but that does not prove efficiency. It still processes about 1,003 shared blocks per call on average, and its total execution time, CPU work, and result size may still be excessive.