skip to content

MVCC with undo logs

how InnoDB and Oracle keep old versions in an undo log instead, and what each design pays.

address
everything.mvcc.undo_logs
part
part 14, MVCC and vacuum
chapter
chapter 12 of 12
reading time
10 min

postgres keeps old row versions in the table. InnoDB (mysql) and oracle update in place and keep the old values in an undo log instead: a reader that needs an older version rebuilds it by applying undo. both give readers a snapshot without blocking writers. they pay in opposite places: postgres in VACUUM and bloat, undo designs in undo space, reconstruction work for old readers, and expensive rollbacks.

the picture

you know how an UPDATE in postgres is an insert, and the old version sits in the table until VACUUM removes it? the obvious question is: why not just change the row, and keep the old value somewhere off to the side? that's the other way to build MVCC, and it's what InnoDB and oracle do.

think of two ways to keep a document's history. one keeps every printed draft in the same folder, newest on top, and someone clears out the old drafts later (postgres). the other keeps only the latest draft in the folder, plus a notebook of edits; to read yesterday's version you take today's and undo the edits backwards (undo logs). the folder stays thin, but reading old versions costs work, and the notebook can't be thrown away while anyone still wants yesterday.

trx 10 inserts alice
postgres · every version in the heap page
xmin10xmax0balance100
↓ t_ctid, old to new
InnoDB · one row in the clustered index, old values in undo
DB_TRX_ID10DB_ROLL_PTR·balance100
undo log, in the undo tablespace

one row, one version, in both. (InnoDB wrote an insert undo record too, but that one is only for rollback and goes away at commit.)

just written or just readthe version the old reader getsfreed by cleanupheaders and undo recordspointers
step 1/5
drawn by hand · the shapes follow the postgres 18 and mysql 8.4 manuals; the ids and balances are made up
step through: one row, updated twice, then read by an old snapshot, then cleaned up. postgres adds a version per update; InnoDB overwrites the row and chains undo records behind its roll pointer. the old reader gets 100 either way, by very different amounts of work.

how InnoDB does it

