DBAtlas · Database operations atlas

Review checkpoint behavior (PG17+ pg_stat_checkpointer, PG16 and earlier pg_stat_bgwriter)

PostgreSQLParameters

Command

-- PG17+
SELECT num_timed,
       num_requested,
       round(100.0 * num_requested / NULLIF(num_timed + num_requested, 0), 1) AS req_pct,
       restartpoints_timed,
       restartpoints_req,
       round((write_time / 1000)::numeric / NULLIF(num_timed + num_requested, 0), 1) AS avg_write_s,
       round((sync_time / 1000)::numeric / NULLIF(num_timed + num_requested, 0), 1) AS avg_sync_s,
       pg_size_pretty(buffers_written * current_setting('block_size')::bigint) AS written_by_checkpoint,
       stats_reset
FROM pg_stat_checkpointer;

-- PG16 and earlier
SELECT checkpoints_timed,
       checkpoints_req,
       round(100.0 * checkpoints_req / NULLIF(checkpoints_timed + checkpoints_req, 0), 1) AS req_pct,
       round((checkpoint_write_time / 1000)::numeric / NULLIF(checkpoints_timed + checkpoints_req, 0), 1) AS avg_write_s,
       round((checkpoint_sync_time / 1000)::numeric / NULLIF(checkpoints_timed + checkpoints_req, 0), 1) AS avg_sync_s,
       pg_size_pretty(buffers_checkpoint * current_setting('block_size')::bigint) AS written_by_checkpoint,
       pg_size_pretty(buffers_clean * current_setting('block_size')::bigint) AS written_by_bgwriter,
       pg_size_pretty(buffers_backend * current_setting('block_size')::bigint) AS written_by_backends,
       maxwritten_clean,
       stats_reset
FROM pg_stat_bgwriter;

-- Related settings
SELECT name, current_setting(name) AS value
FROM pg_settings
WHERE name IN ('checkpoint_timeout', 'max_wal_size', 'min_wal_size',
               'checkpoint_completion_target', 'log_checkpoints')
ORDER BY name;

What it does

Shows the ratio of scheduled checkpoints to checkpoints forced by WAL volume, average write/sync time per checkpoint and the volume of buffers written (on PG16 and earlier, together with bgwriter and backend writes).

When to use it

When write-heavy periods show I/O spikes, WAL generation is high, or the log warns 'checkpoints are occurring too frequently'.

Caution

A high req_pct (typically above 10-20%) means checkpoints are forced because max_wal_size fills up; increase max_wal_size and watch for 'checkpoint starting: wal' lines with log_checkpoints = on. A longer checkpoint_timeout spreads I/O but lengthens crash recovery. On PG16 and earlier, a high written_by_backends means backends must write dirty pages themselves (shared_buffers/bgwriter insufficient); on PG17 this is in pg_stat_io. Averages also count scheduled checkpoints skipped for lack of work, so read them as rough figures. Counters are cumulative since stats_reset.

Open in DBAtlas →

Related