Sub topic 2
The write-ahead rule: log first, data pages later
This chapter assumes basic SQL, transactions, ACID properties, PostgreSQL pages, and reading PostgreSQL configuration settings. The goal is to understand the rule that makes PostgreSQL durable without writing every changed data page immediately.
Why PostgreSQL writes a log
A PostgreSQL table and its indexes are stored in data pages. A transaction can change several pages in memory before those pages are written to disk. These modified memory pages are called dirty buffers.
Writing every dirty page to durable storage at each change would be slow. PostgreSQL therefore records each change in the write-ahead log, or WAL, first. The data page can be written later.
The write-ahead rule is:
> PostgreSQL must write the WAL records describing a change to durable storage before it writes the changed data page to durable storage.
The rule protects the database if the server crashes between those two writes. During recovery, PostgreSQL reads the WAL and reapplies changes that were recorded but not yet reflected in the data pages.
How the rule works
A simplified sequence looks like this:
A WAL record contains enough information for PostgreSQL to reproduce a change during recovery. It may describe an inserted tuple, an updated index page, or another internal operation. WAL is not a SQL statement log. Recovery replays database-level changes rather than rerunning the original SQL text.
Each change has a position in the WAL stream called an LSN, or log sequence number. An LSN identifies a location in WAL. PostgreSQL stores the LSN of the latest WAL record needed by a data page. Before flushing that page, PostgreSQL checks that WAL through that LSN has already been flushed.
The page-level check
Suppose an update changes page P and produces WAL record W.
- PostgreSQL changes
Pin a shared memory buffer. - PostgreSQL records
Wand assigns it an LSN. - PostgreSQL stores
W's LSN in the page metadata. - Before writing
P, PostgreSQL flushes WAL throughW's LSN. - PostgreSQL writes
Pto the data file.
The data page can therefore lag behind WAL. That is intentional. WAL is the authoritative recovery path until the data page catches up.
You can inspect the current WAL position and its segment file with built-in functions:
SELECT pg_current_wal_lsn() AS current_lsn,
pg_walfile_name(pg_current_wal_lsn()) AS wal_file;The result is a current position, not a measure of all WAL generated by a transaction. To measure a change, save an earlier LSN and compare it with a later one:
SELECT pg_current_wal_lsn() AS before_lsn \gset
CREATE TABLE wal_rule_demo (id integer PRIMARY KEY, value text);
INSERT INTO wal_rule_demo VALUES (1, 'written to WAL first');
SELECT pg_size_pretty(
pg_wal_lsn_diff(pg_current_wal_lsn(), :'before_lsn')
) AS wal_generated;The table may later be flushed to its data file, but PostgreSQL must first ensure that the corresponding WAL is durable.
What the rule does not guarantee
The write-ahead rule does not mean that every SQL statement immediately reaches stable storage. It governs the ordering between WAL and data pages. The timing of WAL flushing at transaction commit depends on settings such as synchronous_commit, while fsync controls whether PostgreSQL requests durable storage behavior from the operating system.
Checkpointing later writes many dirty pages and records a consistent recovery starting point. Those are separate mechanisms built on the same WAL stream.
Exercise
Why may PostgreSQL write a dirty data page after its transaction has finished, and what must happen before that page is written?
Show answer
PostgreSQL can delay the data-page write to reduce I/O. It must first flush the WAL records required to reconstruct the page during recovery.