according to the mysql 8.4 manual, every InnoDB row carries hidden fields: a 6-byte DB_TRX_ID, the last transaction that inserted or updated it, and a 7-byte DB_ROLL_PTR, the roll pointer, which points at an undo log record holding what the row looked like before. a DELETE is an update that sets a deleted bit. the rows live in the clustered index (the primary key's b-tree), and they're updated in place.

a consistent read walks back from the current row:

hover a stage.

drawn by hand · from the mysql 8.4 manual, innodb multi-versioning
an InnoDB consistent read, as the mysql 8.4 manual describes it. hover a stage. the work grows with how many changes happened since the reader's snapshot, not with the size of the row.

the manual splits undo into two kinds. insert undo is only needed for rollback and is discarded at commit. update undo is also needed for consistent reads, so it can only be discarded once no snapshot could need it. a background purge does that, and only then are delete-marked rows and their index records physically removed (purge configuration). the queue of undo waiting for purge is the history list length in SHOW ENGINE INNODB STATUS.

secondary indexes work differently from the clustered one: their records have no hidden columns and aren't updated in place. changing an indexed column delete-marks the old entry and inserts a new one, and purge removes the old one later. when a secondary index page has been touched by a newer transaction, a read has to go to the clustered index to check the row, so covering index reads are skipped there.

how oracle does it

the oracle database concepts guide describes the same idea at the level of whole blocks. every change writes undo entries into undo segments. a query notes the SCN (system change number) when it starts, and for every block that changed after that, the database copies the current block into a new buffer and applies undo to it until it reflects that SCN: a consistent read clone. read committed does this per statement, serializable per transaction, the same split as postgres's snapshots and isolation levels.

hover a stage.

drawn by hand · from the oracle database 23 concepts guide, data concurrency and consistency
an oracle consistent read, as the concepts guide describes it. hover a stage. the last stage is the one postgres can't hit: its old versions stay in the table until nobody needs them.

the catch is in the same guide: undo space gets reused. a long-running report can need undo that a newer transaction has already overwritten, and then it fails with a snapshot too old error. an undo retention period tells the database how long to try to keep old undo around.

what each design pays

postgres (versions in the heap)undo logs (InnoDB, oracle)
an UPDATE writesa whole new tuple, plus an entry in every index unless it's HOTthe changed row in place, plus an undo record
the table holdsevery version not yet vacuumedonly the newest version (plus delete-marked rows until purge)
an old readerchecks each version's xmin and xmax, picks onerebuilds the version by applying undo, one change at a time
ROLLBACKflips two bits in the commit log, instantlyapplies undo to put every changed row back
crash recoveryredo only: in-flight transactions' versions just stay invisibleredo, then roll back the transactions that were in flight (InnoDB recovery)
cleanupVACUUM: scan heap and indexes for dead tuplespurge: discard undo, remove delete-marked rows
a long reader holds backcleanup of everything: bloat in every tableundo discard: the undo grows, or (oracle) the reader fails

rollback is the row people forget. postgres's is free, and it leaves garbage. watch it in the browser:

sqldb=lab
SELECT pg_relation_size('notes') AS bytes;
BEGIN;
UPDATE notes SET body = body || '!';
ROLLBACK;
SELECT pg_relation_size('notes') AS bytes;
SELECT pg_xact_status(t_xmin::text::xid8) AS written_by, count(*) AS tuples
FROM generate_series(0, pg_relation_size('notes') / 8192 - 1) AS b,
     heap_page_items(get_raw_page('notes', b::int))
WHERE lp_flags = 1
GROUP BY 1 ORDER BY 1;
output, from postgres 18.6
#1 · SELECT 1
bytesint8
40960
(1 row)
#2 · BEGIN
#3 · UPDATE 400
#4 · ROLLBACK
#5 · SELECT 1
bytesint8
73728
(1 row)
#6 · SELECT 2
written_bytexttuplesint8
aborted400
committed400
(2 rows)

the ROLLBACK itself did nothing to the table. 400 rows updated, rolled back, and the file is bigger than before, holding 400 versions written by an aborted transaction next to the 400 originals. no reader will ever see them; VACUUM will free them. an undo-log engine would instead spend the ROLLBACK walking its undo and restoring 400 rows, and the table would end up as it started. a big transaction that fails late is cheap to abort in postgres and expensive in InnoDB, and the same goes for the in-flight transactions after a crash: the mysql manual warns that rolling back an interrupted transaction can take three or four times as long as it had been running.

predict the output
a two-hour report runs while the table it reads gets heavy updates. which can happen?
sql
-- session 1, for two hours:
SELECT ... FROM orders ...;
-- meanwhile, everyone else:
UPDATE orders SET ...;   -- all day

so which is better?

neither, which is why both exist. undo designs keep tables compact and stable in size, keep old versions out of the table entirely, and an update only touches the secondary indexes whose columns changed. they pay with reconstruction work for long reads, undo space that has to be sized and can run out, purge lag under heavy write load, and slow rollbacks and recoveries of big transactions. postgres pays with dead tuples in tables and indexes, VACUUM as a permanent background job, index entries for non-HOT updates, and wraparound management, and gets instant rollback, simple crash recovery, and readers that never fail for lack of history. a fuller side by side of the two engines is postgres vs InnoDB.

keylines

  • postgres: versions in the table, VACUUM cleans up. InnoDB and oracle: update in place, old values in undo.
  • an undo reader rebuilds old versions by applying undo, one change at a time.
  • postgres rollback is two bits in the commit log; an undo engine applies its undo to put rows back.
  • long readers cost postgres bloat, and cost undo designs undo space or a "snapshot too old" error.
  • neither is free. they just send the bill to different places.