Difficulty: Advanced
What is a checkpoint in a database, why is it needed alongside WAL, and what are the trade-offs of checkpointing more or less often?
A checkpoint is the point at which the database writes all dirty (modified in memory) data pages to the data files and records that position in the WAL. After a checkpoint, crash recovery only needs to replay WAL generated since that point, and older WAL files can be recycled or archived. Checkpoints bound both recovery time and the disk space taken by the log.
With WAL, committed changes are safe in the log even if the data pages are still only in memory. Without checkpoints:
A checkpoint says: "Everything up to this LSN is now safely in the data files." That makes everything before it unnecessary for crash recovery.
fsync on the data files so the writes are durable.pg_control with its location.| Trigger | PostgreSQL setting / default |
|---|---|
| Time elapsed | checkpoint_timeout, default 5 minutes |
| WAL volume | max_wal_size, default 1 GB |
| Manual | CHECKPOINT command (superuser or pg_checkpoint role) |
| Shutdown, start of a base backup | Automatic |
checkpoint_completion_target (default 0.9) spreads the writes across 90% of the interval so a checkpoint does not cause a sudden I/O spike. InnoDB uses fuzzy checkpointing, continuously flushing pages so the redo log never fills up, and only does a full "sharp" checkpoint at shutdown.
| Checkpoint more often | Checkpoint less often |
|---|---|
| Faster crash recovery | Slower crash recovery |
| Less WAL kept on disk | More WAL on disk |
| More total writes: hot pages flushed repeatedly | Hot pages flushed once for many changes |
| More full-page images in WAL after each checkpoint | Fewer full-page images, less WAL volume |
The full-page image point surprises people: with full_page_writes = on, the first change to each page after a checkpoint logs the whole 8 KB page, so very frequent checkpoints can actually increase WAL volume.
max_wal_size is too small for the write load, so WAL-size triggered checkpoints fire before the timeout. Raising it usually fixes it.pg_stat_checkpointer view (earlier versions show them in pg_stat_bgwriter).SHOW checkpoint_timeout; -- 5min default
SHOW max_wal_size; -- 1GB default
SHOW checkpoint_completion_target; -- 0.9 default
-- Timed vs requested checkpoints (PG 17+)
SELECT num_timed, num_requested, buffers_written,
write_time, sync_time
FROM pg_stat_checkpointer;
-- Lots of num_requested relative to num_timed means
-- max_wal_size is too small for the write rate.
ALTER SYSTEM SET max_wal_size = '4GB';
SELECT pg_reload_conf();
If most checkpoints are requested (WAL-size triggered) rather than timed, the server is checkpointing more often than intended; raising max_wal_size spaces them out without a restart.
Checkpoint, Dirty Pages, WAL Replay, Recovery Time, max_wal_size