A PostgreSQL 18 payments ledger shows write-latency spikes every few minutes; which log lines and pg_stat_checkpointer and pg_stat_wal counters show checkpoints cause them, and what do you change?
answer
- log_checkpoints is on by default
- starting: wal versus starting: time
- requested far above timed
- full-page images after each checkpoint
basics
~10 sMatch the spikes to log_checkpoints lines: 'checkpoint starting: wal' and 'checkpoints are occurring too frequently' mean max_wal_size is too small. Confirm with num_requested above num_timed and a wal_fpi surge, then raise max_wal_size.
solid answer
~40 s`log_checkpoints` is on by default, so first line the spikes up with `checkpoint starting:` entries: `wal` means WAL volume forced it, `time` means `checkpoint_timeout` did, and `checkpoints are occurring too frequently (N seconds apart)` with a hint to raise `max_wal_size` settles it. `checkpoint complete:` shows buffers written and the write and sync seconds. In `pg_stat_checkpointer` (17+, with `num_done` new in 18), `num_requested` far above `num_timed` says the same; `pg_stat_wal.wal_fpi` against `wal_records` shows the **full-page-image** surge each checkpoint causes. The fix is to raise `max_wal_size` until checkpoints become timed, consider a longer `checkpoint_timeout`, keep `checkpoint_completion_target` at 0.9, and shrink images with `wal_compression`. Never switch off `full_page_writes`.
code
sql · 8 linesSELECT num_timed, num_requested, num_done,
round(write_time / 1000) AS write_s,
round(sync_time / 1000) AS sync_s,
buffers_written, stats_reset
FROM pg_stat_checkpointer;
SELECT wal_records, wal_fpi, pg_size_pretty(wal_bytes) AS wal
FROM pg_stat_wal;go deeper
Know that checkpoints write dirty pages in bursts and that the server log records each one when log_checkpoints is on.
Read a 'checkpoint starting' and 'checkpoint complete' pair, and explain why WAL-triggered checkpoints and full-page images feed each other.
Diagnose from logs, pg_stat_checkpointer and pg_stat_wal, size max_wal_size from measured WAL, and reject full_page_writes = off.
Weigh checkpoint spacing against the recovery time and disk you can afford, and set alerts on requested-versus-timed ratios so regressions are caught.
## Confirm the spikes line up with checkpoints On a payments ledger that updates the same account and entry pages all day, a latency spike every few minutes is the classic checkpoint signature, but prove it before tuning. Since `log_checkpoints` defaults to `on`, the server log already records each one: ```console LOG: checkpoints are occurring too frequently (24 seconds apart) HINT: Consider increasing the configuration parameter "max_wal_size". LOG: checkpoint starting: wal LOG: checkpoint complete: wrote 61234 buffers (46.7%), wrote 12 SLRU buffers; 0 WAL file(s) added, 0 removed, 34 recycled; write=19.208 s, sync=2.913 s, total=22.512 s; sync files=212, longest=0.611 s, average=0.014 s; distance=551102 kB, estimate=552960 kB; lsn=2A/9C000108, redo lsn=2A/5B0002A8 ``` How to read it: - **`checkpoint starting: wal`** - WAL volume forced this checkpoint. `time` would mean `checkpoint_timeout` did. Manual ones show `immediate force wait`. - **`occurring too frequently`** is logged only for WAL-caused checkpoints that come closer together than `checkpoint_warning` (30 s by default), and its hint names `max_wal_size` directly. - **`distance=551102 kB`** - about 540 MB of WAL between checkpoints. With the default `max_wal_size = 1GB` and `checkpoint_completion_target = 0.9`, the WAL trigger sits at roughly 1 GB / 1.9, which is exactly this. - **`write=` and `sync=`** - a long write phase is the spread writing; a long sync phase points at the storage and OS writeback. The rest of the `checkpoint complete:` line is worth reading field by field: | Field | What it tells you | |---|---| | `wrote N buffers (x%)` | dirty shared buffers written, as a share of `shared_buffers` | | `wrote N SLRU buffers` | commit-log and similar SLRU pages, reported separately since 18 | | `WAL file(s) added, removed, recycled` | how `pg_wal` changed; many `added` means segments are being created under pressure | | `write=`, `sync=`, `total=` | seconds spent writing, fsyncing and overall | | `sync files`, `longest`, `average` | how many files were fsynced and the slowest one: a long `longest` points at one slow device or file | | `distance`, `estimate` | WAL since the previous checkpoint and the moving estimate used for recycling | | `lsn`, `redo lsn` | the checkpoint record's position and the redo point recovery would start from | ## Read `pg_stat_checkpointer` Since PostgreSQL 17 the checkpoint counters live in **`pg_stat_checkpointer`**, not `pg_stat_bgwriter`; 18 added `num_done` (completed checkpoints) and `slru_written`, and documents that `num_timed` and `num_requested` count skipped checkpoints as well as completed ones. ```sql SELECT num_timed, num_requested, num_done, round(write_time / 1000) AS write_s, round(sync_time / 1000) AS sync_s, buffers_written, stats_reset FROM pg_stat_checkpointer; ``` `num_requested` far above `num_timed` means most checkpoints are driven by WAL volume (or by explicit `CHECKPOINT` calls - check for a script issuing them). `write_time` and `sync_time` are in milliseconds. ## See the full-page-image surge in `pg_stat_wal` With `full_page_writes = on`, the first change to each page after a checkpoint logs the whole 8 kB page. On a ledger touching many pages, WAL volume jumps right after every checkpoint starts, which shortens the next WAL-driven interval: a feedback loop. `pg_stat_wal` shows it: ```sql SELECT wal_records, wal_fpi, pg_size_pretty(wal_bytes) AS wal, stats_reset FROM pg_stat_wal; ``` A high `wal_fpi` relative to `wal_records` means images dominate WAL. In 18 the `wal_write`, `wal_sync` and timing columns are gone from this view; WAL I/O timing moved to `pg_stat_io`. ## The fixes, in order 1. **Raise `max_wal_size`** so checkpoints become time-driven. Measure WAL per minute (LSN difference over time), multiply by the `checkpoint_timeout` you want, multiply by about 1.9 for the spread checkpoint, and add headroom. The log above shows about 540 MB every 24 s, roughly 22 MB/s; for a 10-minute interval that is about 13 GB of WAL, so `max_wal_size` near 25 GB. 2. **Consider a longer `checkpoint_timeout`** (10-30 min) if crash-recovery time allows: fewer checkpoints, fewer images. 3. **Keep `checkpoint_completion_target = 0.9`** so writes are spread across the interval. 4. **Shrink images with `wal_compression`** (`lz4` or `zstd` where the build supports them, `pglz` otherwise), trading CPU for WAL bytes. 5. **If `sync=` stays long,** look at storage and at `checkpoint_flush_after` (256 kB on Linux), which asks the OS to write back in smaller chunks instead of one large flush at the end. All of these are reload-level settings. Re-check with the log lines and `pg_stat_checkpointer` after a day: `checkpoint starting: time` should dominate. ## What not to do - **Do not set `full_page_writes = off`.** It removes torn-page protection; an OS crash can then corrupt data beyond repair. - **Do not lower `checkpoint_completion_target`** to get checkpoints 'out of the way': that concentrates the same writes into a sharper burst. - **Do not shrink `checkpoint_timeout`** to make each checkpoint smaller: more checkpoints means more full-page images and more total I/O.
- num_requested is high, but the log shows 'checkpoint starting: immediate force wait' rather than 'wal'. What is going on?Something is issuing explicit `CHECKPOINT` commands - a maintenance script, a backup tool asking for a fast checkpoint, or a monitoring job. Those count as requested and are not spread, so each one is a burst. Find the caller and remove or reschedule it; `max_wal_size` will not help.
- After raising max_wal_size the spikes are smaller but still there every 15 minutes. What next?Check the `sync=` time in `checkpoint complete:` lines. If the write phase is spread but sync is long, the OS is holding many dirty pages until the final fsync; `checkpoint_flush_after` and the storage's write capacity are the next levers, along with `wal_compression` to cut the image surge.
- Turn off full_page_writes to remove the WAL surge after checkpoints.
- On PostgreSQL 18, checkpoint counts are read from pg_stat_bgwriter.
- Lower checkpoint_completion_target so checkpoints finish faster and get out of the way.
- Set checkpoint_timeout to one minute so each checkpoint has less to write.
- A checkpoint every few minutes proves shared_buffers is too large.
Assumes PostgreSQL 18: pg_stat_checkpointer exists since 17 (num_done and slru_written since 18), and 18 moved WAL write and sync timing out of pg_stat_wal into pg_stat_io.