Sub topic 13
Tuning Checkpoint and WAL Settings
Assume you know basic SQL and psql, understand transactions and ACID properties, and know that PostgreSQL stores data in fixed-size pages. You should also be comfortable reading configuration parameters.
Checkpoint tuning balances three costs:
- WAL storage and retention.
- Checkpoint I/O, which can compete with queries.
- Crash-recovery time after an unclean shutdown.
A larger WAL budget usually means fewer checkpoints and less repeated page writing. It also means more WAL may need replaying after a crash.
The main settings
| Setting | Controls | Tuning effect |
|---|---|---|
| `checkpoint_timeout` | Maximum time between checkpoints | A larger value permits longer intervals |
| `max_wal_size` | Approximate WAL volume allowed before a checkpoint | A larger value usually reduces checkpoint frequency |
| `min_wal_size` | WAL retained for reuse | A larger value reduces repeated file allocation |
| `checkpoint_completion_target` | Fraction of the interval used for checkpoint writes | A higher value spreads I/O more evenly |
| `full_page_writes` | Full-page images after checkpoints | Keep enabled for protection against torn pages |
| `synchronous_commit` | When a transaction reports commit success | Lower durability can reduce commit latency |
max_wal_size is a trigger, not a strict storage limit. PostgreSQL can exceed it temporarily, and other requirements such as replication slots or archiving can retain WAL independently.
A common starting strategy is to increase max_wal_size until checkpoints are not frequently requested by WAL volume, then use checkpoint_timeout to bound the maximum interval. Do not choose values only from a benchmark with an empty cache. Measure the production workload.
SHOW checkpoint_timeout;
SHOW max_wal_size;
SHOW min_wal_size;
SHOW checkpoint_completion_target;
SHOW full_page_writes;
SHOW synchronous_commit;Read the checkpoint symptoms
PostgreSQL records whether checkpoints happened because of the timer or because a request occurred first. In PostgreSQL 17 and later, query pg_stat_checkpointer:
SELECT num_timed,
num_requested,
write_time,
sync_time,
buffers_written
FROM pg_stat_checkpointer;On PostgreSQL 16 and earlier, use pg_stat_bgwriter:
SELECT checkpoints_timed,
checkpoints_req,
checkpoint_write_time,
checkpoint_sync_time,
buffers_checkpoint
FROM pg_stat_bgwriter;A high requested-to-timed checkpoint ratio suggests that WAL reaches max_wal_size before checkpoint_timeout. Increasing max_wal_size is often the first change to test. If checkpoint writes cause latency spikes, increase checkpoint_completion_target or reduce the burst of write activity. Checkpoint spreading cannot remove the total I/O; it changes when that I/O occurs.
Enable checkpoint logging so you can correlate checkpoints with workload events:
ALTER SYSTEM SET log_checkpoints = on;
SELECT pg_reload_conf();Some parameters, including checkpoint_timeout, max_wal_size, and checkpoint_completion_target, can also be changed with a reload. Always check the parameter context and restart requirement for your PostgreSQL version.
Choose settings from observed behavior
Use these decision rules:
- Frequent requested checkpoints: test a larger
max_wal_size. - Long checkpoint synchronization times: investigate storage throughput and checkpoint I/O pressure.
- Query latency spikes during checkpoints: spread writes with
checkpoint_completion_target, and verify that the storage system can sustain the average write rate. - Excessive WAL immediately after checkpoints: inspect
full_page_writes, page-update patterns, and checkpoint frequency. - Slow crash recovery: remember that larger WAL intervals can increase replay work. Test recovery time, not only normal throughput.
- Replication or archiving delays: check retained WAL before reducing checkpoint frequency.
Validate each change
Change one related setting at a time, record the old and new values, and compare:
- WAL generated per minute.
- Timed and requested checkpoint counts.
- Checkpoint write and sync time.
- Query latency during checkpoints.
- Recovery time in a test environment.
Exercise
Requested checkpoints are much more common than timed checkpoints, and storage latency is acceptable. What should you test first?
Show answer
Test a larger maxwalsize, then recheck checkpoint statistics, WAL retention, normal query latency, and crash-recovery time.