Write ahead of the data
Postgres guarantees durability through the write ahead log. Before any change touches the actual data pages, a record describing the change is written and flushed to the WAL. If the server crashes, replay of the WAL rebuilds any changes that had not yet reached the data files.
Why this is safe
- A commit only returns once its WAL record is safely on disk.
- Dirty data pages can be flushed lazily later, since the WAL already holds the truth.
- On restart, crash recovery replays WAL from the last checkpoint forward.
A checkpoint periodically flushes dirty pages to disk and records a safe restart point so recovery does not have to replay the entire log.
Archiving and recovery
WAL also enables backups beyond a single moment:
- WAL archiving copies each completed WAL segment to safe storage as it fills.
- Combined with a base backup, archived WAL lets you do point in time recovery, restoring the database to any moment by replaying WAL up to a chosen time.
Key idea
The write ahead log records every change before the data pages move, giving crash recovery and, when segments are archived alongside a base backup, point in time recovery to any chosen moment.