skip to content

the write-ahead rule

log first, data later. why a commit only has to write one small sequential thing.

address
everything.wal.write_ahead_rule
part
part 16, WAL and recovery
chapter
chapter 1 of 11
reading time
9 min

the write-ahead rule: a change is described in the WAL before the data page it changes is allowed on disk. a write changes the page in memory, appends a record to the log, and stamps the page with that record's LSN. COMMIT then flushes the log up to its own commit record, one small sequential write, and returns. the table's pages follow whenever it suits the server, but never ahead of their log.

the picture

you know how a COMMIT comes back in well under a millisecond, but a transaction can change rows scattered over a hundred pages? writing a hundred 8 KB pages to a hundred places on disk, and waiting for all of them, would take much longer than that. postgres doesn't. at COMMIT it writes one thing: the end of the log.

think of a shop's day book. every sale is written in the book, in pen, in order, the moment it happens (the WAL). the shelves (the table's pages) get restocked when someone has a minute. if the shop burns down overnight, the day book in the safe says exactly what the shelves should hold. the one rule: never touch a shelf for a sale that isn't in the book yet. a shelf that's ahead of the book can't be explained after a fire. a shelf that's behind it can always be caught up.

hover a stage.

the order of things in one UPDATE ... COMMIT. hover a stage. the only disk write COMMIT waits for is the third one.

two pointers matter: how far the log has been inserted (written into wal_buffers) and how far it's been flushed (on disk, durable). and every page carries pd_lsn, the end of the last record that changed it. the rule, in one line: a page may be written only if flushed ≥ pd_lsn.

watch it on one page

here's one transaction on lab's accounts, captured on the same engine the run buttons use. step through and watch the flush line, and the page's pd_lsn:

T1BEGIN
pagesaccounts·0pd_lsn+0xF218dirty

a transaction starts. nothing is written yet.

flush line: durable from here backin wal_buffers, not on diskredo pointer, record headerjust appended, being replayeddirty page
step 1/5
real output, postgres 18.6 (PGlite 0.5.12) · write-ahead
step through. each UPDATE is a record past the flush line (dashed: only in memory), and the page's pd_lsn moves to the end of it. the page is dirty the whole time. COMMIT moves the flush line past everything; CHECKPOINT, much later, writes the page.

two updates, 70 bytes of WAL each, then a 34-byte commit record. at COMMIT the server wrote 174 bytes of log, in one sequential piece, and that's what made the transfer durable. the 8 KB page it changed was still only in memory, and stayed there until the CHECKPOINT. (the flush line sits in front of every record because the dataset load before step 1 left a lot of WAL unflushed: nothing had committed since.)

you can read the same three numbers yourself. inside the transaction, the page is ahead of the flushed log:

sqldb=lab · session rule
SELECT count(*) FROM accounts;
BEGIN;
UPDATE accounts SET balance = balance - 25.00 WHERE id = 1;
SELECT lsn AS page_lsn,
       pg_current_wal_insert_lsn() AS inserted,
       pg_current_wal_flush_lsn() AS flushed
FROM page_header(get_raw_page('accounts', 0));
output, from postgres 18.6
#1 · SELECT 1
countint8
3
(1 row)
#2 · BEGIN
#3 · UPDATE 1
#4 · SELECT 1
page_lsnpg_lsninsertedpg_lsnflushedpg_lsn
0/17EBD800/17F2B900/17DB878
(1 row)

still in a transaction (no COMMIT yet)

the page's LSN is past the flushed position: its change exists only in wal_buffers and in the page itself, both in memory. if the server died now, both would vanish together, and that's fine, because nothing was promised. (the inserted position is further on than the page's LSN: reading the catalog for this query wrote some WAL of its own, hint-bit page images with checksums on. see full-page writes.)

the rule, enforced

so what happens if the server needs that buffer for something else before the transaction ends? it can write the dirty page out, uncommitted change and all. but first it has to flush the WAL up to the page's LSN. pg_buffercache_evict() forces exactly that situation:

predict the output
the transaction above hasn't committed. you force its dirty page out of shared buffers. what happens to the flushed position?
sql
-- still inside the transaction from above
SELECT pg_buffercache_evict(bufferid) FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('accounts') AND relforknumber = 0;
SELECT pg_current_wal_flush_lsn();   -- before or after page_lsn?
sqldb=lab · session rule
continues session rule (1 earlier block run first)
SELECT buffer_evicted
FROM pg_buffercache_evict((SELECT bufferid FROM pg_buffercache
                           WHERE relfilenode = pg_relation_filenode('accounts')
                             AND relforknumber = 0));
SELECT pg_current_wal_insert_lsn() AS inserted, pg_current_wal_flush_lsn() AS flushed;
COMMIT;
SELECT pg_current_wal_insert_lsn() AS inserted, pg_current_wal_flush_lsn() AS flushed;
output, from postgres 18.6
#1 · SELECT 1
buffer_evictedbool
t
(1 row)
#2 · SELECT 1
insertedpg_lsnflushedpg_lsn
0/17FA7600/17FA760
(1 row)
#3 · COMMIT
#4 · SELECT 1
insertedpg_lsnflushedpg_lsn
0/17FA7880/17FA788
(1 row)

the flushed position jumped to the insert position the moment the page was evicted, before any COMMIT. the flush covers everything inserted so far, not just up to the page's LSN, because flushing a little more costs nothing extra. then COMMIT appends its record and flushes again.

in the textbook's words, postgres's buffer manager is steal (a page with uncommitted changes may be written to disk) and no-force (COMMIT doesn't force the changed pages to disk). that's the combination that needs both redo (for committed changes that were never written) and, in the textbook, undo (for uncommitted ones that were). postgres gets away without undo, which ARIES explains.

why log first

because the log is the cheap thing to make durable.

WAL flushed at COMMIT174 B
the page it changed8,192 B
real output, postgres 18.6 (PGlite 0.5.12) · write-ahead
bytes made durable at COMMIT for the transfer above, against the size of the one page it changed. the page is written later, at a checkpoint, along with every other change to it since.
the WALthe table's pages
written at COMMITyes, up to the commit recordno
shape of the writeappended, sequential, a few bytes per change8 KB per page, wherever the page lives
how many fsyncsone per flush, shared by everyone committing at oncea batch at the next checkpoint
if lost in a crashthe transaction never committedrebuilt from the WAL

a change is described in tens of bytes; the page it lands on is 8,192. postgres makes the small, sequential thing durable right away and lets the big, scattered things wait, sometimes long enough that one page absorbs hundreds of changes before it's written once. that's also why the WAL is where everything else hangs off: crash recovery replays it, replicas stream it, backups archive it.

keylines

  • log first, data later: no page reaches disk before the WAL that describes it.
  • pd_lsn on every page is the end of the last record that changed it. the rule is flushed ≥ pd_lsn.
  • COMMIT waits for one sequential flush of the log, not for any table page.
  • an uncommitted change can be written to disk (steal), but only behind its WAL.
  • the rule assumes fsync is honest. fsync = off breaks it.