WAL & recovery

WAL-before-data rule

Also called: write-ahead rule, WAL rule, pd_lsn flush gate

The WAL-before-data rule says a modified data page may not be flushed to disk until the WAL describing that modification is already durable. PostgreSQL enforces it by stamping each page with the LSN of the last WAL record that changed it (pd_lsn); the buffer manager checks that the WAL is flushed up to that LSN before writing the page out.

What this means

The core durability promise: a changed data page can't be written to disk until the WAL describing that change is already safely on disk. Postgres enforces it by stamping each page with the log position of its last change and refusing to flush the page until the WAL has caught up to that mark. Log first, data second, always.

Why it matters operationally

This single rule is what makes crash recovery sound. If data could reach disk before its WAL, a crash could leave changes with no log to recover or undo them. Because of the rule, recovery can always replay WAL forward onto the last consistent data and arrive at a correct state.

Related & next

Anatomy of a WAL recordLSN, the value stamped on each pageCheckpoint, when dirty pages finally flush

← All glossary terms · GUC reference · Error catalog