← Lessons

quiz vs the machine

Gold1410

Databases

WAL And Archiving

How write ahead logging gives Postgres durability and enables point in time recovery.

6 min read · core · beat Gold to climb

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.

Check yourself

Answer to earn rating on the learn ladder.

1. Why is writing the WAL before the data pages safe?

2. What does a checkpoint do?

3. What does archived WAL plus a base backup enable?