Video in progress
Write latency spikes every few minutes. How do you check whether it's related to checkpoint and WAL settings?
If max_wal_size is small, checkpoints happen often, full page writes bloat the WAL, and IO waves appear. I confirm it with log_checkpoints, raise max_wal_size, and smooth out the writes.
📖 Answer
First I confirm that the spikes line up with checkpoints. log_checkpoints = on (the default in PG 15+) writes the start, the reason and the duration of every checkpoint to the log:
LOG: checkpoint starting: wal
LOG: checkpoint complete: wrote 412340 buffers (39.3%); ... total=48.210 s
starting: wal is a forced checkpoint because max_wal_size filled up. starting: time is the scheduled one, per checkpoint_timeout. If you see a lot of the first kind, the WAL limit is too small.
Why this affects latency:
- After a checkpoint, the first change to each page writes the whole 8 KB page into the WAL (a full page write). If checkpoints are frequent, the WAL volume grows several times over, which in turn leads to even faster checkpoints.
- When many dirty pages are written to disk in a short time, the IO queue fills up and commits (the WAL flush) wait too.
The tuning:
max_wal_size = 16GB # default 1GB, too small for a write-heavy system
checkpoint_timeout = 15min
checkpoint_completion_target = 0.9
wal_compression = lz4 # PG 15+, reduces the FPW volume
The trade-off: a big max_wal_size and a long interval increase recovery time after a crash, since more WAL is replayed. It also needs that much space on disk.
If the checkpoints are normal and the spikes continue, I check other causes: autovacuum hitting a big table, the GIN pending list being flushed, a cloud disk's burst credits running out. pg_stat_io (PG 16+) shows which process is writing how much.
Sign up to mark studied questions and save your progress.