Sub topic 2
How the Write-Ahead Log Makes Replay Possible
This chapter assumes you know basic PostgreSQL administration, can read postgresql.conf, and understand that a transaction either commits or rolls back. You should also know that PostgreSQL stores table data in a cluster data directory.
The working model
The write-ahead log, or WAL, is a sequential record of changes made to a PostgreSQL cluster. PostgreSQL writes a description of a change to WAL before it writes the changed data page to the table or index files.
This ordering is the central durability rule:
- A transaction changes a row in memory.
- PostgreSQL creates WAL records describing the change.
- PostgreSQL flushes the required WAL records to durable storage.
- PostgreSQL reports a successful commit.
- The changed data pages can be written later.
If the server stops after step 4 but before step 5, the data files may not contain the newest changes. During startup, PostgreSQL reads the WAL and replays the missing changes.
The WAL is therefore a recovery source, not a second copy of every table. WAL records describe operations such as inserting a row, changing an index page, or modifying a page after a checkpoint. PostgreSQL uses these records to bring data files forward to a consistent state.
WAL positions and segments
A log sequence number, or LSN, identifies a position in the WAL stream. An LSN is useful when you need to describe an exact recovery point or compare how far a server has written, flushed, or replayed WAL.
You can inspect the current insertion position with SQL:
SELECT pg_current_wal_lsn();WAL is stored in segment files under pg_wal. PostgreSQL creates a new segment as the current one fills. Segment files have generated names, and a segment is normally 16 MiB unless the cluster was initialized with a different size.
A segment is not automatically an archive. PostgreSQL may recycle or remove old segments after they are no longer needed locally. Point-in-time recovery needs a separate process that copies completed segments to durable archive storage.
Checkpoints and replay
A checkpoint records a point at which PostgreSQL has written all dirty data pages known at that point to the data files. The checkpoint reduces the amount of WAL that PostgreSQL must inspect after a crash, but it does not eliminate the need for WAL.
Recovery starts from a checkpoint and replays later WAL records. PostgreSQL can replay the same record safely according to the recovery rules for that record. This process is called redo or WAL replay.
A base backup supplies the data files from an earlier time. Archived WAL supplies the changes after that backup. Together, they let PostgreSQL reconstruct a later database state. WAL alone cannot replace the base backup because it does not contain a complete independent copy of every page.
What WAL replay can and cannot recover
WAL replay protects against a crash or loss of the active data directory when you have a usable base backup and every required WAL segment. It can reproduce committed changes and discard incomplete transactions.
WAL does not protect you if the archive is missing a required segment, if the base backup is corrupt, or if an operator deletes important data and the recovery target is not chosen before that deletion. For that case, recovery must stop at a time, LSN, transaction, or named restore point before the unwanted change.
Exercise
Why is a base backup required for point-in-time recovery if WAL contains all database changes?
Show answer
WAL describes changes but is not a complete copy of the database. The base backup provides the starting data files, and WAL replays changes made after that starting point.