Sub topic 9
WAL and checkpoint tuning
Assume you can run basic SQL in psql, edit postgresql.conf, and read a simple EXPLAIN plan. This chapter focuses on write-ahead logging (WAL) and checkpoints. WAL records changes before PostgreSQL writes the changed data pages to the main data files. A checkpoint makes recovery faster by establishing a known point from which PostgreSQL can replay WAL after a crash.
How checkpoints affect I/O
PostgreSQL starts a checkpoint when the time limit expires or when generated WAL reaches the available checkpoint limit. During a checkpoint, dirty pages are written to disk. The server then recycles or removes WAL segments that are no longer needed.
A checkpoint can create a burst of disk writes if PostgreSQL tries to finish most of the work quickly. checkpoint_completion_target spreads those writes across the checkpoint interval. A higher value, commonly 0.9, reduces bursts but leaves less idle time before the next checkpoint.
checkpoint_timeout sets the maximum time between checkpoints. max_wal_size sets a target for how much WAL can accumulate before PostgreSQL requests an earlier checkpoint. max_wal_size is not a strict disk-space limit. Heavy write activity can temporarily use more WAL, and replication or backup requirements can retain additional segments.
| Setting | Main effect | Common starting point |
|---|---|---|
| `checkpoint_timeout` | Limits time between scheduled checkpoints | `15min` |
| `max_wal_size` | Allows more WAL before write pressure triggers a checkpoint | `4GB`, then measure |
| `checkpoint_completion_target` | Spreads checkpoint writes over the interval | `0.9` |
Configure and observe
A larger max_wal_size often reduces checkpoint frequency on a write-heavy system. The trade-off is more WAL to replay after an unexpected restart. A longer checkpoint_timeout has a similar trade-off. Choose values that keep checkpoint activity smooth while keeping recovery time acceptable.
Enable checkpoint logging and inspect the result:
# postgresql.conf
log_checkpoints = on
max_wal_size = 4GB
checkpoint_timeout = 15min
checkpoint_completion_target = 0.9Some settings can be reloaded, while others require a restart. Check the current context instead of guessing:
SELECT name, setting, unit, context, pending_restart
FROM pg_settings
WHERE name IN (
'max_wal_size',
'checkpoint_timeout',
'checkpoint_completion_target'
);After changing a reloadable setting, apply it and verify the value:
SELECT pg_reload_conf();
SHOW max_wal_size;
SHOW checkpoint_timeout;
SHOW checkpoint_completion_target;The server log reports checkpoint duration, the amount of WAL used, and how many buffers were written. Frequent checkpoints caused by WAL volume suggest increasing max_wal_size or improving the workload. Long checkpoints or high write latency may indicate that the storage device cannot sustain the write rate.
A safe tuning sequence
- Record checkpoint log messages and workload throughput.
- Change one setting, usually
max_wal_size, by a moderate amount. - Keep the workload comparable.
- Compare checkpoint frequency, duration, write latency, and recovery time.
- Revert the change if it improves one measure while harming an important requirement.
Exercise
A server checkpoints every few minutes during a bulk load, and the logs show that WAL volume triggers each checkpoint. Which setting is the first candidate, and what trade-off must you check?
Show answer
Increase maxwalsize moderately, then measure. Check that available disk space and crash-recovery time remain acceptable because more WAL may accumulate and require replay.