skip to content

ARIES

analysis, redo, undo. the textbook recovery algorithm, and why postgres only needs the middle pass.

address
everything.wal.aries
part
part 16, WAL and recovery
chapter
chapter 7 of 11
reading time
10 min

ARIES (Mohan et al., 1992) is the textbook recovery algorithm: after a crash, an analysis pass works out which transactions were running and which pages were dirty, a redo pass repeats history, losers included, and an undo pass rolls the losers back, logging a compensation log record (CLR) for every change it undoes. postgres runs only the middle pass. its rollback is a status bit, not an undo, because an old row version is never overwritten.

the picture

you know how every database course teaches "analysis, redo, undo" as if that's how all databases recover? it's how the ones that update in place recover: DB2, where ARIES was born, SQL Server, InnoDB. postgres doesn't, and the reason is the most postgres thing about postgres.

think of two accountants. one corrects the ledger by crossing out the old number and writing the new one over it (update in place). if they're interrupted mid-transaction, the next morning they have to find every number they changed for a deal that never closed and change it back (undo). the other never crosses anything out: they write a new line and a note saying which deal it belongs to (a new row version with an xid). a deal that never closed is just marked "void" in a separate book (pg_xact). the next morning there's nothing to change back.

the textbook, step by step

the paper is C. Mohan, D. Haderle, B. Lindsay, H. Pirahesh and P. Schwarz, "ARIES: a transaction recovery method supporting fine-granularity locking and partial rollbacks using write-ahead logging", ACM Transactions on Database Systems 17(1), 1992. its log records carry a prevLSN (the same transaction's previous record), every page carries a pageLSN, and recovery runs three passes. here they are on a small log: T1 commits, T2 doesn't, T3 is cut off by the crash.

textbook ARIES, simulated. postgres has no undo pass.
  1. the log
  2. crash
  3. analysis
  4. redo
  5. undo
  6. done
lsn 10T1 updatex: 1 → 2
lsn 20T2 updatey: 5 → 6
lsn 30prev 10T1 updatez: 0 → 9
lsn 40prev 30T1 commit
lsn 50prev 20T2 updatex: 2 → 3
lsn 60prev 40T1 end
lsn 70T3 updatez: 9 → 4
transaction table
empty
dirty page table
empty
pages on disk
P1 pageLSN 10
P2 pageLSN 0
P3 pageLSN 0

beforethe log as it was written. everything up to lsn 60 reached disk.

step 1/17
model of textbook ARIES (Mohan et al.), not postgres
step through the textbook algorithm. analysis rebuilds the transaction table and dirty page table; redo repeats every change whose page is older, the losers' too; undo walks the losers backwards, writing a CLR for each change. this is a model, not postgres.

the three passes, in short:

  1. analysis. start at the last checkpoint and read forward. rebuild the transaction table (who was running, their last LSN) and the dirty page table (every page that might be newer in memory than on disk, with recLSN: the first record that dirtied it). transactions without a commit at the end are losers.
  2. redo. start at the smallest recLSN and replay every update or CLR whose page's pageLSN is older than the record. losers included. this is "repeating history": after redo, the database is exactly as it was at the instant of the crash, uncommitted changes and all.
  3. undo. roll back the losers, newest change first. each undo is itself logged, as a CLR whose undoNextLSN points at the next change to undo. if the server crashes during undo, the next recovery redoes the CLRs and continues from undoNextLSN: no change is ever undone twice.

why repeat history, then undo it? because locks and in-place updates are fine-grained: a page can hold a committed change and an uncommitted one side by side, and the only safe way to get it right is to rebuild the exact crash state, then take back what was never committed. it's also what makes partial rollbacks (savepoints) work with the same machinery.

what postgres does instead

postgres keeps two of the three ideas and drops the third.

  • ├── analysisrebuild transaction table and dirty page table from the last checkpoint
  • ├── redofrom the smallest recLSN; apply if pageLSN < record LSN; losers included
  • └── undoroll back losers newest-first, logging CLRs
  • ├── no analysis passevery dirty page was written, the redo pointer is enough
  • ├── redofrom the redo pointer; apply if pd_lsn < record end; FPIs restore pages outright
  • └── no undo passno commit record: pg_xact says aborted, MVCC hides the rows
drawn by hand · after Mohan et al. (1992) and postgres 18's src/backend/access/transam/xlogrecovery.c
the three ARIES passes, and what postgres does in their place: the first tree is the textbook, the second is what postgres's startup process really does (xlogrecovery.c).

no analysis. an ARIES checkpoint is fuzzy: it only writes down the dirty page table, and redo starts at the oldest recLSN in it. a postgres checkpoint writes every dirty page before it completes (checkpoints), so "replay from where the last checkpoint started" is already a safe starting point, and there's no table to rebuild.

the same redo. this part is ARIES all the way down: replay forward, compare each page's LSN with the record, skip what the page already has, repeat history including uncommitted changes. crash recovery has it on a real server, record by record. postgres adds full-page images, which make the comparison moot for the first touch of each page after a checkpoint.

no undo. a postgres UPDATE never overwrites a row other transactions might need: it writes a new version and stamps the old one's xmax (an UPDATE is an INSERT). so after redo, a loser's changes are new versions whose xmin is the loser's xid, and old versions whose xmax is the loser's xid. neither needs touching. the loser has no commit record, so pg_xact reports it as aborted, and visibility rules ignore its versions and its xmax stamps alike. here's the page from that crash capture after the restart:

byte 0 header line pointerstuples, byte 8,192
lower 36free 8,004 B, unlitupper 8040
lpctidxminxmaxt_ctididbody
(0,1)7580(0,1)1before the checkpoint
(0,2)7590(0,2)3in flight at the crash
(0,3)7600(0,3)2committed
tuple (0,2) · off 8080 · len 51 B
t_xmin759 aborted
t_xmax0 nobody deleted it
t_ctid(0,2) itself: the newest version
infomaskHASVARWIDTHXMIN_INVALIDXMAX_INVALID
t_hoff24 B
dataid 3body in flight at the crash
headerline pointer → tuplefree space, unlitdeadin progress
real postgres 18.6 output · a native server · traces/crash-recovery, after the restart
after recovery: slot 2 is the insert of a transaction that never committed. its xmin reads aborted, and it stays on the page, invisible, until VACUUM. no undo record was written to make that so.

the same thing happens without a crash, every time you roll back. no change is reversed; the xid is marked aborted:

sqldb=lab
BEGIN;
UPDATE accounts SET balance = 0 WHERE id = 3;
ROLLBACK;
SELECT lp, t_xmin, t_xmax, pg_xact_status(t_xmax::text::xid8) AS xmax_status
FROM heap_page_items(get_raw_page('accounts', 0))
WHERE t_xmax <> 0 ORDER BY lp;
SELECT lp, t_xmin, pg_xact_status(t_xmin::text::xid8) AS xmin_status
FROM heap_page_items(get_raw_page('accounts', 0))
WHERE lp > 3 ORDER BY lp;
output, from postgres 18.6
#1 · BEGIN
#2 · UPDATE 1
#3 · ROLLBACK
#4 · SELECT 1
lpint2t_xminxidt_xmaxxidxmax_statustext
3752753aborted
(1 row)
#5 · SELECT 1
lpint2t_xminxidxmin_statustext
4753aborted
(1 row)

carol's original tuple keeps the aborted xid in xmax, and the version the UPDATE wrote is still in slot 4, aborted. the old value was never touched, so there's nothing to put back.

predict the output
postgres crashes while a 10-million-row DELETE is running. how long does undoing it take at restart?
sql
BEGIN;
DELETE FROM events;   -- 10,000,000 rows, crash halfway
-- pg_ctl start

where the bill goes

nothing is free. the undo work postgres skips at recovery and at rollback, it pays elsewhere:

ARIES-style (InnoDB, SQL Server, DB2)postgres
rollbackreverse every change, logging CLRsflip the xid to aborted in pg_xact
restart after a crashanalysis, redo, then undo the losersredo only
old row versions livein an undo log or version storein the table itself
cleanupundo log purgeVACUUM, for aborted and dead versions alike
pricebig rollbacks and crash restarts are slowtables and indexes bloat until VACUUM catches up

MVCC with undo logs is the other side of this table: how InnoDB and oracle build old versions from undo, and what that costs readers.

one level deeperCLRs and why undo needs its own log records

suppose undo didn't log anything. recovery undoes T2's change to page P1, writing P1 back to its old value. it crashes again before finishing. next time around, redo runs first and repeats history: it re-applies T2's original change to P1 (the page's LSN says it's older than the record, because undo didn't bump it). then undo has to undo it again, which works, but only because undo is idempotent here. with logical undo (undoing an insert in a b-tree that has since split, say) that stops being true. CLRs fix it: undoing a change is a logged change with its own LSN, redo repeats CLRs like any other record, and each CLR's undoNextLSN tells undo where to resume, skipping what was already undone. the bounded amount of log written during undo is one of the paper's selling points.

keylines

  • ARIES: analysis, redo, undo. repeat history, then roll back the losers.
  • CLRs log each undo, with undoNextLSN, so a crash during undo never undoes twice.
  • postgres skips analysis: its checkpoints write every dirty page, so the redo pointer is enough.
  • postgres skips undo: no version is overwritten, and an aborted xid makes its versions invisible.
  • the price of no undo is VACUUM.