dataiotech Labs
dbaguidedbaguideDBAdataxtoolsdataopsphere
Scripts Library/postgresql/#15
Script #15
postgresql
Monitoringread-onlyMin Version: 9.x

PostgreSQL Checkpoint and Background Writer Stats

Shows scheduled versus requested checkpoint counts. Used to judge whether max_wal_size is adequate.

#checkpoint#bgwriter#io
postgresql · sql
-- PostgreSQL 16 ve öncesi
SELECT checkpoints_timed, checkpoints_req,
       round(100.0 * checkpoints_req / NULLIF(checkpoints_timed + checkpoints_req, 0), 1) AS req_pct,
       buffers_checkpoint, buffers_clean, buffers_backend, stats_reset
FROM pg_stat_bgwriter;

-- PostgreSQL 17+
SELECT num_timed, num_requested, write_time, sync_time, buffers_written, stats_reset
FROM pg_stat_checkpointer;
Operational Notes & Remediation Guidance

If requested checkpoints exceed scheduled checkpoints, increase `max_wal_size` and adjust `checkpoint_completion_target`.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 9.x).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.