Sub topic 13
Tuning checkpoint and WAL settings
This chapter assumes you have read the earlier chapters on the write-ahead rule, LSNs, checkpoints and crash recovery. You should be comfortable with psql, ALTER SYSTEM and reading postgresql.conf. Tuning here means picking values from evidence, so the chapter first shows what to measure and then which setting to move.
What tuning trades against what
Checkpoint and WAL settings balance four things. Each setting moves more than one of them.
| Goal | Pushed up by | Cost |
|---|---|---|
| Fewer full-page images and less WAL | Longer gaps between checkpoints | Longer crash recovery, more disk used by `pg_wal` |
| Short crash recovery | Frequent checkpoints | More WAL, more write I/O bursts |
| Smooth I/O | Higher `checkpoint_completion_target` | Dirty pages stay in cache longer |
| Fast commits | `synchronous_commit = on` with fast WAL storage | Commits wait for the WAL flush |
The main trade is recovery time against steady-state I/O. A checkpoint is a bounded amount of work, because it flushes the dirty buffers that existed when it began. Spreading that work over a longer interval and running it less often both lower the load. The price is that after a crash, PostgreSQL replays more WAL from the redo point.
Step 1: Record the current settings
Start by writing down what the server uses today. Defaults differ between versions. For example, checkpoint_completion_target was 0.5 before PostgreSQL 14 and is 0.9 since.
SHOW checkpoint_timeout;
SHOW max_wal_size;
SHOW min_wal_size;
SHOW checkpoint_completion_target;
SHOW full_page_writes;
SHOW synchronous_commit;Typical defaults are checkpoint_timeout = 5min, max_wal_size = 1GB and min_wal_size = 80MB. These suit a small server and are usually too low for a busy one.
Step 2: Find out why checkpoints happen
A checkpoint starts for one of two main reasons. The timer checkpoint_timeout expires (a timed checkpoint), or WAL growth approaches max_wal_size (a requested checkpoint). Requested checkpoints also come from commands such as CHECKPOINT, pg_basebackup, CREATE DATABASE and shutdown, so a few are normal.
-- PostgreSQL 17 and later
SELECT num_timed, num_requested, write_time, sync_time, buffers_written
FROM pg_stat_checkpointer;
-- PostgreSQL 16 and earlier
SELECT checkpoints_timed, checkpoints_req, checkpoint_write_time,
checkpoint_sync_time, buffers_checkpoint
FROM pg_stat_bgwriter;These counters accumulate since the last stats reset. Old totals can hide a recent change. Reset them before an experiment with SELECT pg_stat_reset_shared('checkpointer'); on 17 and later, or 'bgwriter' on 16 and earlier.
A healthy server has mostly timed checkpoints. If requested checkpoints are a large share, WAL fills max_wal_size before the timer fires. Each early checkpoint also resets the full-page-write cycle sooner, so WAL volume rises further.
Checkpoint logging shows the same thing for each event. Turn it on (it is on by default from PostgreSQL 15) and read the reason in the checkpoint starting: line.
ALTER SYSTEM SET log_checkpoints = on;
SELECT pg_reload_conf();A line ending in time means the timer fired. A line ending in wal means max_wal_size forced it. PostgreSQL also writes a warning when checkpoints caused by WAL volume come closer together than checkpoint_warning (30 seconds by default).
Step 3: Measure WAL volume
To size max_wal_size you need to know how much WAL your workload writes during one checkpoint interval. Measure it during busy hours with two LSN readings.
SELECT pg_current_wal_lsn() AS start_lsn \gset
SELECT pg_sleep(300);
SELECT pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), :'start_lsn')) AS wal_in_5_min;pg_stat_wal (PostgreSQL 14 and later) gives cumulative totals and shows how much of the volume is full-page images.
SELECT wal_records, wal_fpi, pg_size_pretty(wal_bytes) AS wal_bytes, stats_reset
FROM pg_stat_wal;A wal_fpi count that is large compared with wal_records means checkpoints are frequent relative to how many pages your workload touches. Lengthening the checkpoint interval is the main fix.
Step 4: Set `max_wal_size`
max_wal_size is a soft limit. The server tries to start a checkpoint before WAL reaches it, but it can be exceeded under heavy load, with a high wal_keep_size, or while a replication slot holds back old files.
A practical method:
- Measure WAL generated per
checkpoint_timeoutinterval at peak, as in Step 3. - Set
max_wal_sizeto at least two to three times that figure. WAL from the previous checkpoint cycle is still on disk while the next one runs. The extra room also covers bursts such as bulk loads and large index builds. - Confirm that the disk holding
pg_walcan hold that size, plus archive or slot backlog.
For example, if peak load writes 2 GB per 15 minutes, a max_wal_size of 6 GB to 8 GB is a reasonable starting point. If requested checkpoints are still frequent, raise it further.
min_wal_size sets how many recycled segment files PostgreSQL keeps for reuse instead of deleting them. If WAL use swings widely, such as nightly batch jobs, raising it avoids repeatedly creating and removing files. The default is usually fine otherwise.
Step 5: Choose `checkpoint_timeout`
The allowed range is 30 seconds to 1 day. Once max_wal_size is large enough that the timer is the usual trigger, checkpoint_timeout sets the checkpoint interval and, indirectly, the worst-case amount of WAL to replay.
- 5 minutes (the default) keeps recovery short but produces many full-page images.
- 15 to 30 minutes is a common choice for write-heavy servers. It cuts full-page writes noticeably.
- Values of an hour or more are rarely worth it. Recovery becomes slower and the gain from fewer full-page images shrinks.
Crash recovery time depends on the WAL between the redo point and the crash, and on how fast it replays. It does not depend directly on the timeout. As a rough guide, replay is often faster than the original write rate, but test your own hardware. Time a recovery in a staging environment after a simulated crash (pg_ctl stop -m immediate) and check that it meets your availability target.
Step 6: Spread the writes
checkpoint_completion_target is the fraction of the checkpoint interval over which dirty buffers are written. At 0.9, a 15-minute checkpoint writes for about 13.5 minutes, which leaves a margin for the final sync phase. Values close to 1.0 leave too little margin. Values below 0.5 bring back the I/O spike the setting exists to prevent. For most servers, leave it at 0.9.
Checkpoint writes are paced against both the timer and WAL growth. If WAL is consumed faster than expected, the checkpoint speeds up to finish before max_wal_size is reached. That shows up as high write_time with a large num_requested. The fix is a bigger max_wal_size, not a change to the completion target.
Step 7: Apply changes
All of these parameters take effect on a configuration reload, with no restart.
ALTER SYSTEM SET checkpoint_timeout = '15min';
ALTER SYSTEM SET max_wal_size = '8GB';
ALTER SYSTEM SET checkpoint_completion_target = 0.9;
SELECT pg_reload_conf();Verify with SHOW. Then let the workload run for at least several checkpoint intervals before judging, and compare the new pg_stat_checkpointer numbers against your baseline.
Settings to leave alone, and settings to consider
synchronous_commit is the safe knob for commit speed. Setting it to off lets a commit return before its WAL is flushed. After a crash you can lose the last few transactions (up to about three times wal_writer_delay), but the database stays consistent. Set it per session or per transaction for low-value writes such as event logs, rather than server-wide.
SET synchronous_commit = off; -- this session onlyAnother option is wal_compression, which compresses full-page images. It trades CPU for less WAL volume, and it helps most when full-page images dominate pg_stat_wal. Test it against your CPU headroom.
Common mistakes
- Raising
max_wal_sizewithout raisingcheckpoint_timeoutand expecting fewer checkpoints. If the timer is already the trigger, nothing changes. - Sizing
max_wal_sizefor the average load instead of the peak. - Forgetting that replication slots and archive failures keep WAL on disk beyond
max_wal_size. - Judging changes after a few minutes. Several full checkpoint cycles are needed, and the first one after a reload still follows the old schedule.
- Reading cumulative counters without resetting them first.
Exercise
After a reload, pgstatcheckpointer shows numtimed = 40 and numrequested = 310. The log lines say "checkpoint starting: wal". Peak load writes about 3 GB of WAL per 15 minutes, and maxwalsize is 1GB. Which setting do you change first, and to roughly what value?
Show answer
Raise maxwalsize first, because WAL volume (not the timer) is triggering most checkpoints. Two to three times 3 GB gives 6 GB to 9 GB, so something like 8GB is a reasonable start. Check that the pgwal disk can hold it. Then reset the statistics, run through several checkpoint intervals, and confirm that numtimed now dominates. Raise checkpoint_timeout to 15min only if it is still 5min, so the timer becomes the intended trigger.
Exercise
Why is setting synchronous_commit = off safer than setting fsync = off?
Show answer
With synchronous_commit = off, WAL is still written and flushed in order, only slightly later. After a crash you lose at most the last few committed transactions, and the database stays consistent. With fsync = off, PostgreSQL does not force data to stable storage at all, so after a crash the files can be inconsistent or corrupt.