Sub topic 8
Crash Recovery: Replaying WAL from the Redo Point
This chapter assumes basic SQL and psql, familiarity with transactions and ACID properties, and a basic understanding of database pages and PostgreSQL configuration.
A PostgreSQL server can crash after a transaction changes memory but before the changed table pages reach disk. The data files may then contain an older version of a page. PostgreSQL restores consistency by replaying the write-ahead log (WAL) during startup.
What the redo point means
A checkpoint records a consistent relationship between the data files and WAL. It also stores a redo point, which is an LSN marking where crash recovery should begin replaying WAL.
LSN means log sequence number. It identifies a position in the WAL stream. The redo point is usually earlier than the newest WAL record because changes made after the checkpoint might exist in WAL but not yet in the data files.
The control file stores information about the latest checkpoint. You can inspect it with this query:
SELECT checkpoint_lsn,
redo_lsn,
redo_wal_file,
timeline_id,
checkpoint_time
FROM pg_control_checkpoint();checkpoint_lsnidentifies the checkpoint record.redo_lsnidentifies the start of the WAL range needed for recovery.redo_wal_filenames the WAL segment containing that point.timeline_ididentifies the server's history branch.
What happens after a crash
On an unclean shutdown, PostgreSQL detects that the previous server process did not complete a normal shutdown. During startup, it performs crash recovery:
- PostgreSQL reads the latest checkpoint information.
- It opens the WAL segment containing the redo point.
- It reads WAL records in LSN order.
- It applies each record when the corresponding data change is not safely present on disk.
- It continues until it reaches the end of valid WAL.
- It makes the database available for new connections.
The recovery process is called redo because it repeats changes described by WAL. A WAL record might describe an inserted tuple, a modified index page, or a page initialization operation.
Committed and uncommitted transactions
WAL replay restores physical changes to database pages. It does not mean that every transaction whose records were replayed committed successfully.
A crash can occur after PostgreSQL writes ordinary change records but before it writes the transaction's commit record. During recovery, the ordinary changes may be replayed, but the transaction still lacks a durable commit status. PostgreSQL's transaction visibility rules prevent other transactions from treating those changes as committed.
A transaction that has a durable commit record can become visible after recovery. This is why PostgreSQL must flush the relevant WAL before reporting a durable commit when synchronous_commit requires that behavior.
Redo point and recovery time
The amount of WAL between the redo point and the end of valid WAL affects startup time. More WAL usually means more records to inspect and replay. Checkpoints limit this distance by periodically writing dirty pages and recording a new recovery starting point.
A checkpoint does not prevent crashes. It reduces the amount of WAL that PostgreSQL normally needs to replay after one. If the server stops during recovery, PostgreSQL can repeat recovery safely because WAL replay is designed to recognize whether required page changes are already present.
Exercise
Why does PostgreSQL begin crash recovery at the checkpoint's redo LSN instead of at the newest WAL record?
Show answer
The redo LSN marks the earliest WAL position that might describe changes missing from the data files. WAL after that position must be considered until the end of valid WAL.