checkpoints
flushing dirty pages, the redo pointer, and the trade between recovery time and steady I/O.
- address
- everything.wal.checkpoints
- part
- part 16, WAL and recovery
- chapter
- chapter 5 of 11
- reading time
- 8 min
a checkpoint writes every dirty buffer to its file, fsyncs the files, and records the redo pointer: the WAL position where it started. after that, crash recovery never needs WAL older than the redo pointer, so those segments can be recycled. checkpoints happen every checkpoint_timeout (5 min) or every max_wal_size (1 GB) of WAL, whichever comes first, and the writes are spread over checkpoint_completion_target (90%) of the gap. rarer checkpoints mean smoother I/O and less WAL, and a longer recovery.
the picture
you know how crash recovery replays the WAL "from the last checkpoint"? the checkpoint is the thing that makes that a short trip instead of a replay of everything since the cluster was created.
think of a shop doing its month-end close. the day book has every sale (the WAL). at the close, the bookkeeper makes sure the ledgers match the day book up to today (writes every dirty page out), files them away (fsync), and writes "ledgers complete up to the 1st" at the top of a fresh page (the redo pointer). an auditor after a fire starts reading the day book from the 1st, not from the day the shop opened, and the old day books can go to the shredder (WAL recycling).
hover a stage.
what a checkpoint writes in the WAL
three records, captured on a real server:
| rmgrtext | record_typetext | lenint4 | descriptiontext |
|---|---|---|---|
| XLOG | CHECKPOINT_REDO | 30 | wal_level replica |
| Standby | RUNNING_XACTS | 50 | nextXid 759 latestCompletedXid 758 oldestRunnin… |
| XLOG | CHECKPOINT_ONLINE | 114 | redo 0/1620A38; tli 1; prev tli 1; fpw true; wa… |
CHECKPOINT_REDO is the redo pointer, written first, before any buffer is written (postgres 17 added this record; before that the redo pointer was just the insert position at the start). RUNNING_XACTS tells replicas which transactions were open. CHECKPOINT_ONLINE at the end carries the redo pointer and the counters a restart needs. the redo pointer is earlier than the checkpoint record because the checkpoint takes time: a page changed while it was running might have been written before the change or after, and replaying from the start covers both.
you can watch pg_control move. in the browser there's no checkpointer process: CHECKPOINT runs inside your own session, and the checkpointer's statistics stay at zero because nobody else exists to count them. pg_control still moves the same way:
UPDATE notes SET body = 'edited' WHERE id <= 100;
SELECT count(*) FILTER (WHERE isdirty) AS dirty_buffers FROM pg_buffercache;
SELECT redo_lsn, checkpoint_lsn FROM pg_control_checkpoint();
CHECKPOINT;
SELECT count(*) FILTER (WHERE isdirty) AS dirty_buffers FROM pg_buffercache;
SELECT redo_lsn, checkpoint_lsn FROM pg_control_checkpoint();
SELECT num_timed, num_requested, num_done, buffers_written FROM pg_stat_checkpointer;| dirty_buffersint8 |
|---|
| 19 |
| redo_lsnpg_lsn | checkpoint_lsnpg_lsn |
|---|---|
| 0/17DB800 | 0/17DB800 |
| dirty_buffersint8 |
|---|
| 0 |
| redo_lsnpg_lsn | checkpoint_lsnpg_lsn |
|---|---|
| 0/17F8F30 | 0/17F8F88 |
| num_timedint8 | num_requestedint8 | num_doneint8 | buffers_writtenint8 |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
CHECKPOINT;
SELECT redo_lsn, checkpoint_lsn FROM pg_control_checkpoint();
-- redo_lsn < checkpoint_lsnlog_checkpoints
log_checkpoints is on by default since postgres 15. here's what it printed for that CHECKPOINT on the capture server (pgtrace blanks the timings, which change every run):
- LOG: checkpoint starting: immediate force wait
- LOG: checkpoint complete: wrote 165 buffers (1.0%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 0 recycled; write=… s, sync=… s, total=… s; sync files=72, longest=… s, average=… s; distance=883 kB, estimate=883 kB; lsn=0/1620A90, redo lsn=0/1620A38
a CHECKPOINT you type is immediate: write everything as fast as possible. the ones the server starts by itself are not. this is a throwaway cluster on this mac with checkpoint_timeout = 30s, a freshly loaded 46 MB table, and nothing else to do:
LOG: checkpoint starting: time
LOG: checkpoint complete: wrote 5914 buffers (36.1%), wrote 3 SLRU buffers; 0 WAL file(s) added, 0 removed, 3 recycled; write=27.075 s, sync=0.002 s, total=27.078 s; sync files=29, longest=0.001 s, average=0.001 s; distance=51906 kB, estimate=51906 kB; lsn=0/4A39A18, redo lsn=0/4A399885,914 buffers, about 46 MB, that the disk could have taken in well under a second, and the checkpointer took 27.075 s: 90% of the 30 s interval, exactly what checkpoint_completion_target = 0.9 asks for. it writes a few buffers, checks whether it's ahead of schedule, and naps if it is. that's the point: a checkpoint competing with queries for the disk is a latency spike; one trickling along in the background is noise.
when they happen
a checkpoint starts when the first of these happens:
| trigger | default | log says |
|---|---|---|
checkpoint_timeout since the last one | 5 min | checkpoint starting: time |
max_wal_size of WAL since the last one (a soft limit, spread over the interval) | 1 GB | checkpoint starting: wal |
someone runs CHECKPOINT | immediate force wait | |
| shutdown, end of recovery, start of a base backup | shutdown immediate, end-of-recovery, ... |
with the defaults, a quiet server checkpoints every 5 minutes, and a busy one every time it writes about a gigabyte of WAL. here's the same cluster with max_wal_size shrunk to 32 MB, and two UPDATEs of every row of that 46 MB table:
LOG: checkpoints are occurring too frequently (11 seconds apart)
HINT: Consider increasing the configuration parameter "max_wal_size".
LOG: checkpoint starting: wal
LOG: checkpoint complete: wrote 1040 buffers (6.3%), wrote 0 SLRU buffers; 0 WAL file(s) added, 1 removed, 0 recycled; write=0.151 s, sync=0.008 s, total=0.169 s; sync files=2, longest=0.008 s, average=0.004 s; distance=10013 kB, estimate=47717 kB; lsn=0/6414680, redo lsn=0/5400FD0
LOG: checkpoints are occurring too frequently (0 seconds apart)
HINT: Consider increasing the configuration parameter "max_wal_size".
LOG: checkpoint starting: walthirteen checkpoints during two statements, each with no time to spread its writes (write=0.151 s), each resetting every page's "first change since a checkpoint" flag, so the next touch of every page logs a full-page image, which fills max_wal_size sooner, which starts the next checkpoint. the warning fires whenever WAL-triggered checkpoints come closer together than checkpoint_warning (30 s). it's postgres asking for a bigger max_wal_size.
the trade
| frequent checkpoints | rare checkpoints | |
|---|---|---|
| crash recovery | short: little WAL since the redo pointer | longer: up to max_wal_size of WAL to replay |
| steady-state writes | the same hot pages written again and again | a hot page is written once per checkpoint, however often it changed |
| WAL volume | more full-page images | fewer |
| disk space in pg_wal | less | up to about max_wal_size, plus what slots and archiving hold |
most servers want the right column: a longer checkpoint_timeout (15 to 30 minutes is common), a max_wal_size large enough that checkpoints are triggered by time, not by WAL, and the default completion target. what you give up is recovery time after a crash, which is replay of at most that much WAL. the checkpointer is one of three processes that write pages, with the background writer and backends themselves: writing pages back has all three.
keylines
- a checkpoint writes every dirty buffer, fsyncs, and records the redo pointer in pg_control.
- recovery starts at the redo pointer. WAL before it can be recycled.
- triggered by checkpoint_timeout (5 min) or max_wal_size (1 GB), whichever comes first.
- the writes are spread over checkpoint_completion_target (0.9) of the interval: 27 s of a 30 s timeout.
- "checkpoints are occurring too frequently" means raise max_wal_size.
- rarer checkpoints: less I/O, less WAL, longer crash recovery.