skip to content

In PostgreSQL 18, how do you use pg_stat_io to tell whether client backends are writing out and evicting shared buffers themselves, and what changed in the view in 18?

level: seniorimportance: nice to knowfreq 24%

answer

  1. backend type, object, context
  2. who writes the dirty pages
  3. evictions versus ring reuses
  4. bytes columns replaced op_bytes

basics

~20 s

Filter pg_stat_io to client backend, relation, normal context: many writes or fsyncs suggest backends flush dirty pages the checkpointer and background writer should handle, and high evictions hint at small shared buffers. PostgreSQL 18 replaced op_bytes with byte columns and added WAL rows.

solid answer

~40 s

`pg_stat_io` has one row per combination of `backend_type`, `object` (`relation`, `temp relation`, `wal`) and `context` (`normal`, `init`, `vacuum`, `bulkread`, `bulkwrite`), with counters such as `reads`, `writes`, `extends`, `hits`, `evictions`, `reuses` and `fsyncs`. Client backends should leave writing dirty pages to the checkpointer and background writer as much as possible, so in the `client backend` / `relation` / `normal` row, large `writes` or `fsyncs` point at shared buffers or checkpointer settings that do not keep up, and high `evictions` suggest shared buffers is too small for the working set. `reuses` in `bulkread` are ring-buffer traffic from big scans and are expected. In 18 the view reports `read_bytes`, `write_bytes` and `extend_bytes` instead of `op_bytes`, adds `wal` rows whose timing needs `track_wal_io_timing`, and `pg_stat_get_backend_io(pid)` gives one backend's figures.

code

sql · 7 lines
sql
SELECT backend_type, context, reads, read_bytes, hits, evictions, reuses
FROM pg_stat_io
WHERE object = 'relation'
  AND context IN ('normal', 'bulkread')
  AND reads IS NOT NULL
ORDER BY read_bytes DESC NULLS LAST
LIMIT 10;

go deeper

for a junior

Recall that pg_stat_io counts reads, writes, hits and evictions by backend type, object and context across the whole cluster.

for a middle

Explain the contexts: normal goes through shared buffers, while bulkread, bulkwrite and vacuum use ring buffers, and know which timing settings fill the time columns.

for a senior

Show the diagnosis: compare client-backend writes, fsyncs and evictions with checkpointer and background writer rows, then hand the fix to buffer and checkpoint tuning; update dashboards for 18's byte columns.

for a principal

Weigh which I/O signals deserve alerts versus investigation, and how to keep cross-version dashboards honest when views change shape between majors.

