Sub topic 6
What a Checkpoint Does
This chapter assumes you know basic SQL and psql, the ACID properties, and that PostgreSQL stores table and index data in fixed-size pages (8 kB by default). It also assumes you have read the previous chapters on the write-ahead rule, WAL records and LSNs, and commit-time WAL flushing.
The problem a checkpoint solves
Commit only forces WAL to disk. The changed data pages stay in shared buffers as "dirty" pages and reach the data files later. That is fast, but it leaves two problems:
- Recovery time. If the server crashes, it must replay WAL to rebuild every change that never reached the data files. Without a limit, it would have to replay from the beginning of time.
- WAL retention. PostgreSQL can only delete a WAL segment once nothing needs it for recovery. If dirty pages from old changes might still be missing from disk, the old WAL must be kept.
A checkpoint addresses both. It is a point in the WAL stream at which PostgreSQL guarantees that every data page modified before that point has been written to the data files. After that, crash recovery never needs WAL older than the checkpoint's starting position.
What a checkpoint does, step by step
The checkpointer is a dedicated background process. When a checkpoint starts, it does roughly this:
- Determine the redo point. This is the current WAL insert position (an LSN) at the moment the checkpoint begins. Crash recovery will start replaying from here.
- Write all dirty pages in shared buffers to the operating system. Pages dirtied after the checkpoint began are not required to be included, because their changes are in WAL after the redo point.
- Ask the operating system to flush those writes to disk (
fsyncon the affected files). - Write a checkpoint record into the WAL, and flush WAL through that record.
- Update the
pg_controlfile (inglobal/pg_control) so it points at the new checkpoint record. This update is done last, on purpose. - Recycle or remove WAL segments that are older than the redo point and no longer needed.
The ordering matters. pg_control is only updated after every dirty page is safely on disk. If the server crashes in the middle of a checkpoint, pg_control still points at the previous completed checkpoint, so recovery starts from there. A half-finished checkpoint is never trusted.
The redo point is not the checkpoint record
Beginners often assume a checkpoint happens at a single instant. It does not. A spread-out checkpoint can take minutes, and normal transactions keep running while it works. So there are two LSNs:
- The redo LSN is where the checkpoint began. Recovery starts replaying here.
- The checkpoint LSN is where the checkpoint record was written, after all the writing finished.
The redo LSN is always less than or equal to the checkpoint LSN. The gap between them is the WAL generated while the checkpoint was running. You can see both values:
SELECT checkpoint_lsn, redo_lsn, redo_wal_file, timeline_id, checkpoint_time
FROM pg_control_checkpoint();pg_control_checkpoint() reads the control file, so it reports the last completed checkpoint. On an idle test server the two LSNs may be close together. On a busy server with a long checkpoint they can differ by hundreds of megabytes.
Forcing a checkpoint and watching the redo point move
You can request a checkpoint manually. This needs superuser rights or membership in the pg_checkpoint role (PostgreSQL 15 and later).
CHECKPOINT;
SELECT redo_lsn, checkpoint_time FROM pg_control_checkpoint();Run the SELECT once before and once after the CHECKPOINT. The redo_lsn and checkpoint_time move forward. A manual CHECKPOINT runs at full speed and does not spread its writes, so on a large buffer cache it can cause a burst of I/O. Avoid running it casually on a busy production system.
Why checkpoints bound recovery
After a crash, PostgreSQL reads pg_control, finds the last completed checkpoint, and replays WAL from its redo LSN to the end of the log. The amount of WAL to replay is therefore at most the WAL written since the checkpoint began, plus any WAL written while the crash was developing. Roughly:
- Longer gaps between checkpoints mean more WAL to replay and a longer startup after a crash.
- Shorter gaps mean less to replay, but more write I/O during normal operation.
Replay is also not free of cost: it reads WAL and the data pages it touches, so the replay duration depends on how much WAL there is and on how many distinct pages must be read. The following chapters on crash recovery and tuning return to this.
A checkpoint also resets the full-page-write clock
After a checkpoint, the first change to any page writes a copy of the whole page into WAL, when full_page_writes is on. This protects against torn pages, where a crash during an 8 kB write leaves half old and half new data. The cost shows up as a jump in WAL volume right after each checkpoint, which then falls as more pages have already been copied. This is why checkpoint frequency affects WAL size, and not only I/O. The detail belongs to the next chapters.
What a checkpoint costs
A checkpoint does real work, and the work lands in a few places:
| Cost | Where it appears |
|---|---|
| Writing dirty pages | Disk write bandwidth, spread out or in a burst |
| `fsync` of data files | Latency spikes if the storage is slow to flush |
| Extra WAL from full-page images | Larger WAL volume right after the checkpoint |
| Reading `pg_control` and WAL at startup | Crash recovery duration |
Pages that were already written by the background writer or by a backend that needed to evict them are no longer dirty, so the checkpointer has less to do. The checkpointer does not write pages that are clean.
Checkpoint types and the `CHECKPOINT` flags
Not every checkpoint is the same kind. PostgreSQL labels them with flags, and these appear in the server log when log_checkpoints is on:
- time: the
checkpoint_timeoutinterval expired. - wal: WAL growth reached
max_wal_size(shown asxlogin old versions). - immediate: the checkpoint skips throttling. Used by
CHECKPOINT, shutdown, and some other commands. - force: a checkpoint is forced even if nothing has changed, for example during
CREATE DATABASEorpg_basebackupstart. - end-of-recovery and shutdown: special checkpoints at the end of crash recovery and during clean shutdown.
A clean shutdown writes a shutdown checkpoint, so the next start finds the database already consistent and needs no replay. The next chapter covers what triggers a checkpoint in more detail.
Seeing checkpoints in the log
Turn on logging so each checkpoint reports what it did. Both settings take effect after a configuration reload, without a restart:
ALTER SYSTEM SET log_checkpoints = on;
ALTER SYSTEM SET max_wal_size = '4GB';
SELECT pg_reload_conf();With log_checkpoints on (it is on by default from PostgreSQL 15), each checkpoint logs a "starting" line with its trigger flags and a "complete" line with the number of buffers written, the write and sync times, the total time, and the distance between checkpoints. Use these lines to answer practical questions: how many buffers does each checkpoint write, and how long does it take?
You can also decode the checkpoint record itself with pg_waldump. Replace the LSN with one from your own server, for example the checkpoint_lsn you queried above:
pg_waldump -p "$PGDATA/pg_wal" -s 0/1000028 -n 10A checkpoint appears as an XLOG record named CHECKPOINT_ONLINE (a running server) or CHECKPOINT_SHUTDOWN. It contains the redo LSN and the next transaction ID, among other state.
Common mistakes
- Assuming COMMIT triggers a checkpoint. It does not. Commit flushes WAL only. Data pages wait for the checkpointer, the background writer, or eviction.
- Reading
checkpoint_lsnas the recovery start. Recovery starts atredo_lsn. - Running
CHECKPOINTto "make data safe". Committed data is already safe in WAL. A manual checkpoint only shortens the next crash recovery and can hurt performance. - Expecting a checkpoint to empty
pg_wal. It makes old segments eligible for recycling or removal. Replication slots, archiving, andwal_keep_sizecan still keep them. - Reading the checkpoint time as an instant. The
checkpoint_timevalue reflects when the checkpoint record was written, which is after the writes finished.
Summary
A checkpoint writes every page dirtied before its redo point to the data files, flushes them, records a checkpoint record in WAL, and then updates pg_control. This bounds crash recovery to the WAL after the redo LSN and lets PostgreSQL recycle older WAL. The price is write I/O and extra full-page images after each checkpoint, which is why how often checkpoints run is a tuning decision.
Exercise
A server crashes. pg_control_checkpoint() showed redo_lsn = 0/5000000 and checkpoint_lsn = 0/5400000 before the crash. At which LSN does crash recovery start replaying WAL, and why is it not the other value?
Show answer
Recovery starts at the redo LSN, 0/5000000. The redo LSN is the WAL position when the checkpoint began. Changes made while the checkpoint was running (between 0/5000000 and the checkpoint record at 0/5400000) may be in pages the checkpointer did not write, so they must be replayed. The checkpoint LSN is only where the checkpoint record was written, after the page writing finished.
Exercise
Why does PostgreSQL update pg_control as the last step of a checkpoint rather than the first?
Show answer
If the server crashed mid-checkpoint after pg_control was updated, recovery would start from a redo point whose dirty pages might not all be on disk, and changes could be lost. Updating pg_control only after all pages are flushed and the checkpoint record is in WAL means it always points to a fully completed checkpoint. A crash during a checkpoint falls back to the previous one.
Exercise
Run CHECKPOINT; on a test server, then query pg_control_checkpoint(). Which two columns change, and what does the change tell you?
Show answer
redo_lsn and checkpoint_time move forward (and checkpoint_lsn and redo_wal_file usually do too). This shows that a new checkpoint completed and that crash recovery would now begin from a later WAL position.