Sub topic 6
What a Checkpoint Does
This chapter assumes basic SQL and psql, transactions, ACID properties, database pages, and PostgreSQL configuration parameters.
A checkpoint is a PostgreSQL operation that makes the database's current in-memory state easier to recover after a crash. It writes changed data pages to disk and records a position in the write-ahead log (WAL). Crash recovery can then start from that position instead of replaying the entire WAL history.
The checkpoint process
PostgreSQL normally changes pages in shared memory first. These changed pages are called dirty pages. The database does not need to write every dirty page to disk when a transaction commits. It only needs to flush the relevant WAL records before reporting a durable commit.
A checkpoint later writes dirty pages to their data files. It also writes a checkpoint record to WAL and updates control information about the checkpoint.
The main steps are:
- PostgreSQL establishes a checkpoint position in WAL.
- It ensures the checkpoint record is safely written.
- It writes dirty data pages to the data files.
- It flushes the required files to storage.
- It records checkpoint metadata, including the redo position.
The redo position tells recovery where WAL replay should begin. WAL before that point describes changes already reflected in the data files, so replaying it is unnecessary for normal crash recovery.
A checkpoint does not commit transactions. It does not make an uncommitted transaction visible, and it does not replace the commit record required for transaction durability.
Checkpoint metadata
PostgreSQL exposes the latest checkpoint through pg_control_checkpoint(). The redo_lsn value is the WAL position where crash recovery would begin. The checkpoint_lsn value identifies the checkpoint record itself.
SELECT checkpoint_lsn,
redo_lsn,
redo_wal_file,
timeline_id,
checkpoint_time
FROM pg_control_checkpoint();The exact values change as checkpoints complete. You can request a checkpoint manually when testing:
CHECKPOINT;
SELECT redo_lsn, checkpoint_time
FROM pg_control_checkpoint();Running CHECKPOINT usually requires superuser access or the pg_checkpoint privilege. A manual checkpoint is useful for experiments, but frequent manual checkpoints can create unnecessary disk activity.
| Item | Purpose |
|---|---|
| Dirty data page | An in-memory page that differs from its disk copy |
| Checkpoint record | WAL metadata identifying a checkpoint |
| Redo LSN | WAL position where crash recovery begins |
| Checkpoint flush | Writing required changed pages and WAL to durable storage |
Why checkpoints matter
Checkpoints create a boundary for crash recovery. After an unclean shutdown, PostgreSQL reads the latest checkpoint information and replays WAL from the redo point. It reapplies changes that reached WAL but might not have reached the data files before the crash.
Checkpoints also affect performance. Writing many dirty pages at once can cause I/O spikes. PostgreSQL can spread checkpoint writes over time, reducing sudden bursts. A checkpoint that happens too often can increase write pressure and WAL overhead. One that happens too rarely can make recovery take longer and keep more WAL files.
Checkpoint timing is controlled mainly by checkpoint_timeout and max_wal_size. A checkpoint can occur when the timeout expires, when WAL usage reaches the configured limit, or when another operation requests one.
Exercise
After a crash, why does PostgreSQL replay WAL from the checkpoint's redo LSN instead of from the beginning of the WAL stream?
Show answer
Data pages before the redo LSN were covered by the completed checkpoint, so recovery only needs to replay later WAL records that may not yet be reflected on disk.