What pg_stat_io answers that older views did not
Before PostgreSQL 16 you had pg_stat_bgwriter (one row, cluster-wide) and per-table hit counters in pg_statio_*. Neither told you which process type did the I/O or why. pg_stat_io has one row per (backend_type, object, context) with counts of reads, writes, writebacks, extends, hits, evictions, reuses and fsyncs.
Use it to answer:
- Are client backends writing dirty buffers themselves because the checkpointer and bgwriter cannot keep up?
- Is
shared_bufferstoo small for the hot set (high evictions in thenormalcontext)? - Is autovacuum or a bulk load churning a small ring buffer, or evicting everyone else's pages?
- Where does fsync time go?
Tip
Turn on
track_io_timing(andtrack_wal_io_timingfor WAL) or the*_timecolumns stay at zero. Measure the overhead first withpg_test_timing. On modern Linux with a TSC clock source it is usually negligible.
Column reference
| Column | Meaning |
|---|---|
backend_type | client backend, autovacuum worker, checkpointer, background writer, startup, walsender, background worker and so on |
object | relation (permanent tables/indexes), temp relation; PostgreSQL 18 adds wal |
context | normal (shared buffers), vacuum, bulkread, bulkwrite (ring buffers), and in 18 init for WAL |
reads, writes, extends | Number of operations |
op_bytes (16, 17) | Bytes per operation, normally the block size |
read_bytes, write_bytes, extend_bytes (18) | Total bytes; replace op_bytes |
hits | Found in shared buffers |
evictions | A buffer was evicted so this backend could use it |
reuses | A ring buffer slot was reused (bulk contexts only) |
writebacks, fsyncs | Kernel writeback requests and fsync calls |
Column sets differ between 16, 17 and 18. Run \d pg_stat_io on your build before you copy queries into monitoring.
Core queries
Who does the I/O
SELECT backend_type, object, context,
reads, writes, extends, hits, evictions, reuses, fsyncs,
round(read_time::numeric, 0) AS read_ms,
round(write_time::numeric, 0) AS write_ms,
round(fsync_time::numeric, 0) AS fsync_ms
FROM pg_stat_io
WHERE coalesce(reads,0) + coalesce(writes,0) + coalesce(extends,0) > 0
ORDER BY coalesce(reads,0) + coalesce(writes,0) DESC;
NULL is not zero here. It means the operation is impossible for that combination, for example hits for the checkpointer.
Shared-buffer hit ratio by backend type
SELECT backend_type,
sum(hits) AS hits,
sum(reads) AS reads,
round(100.0 * sum(hits) / nullif(sum(hits) + sum(reads), 0), 2) AS hit_pct
FROM pg_stat_io
WHERE object = 'relation' AND context = 'normal'
GROUP BY backend_type
ORDER BY reads DESC NULLS LAST;
A "read" here may still be served by the OS page cache. Compare read_time / reads: tens of microseconds means page cache, milliseconds means real storage.
Backends doing the writer's job
SELECT backend_type, writes, fsyncs,
round(write_time::numeric / nullif(writes,0), 3) AS ms_per_write
FROM pg_stat_io
WHERE object = 'relation' AND context = 'normal'
AND backend_type IN ('client backend','checkpointer','background writer','autovacuum worker')
ORDER BY writes DESC NULLS LAST;
If client backend writes are a large share of total writes, foreground queries are cleaning buffers before they can use them. That adds latency to user queries.
Ring-buffer behaviour of vacuum and bulk operations
SELECT backend_type, context, reads, writes, reuses, evictions
FROM pg_stat_io
WHERE context IN ('vacuum','bulkread','bulkwrite')
ORDER BY reads DESC NULLS LAST;
High writes in the vacuum context means vacuum keeps flushing dirty pages from its small ring. Each flush may also force a WAL flush. PostgreSQL 16+ lets you size the ring with vacuum_buffer_usage_limit (and VACUUM (BUFFER_USAGE_LIMIT ...)).
WAL I/O (PostgreSQL 18)
SELECT backend_type, context, writes, write_bytes, fsyncs,
round(fsync_time::numeric, 0) AS fsync_ms
FROM pg_stat_io
WHERE object = 'wal'
ORDER BY write_bytes DESC NULLS LAST;
On 16 and 17, use pg_stat_wal (wal_records, wal_fpi, wal_bytes, wal_buffers_full, and the write/sync counters, which vary by version).
Taking deltas, not lifetime totals
Lifetime counters hide incidents. Snapshot and diff:
CREATE SCHEMA IF NOT EXISTS dba;
CREATE TABLE IF NOT EXISTS dba.io_snap AS
SELECT now() AS ts, * FROM pg_stat_io WITH NO DATA;
INSERT INTO dba.io_snap SELECT now(), * FROM pg_stat_io; -- run every 5 min (pg_cron)
-- Delta between the last two snapshots
WITH s AS (
SELECT *, dense_rank() OVER (ORDER BY ts DESC) AS rk FROM dba.io_snap
)
SELECT a.backend_type, a.object, a.context,
a.reads - b.reads AS reads,
a.writes - b.writes AS writes,
a.evictions - b.evictions AS evictions,
round((a.read_time - b.read_time)::numeric, 0) AS read_ms
FROM s a JOIN s b
ON a.rk = 1 AND b.rk = 2
AND a.backend_type = b.backend_type AND a.object = b.object AND a.context = b.context
ORDER BY 4 DESC NULLS LAST;
Reset only when you have to: SELECT pg_stat_reset_shared('io');. It wipes the history other people may be relying on.
Per-session I/O (PostgreSQL 18)
SELECT a.pid, a.usename, a.application_name, b.object, b.context, b.reads, b.read_bytes
FROM pg_stat_activity a
CROSS JOIN LATERAL pg_stat_get_backend_io(a.pid) b
WHERE a.backend_type = 'client backend' AND b.reads > 0
ORDER BY b.read_bytes DESC
LIMIT 20;
Symptom → cause → fix
| Pattern in pg_stat_io | Likely cause | Fix |
|---|---|---|
client backend writes in normal context are high | Checkpointer/bgwriter not keeping up; buffers dirty when evicted | Raise bgwriter_lru_maxpages / bgwriter_lru_multiplier, spread checkpoints (checkpoint_completion_target), check max_wal_size |
High evictions for client backends, low hit % | Hot set larger than shared_buffers | Increase shared_buffers if RAM allows, or cut the working set (indexes, partition pruning) |
fsyncs by client backend > 0 | fsync request queue full, so backends fsync themselves | Usually a checkpointer that is stalled or storage too slow; investigate storage latency |
Large extends with high extend_time | Bulk inserts extending relations on slow storage | Batch loads, check the filesystem, consider COPY |
autovacuum worker reads dominate | Vacuum scanning big tables repeatedly | Check the visibility map; tune per-table autovacuum_vacuum_scale_factor; consider partitioning |
vacuum context writes high | Ring too small for dirty heap | Raise vacuum_buffer_usage_limit for manual vacuums of large tables |
bulkread reads with long read_time | Large seq scans hitting storage | Check plans, missing indexes, parallel scans hammering disks |
Warning
Do not raise
shared_bufferson a managed service or a memory-tight VM without checking total memory:shared_buffers+max_connections×work_mempeak + OS cache. A restart is required, and an oversized value causes OOM kills.
Prevent it next time
- Export
pg_stat_iodeltas to your monitoring (postgres_exporter and most agents now have a collector) and alert on the client-backend share of writes and on fsyncs by backends. - Keep
track_io_timing = onin your baseline config so the timing data exists when you need it. - Record a baseline after each major version upgrade. Column names changed in 18, so update dashboards as part of the upgrade.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get