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.
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:
a transaction starts. nothing is written yet.
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:
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));| countint8 |
|---|
| 3 |
| page_lsnpg_lsn | insertedpg_lsn | flushedpg_lsn |
|---|---|---|
| 0/17EBD80 | 0/17F2B90 | 0/17DB878 |
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:
-- 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?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;| buffer_evictedbool |
|---|
| t |
| insertedpg_lsn | flushedpg_lsn |
|---|---|
| 0/17FA760 | 0/17FA760 |
| insertedpg_lsn | flushedpg_lsn |
|---|---|
| 0/17FA788 | 0/17FA788 |
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.
| the WAL | the table's pages | |
|---|---|---|
| written at COMMIT | yes, up to the commit record | no |
| shape of the write | appended, sequential, a few bytes per change | 8 KB per page, wherever the page lives |
| how many fsyncs | one per flush, shared by everyone committing at once | a batch at the next checkpoint |
| if lost in a crash | the transaction never committed | rebuilt 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.