## The shape of pg_stat_io `pg_stat_io` is a cluster-wide view: one row for each combination of who did the I/O, on what, and in which context. Combinations that make no sense are omitted, and cells for operations a backend type never performs are NULL; the background writer, for instance, never reads. | Key column | Values | |---|---| | `backend_type` | the same names as `pg_stat_activity.backend_type`: `client backend`, `checkpointer`, `background writer`, `autovacuum worker`, `walwriter` and others | | `object` | `relation` (permanent tables and indexes), `temp relation`, `wal` | | `context` | `normal` (through shared buffers), `init` (creating WAL segments), `vacuum`, `bulkread`, `bulkwrite` | The `vacuum`, `bulkread` and `bulkwrite` contexts are the **ring buffers**: a large sequential scan, a `COPY` or a vacuum works through a small, size-limited set of buffers so it does not flush the whole cache. ## The columns that matter - `reads`, `writes`, `extends`, `hits`: operation counts. - `read_bytes`, `write_bytes`, `extend_bytes`: the same operations in bytes, new in 18. - `evictions`: a block written out or dropped to make room. In `normal` it means replacement in shared buffers; in the ring contexts it means a shared buffer was taken into a ring. - `reuses`: a ring buffer recycled inside a bulk operation, which is expected traffic. - `fsyncs`: tracked only in `normal`. - `read_time`, `write_time`, `extend_time`, `fsync_time`: milliseconds, non-zero only when `track_io_timing` is on for relations, or `track_wal_io_timing` for `wal` rows. - `stats_reset`: when the view was last zeroed. ## Diagnosing backend writes The manual's own tuning guidance for the view is a short list: 1. **High `evictions`** in `normal` can indicate shared buffers should be larger. 2. **Large `writes` by `client backend`** mean query processes are writing dirty pages themselves instead of relying on the checkpointer and background writer. That suggests a misconfiguration of shared buffers or of the checkpointer. 3. **Large `fsyncs` by `client backend`** point the same way: backends should rely on the checkpointer to make data durable. Read those against the auxiliary processes in the same view: ```sql SELECT backend_type, context, writes, write_bytes, evictions, fsyncs, stats_reset FROM pg_stat_io WHERE object = 'relation' AND backend_type IN ('client backend', 'background writer', 'checkpointer') ORDER BY backend_type, context; ``` If client backends account for a large share of the `normal` writes, the fix lies in shared buffer sizing, background writer and checkpoint settings, which the memory-and-I/O tuning and WAL-durability leaves own. This view tells you that the problem exists and who bears it. ## What changed in 17 and 18 | Release | Change | |---|---| | 17 | `buffers_backend` and `buffers_backend_fsync` were removed from `pg_stat_bgwriter` as redundant with `pg_stat_io`; checkpoint counters moved to `pg_stat_checkpointer` | | 18 | `op_bytes`, which always equalled the block size, was removed; `read_bytes`, `write_bytes` and `extend_bytes` replace it | | 18 | rows with `object = 'wal'` report WAL writes and fsyncs, including WAL receiver activity | | 18 | `track_wal_io_timing` now controls WAL timing in `pg_stat_io`; the `wal_write`, `wal_sync`, `wal_write_time` and `wal_sync_time` columns left `pg_stat_wal` | | 18 | `pg_stat_get_backend_io(pid)` returns the same columns for one backend, and `pg_stat_reset_backend_stats(pid)` clears them | Dashboards written for 16 or 17 that multiply `reads` by `op_bytes` break on 18 and should read `read_bytes` directly. ## Reading the WAL rows in 18 The `wal` object adds a second kind of write pressure to the same view: - **`context = 'init'`** counts I/O done while creating new WAL segment files. - **`context = 'normal'`** counts ordinary WAL writes and the fsyncs that make commits durable. - **Who writes.** Rows for `walwriter`, `client backend` and, on a standby, `walreceiver` show which processes do the writing. Backends write WAL themselves at commit when the WAL writer has not already done so, so client-backend WAL writes are normal; what matters is how their volume and timing change. - **Timing.** `write_time` and `fsync_time` in `wal` rows are filled only when `track_wal_io_timing` is on, which is off by default. Before 18 that setting fed columns of `pg_stat_wal` instead. Whether commits should wait for that fsync at all is a durability setting owned by the WAL-durability leaf; the view only shows what it costs. ## Limits - **Cluster-wide, not per table.** For which table caused the reads, use `pg_statio_user_tables`. - **Bypassing shared buffers.** Relation I/O that does not go through shared buffers, such as moving a table to another tablespace, is not tracked. - **Cumulative.** Counters run from `stats_reset`; `pg_stat_reset_shared('io')` zeroes this view only. Snapshot and diff instead of resetting when others watch the same numbers.

  • Why do large sequential scans show reuses in the bulkread context rather than evictions in normal?
    PostgreSQL reads big tables through a small ring of buffers so one scan cannot push the whole working set out of shared buffers. Recycling a ring slot counts as a `reuse` in `bulkread`; taking a shared buffer into the ring counts as an `eviction` there. High `normal` evictions are the signal of real cache pressure.
  • How do you see the I/O of a single suspicious session in PostgreSQL 18?
    Call `pg_stat_get_backend_io(pid)` with the process ID from `pg_stat_activity`. It returns rows with exactly the columns of `pg_stat_io`, for that backend only. It does not cover the checkpointer, background writer, startup process or autovacuum launcher, which already appear once each in the view. `pg_stat_reset_backend_stats(pid)` clears those figures.

  • pg_stat_io breaks I/O down per table, so it shows which table is read most.
  • pg_stat_bgwriter's buffers_backend column still shows backend writes in PostgreSQL 18.
  • A high reuses count in bulkread means shared_buffers is too small.
  • pg_stat_io's read_time is populated by default with no setting turned on.
  • In PostgreSQL 18 you get bytes by multiplying reads by the op_bytes column.

Assumes PostgreSQL 18: pg_stat_io reports read_bytes, write_bytes and extend_bytes instead of op_bytes and adds object 'wal' rows; 17 had already moved buffers_backend out of pg_stat_bgwriter.