an UPDATE is an INSERT
postgres never overwrites a row. it writes a new version and stamps the old one with xmax.
- address
- everything.mvcc.update_is_insert
- part
- part 14, MVCC and vacuum
- chapter
- chapter 1 of 12
- reading time
- 14 min
postgres never changes a row in place. an UPDATE writes a whole new version of the row and stamps the old one with its transaction id in xmax. the old version stays right where it was until VACUUM clears it away.
that's MVCC, postgres style. the rest is watching it happen.
the picture
you know how an UPDATE feels like editing a cell in a spreadsheet? find the row, change the number, done. postgres doesn't do that. not for any row, not ever.
think of a bank ledger written in pen. you never erase a line. to change alice's balance you strike her line through (the xmax stamp) and write a fresh line wherever there's room (a new tuple, at a new address). anyone halfway through reading the old line can keep reading it. the struck lines get cleared out later, by someone else (that's VACUUM).
here's the real thing on bank, where alice is account 1. three system columns tell the story: ctid is a version's address (page, slot), xmin is the transaction that wrote it, xmax the one that replaced it (0 means nobody yet).
SELECT ctid, xmin, xmax, owner, balance FROM accounts WHERE id = 1;
UPDATE accounts SET balance = balance + 10 WHERE id = 1
RETURNING ctid, xmin, xmax, owner, balance;| ctidtid | xminxid | xmaxxid | ownertext | balancenumeric |
|---|---|---|---|---|
| (0,1) | 752 | 0 | alice | 100.00 |
| ctidtid | xminxid | xmaxxid | ownertext | balancenumeric |
|---|---|---|---|---|
| (9,46) | 797 | 0 | alice | 110.00 |
same alice, different everything else. the ctid went from (0,1) to (9,46), a different page. and xmin went from 752 to 797, because a different transaction wrote this tuple. it isn't the old row with a new number. it's a new row. (xids are just a counter. on a server that has done other work you'd see bigger numbers, but the addresses and sizes in this chapter come out the same.)
so where's the old one? a SELECT can't show it (it only returns versions your snapshot can see), but the page still has it. pageinspect reads raw pages, every slot, visible or not:
SELECT lp, lp_len, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0)) WHERE lp = 1;
SELECT lp, lp_len, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 9)) WHERE lp = 46;| lpint2 | lp_lenint2 | t_xminxid | t_xmaxxid | t_ctidtid |
|---|---|---|---|---|
| 1 | 64 | 752 | 797 | (9,46) |
| lpint2 | lp_lenint2 | t_xminxid | t_xmaxxid | t_ctidtid |
|---|---|---|---|---|
| 46 | 64 | 797 | 0 | (9,46) |
there's the struck line. page 0, slot 1 is still alice's old tuple, data untouched, but its header now says t_xmax 797 and its t_ctid points at (9,46), the version that replaced it. the new tuple's t_ctid points at itself: nothing has replaced it yet.
and why page 9? the new version goes wherever there's room. ask every page of the table how much it has left:
SELECT blkno AS page, upper - lower AS free_bytes
FROM generate_series(0, 9) AS blkno,
page_header(get_raw_page('accounts', blkno))
ORDER BY blkno;| pageint4 | free_bytesint4 |
|---|---|
| 0 | 44 |
| 1 | 24 |
| 2 | 40 |
| 3 | 48 |
| 4 | 48 |
| 5 | 48 |
| 6 | 32 |
| 7 | 16 |
| 8 | 56 |
| 9 | 4632 |
bank's rows went in all at once, so pages 0 to 8 are packed: 16 to 56 bytes free each, and alice's tuple is 64 bytes. only the last page had room. a ledger written in pen, new lines at the end.
two versions, one page
bank's page 0 was full. usually there's room on the same page, and then both versions sit side by side. the lab database has a three-row accounts table on one mostly empty page:
UPDATE accounts SET balance = balance + 10 WHERE id = 1;
SELECT lp, lp_off, lp_len, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0))
ORDER BY lp;| lpint2 | lp_offint2 | lp_lenint2 | t_xminxid | t_xmaxxid | t_ctidtid |
|---|---|---|---|---|---|
| 1 | 8152 | 39 | 752 | 753 | (0,4) |
| 2 | 8112 | 37 | 752 | 0 | (0,2) |
| 3 | 8072 | 39 | 752 | 0 | (0,3) |
| 4 | 8032 | 39 | 753 | 0 | (0,4) |
four tuples for three rows. slot 4 is the new alice, written by 753. slot 1 is the old alice, stamped with xmax 753, and its t_ctid points at slot 4. the page fills from both ends: line pointers from the front (one per slot, 4 bytes each), tuples from the back (lp_off going down from 8152 to 8032).
| lp | ctid | xmin | xmax | t_ctid | id | owner | balance |
|---|---|---|---|---|---|---|---|
| (0,1) | 752 | 753 | (0,4) | 1 | alice | 100.00 | |
| (0,2) | 752 | 0 | (0,2) | 2 | bob | 100.00 | |
| (0,3) | 752 | 0 | (0,3) | 3 | carol | 50.00 | |
| (0,4) | 753 | 0 | (0,4) | 1 | alice | 110.00 |
after: alice has a second tuple. the old one is still in slot 1, with T1's xid in xmax and a t_ctid that points at slot 4.
notice what didn't change in slot 1: its 39 bytes of data. an UPDATE only touches the old tuple's header (t_xmax, t_ctid, a few flag bits). and notice the new tuple is a full copy of the row, all 39 bytes, even though only balance changed. there's no "patch" format. every version is a whole row.
who sees which
two versions of one row is the whole trick. it means a reader never waits for a writer: it reads whichever version was current for it. "current for it" comes from the reader's snapshot: a note of which transactions had committed when it was taken, and which hadn't. READ COMMITTED takes a fresh one for every statement, REPEATABLE READ takes one at the transaction's first statement and keeps it (snapshots has the details).
the browser engine runs one session at a time, so it can't show two transactions overlapping. this one is captured from a real postgres 18.6 server with two sessions. T2 opens a REPEATABLE READ transaction and reads alice. then T1 adds 10, twice, and commits.
/* T2 */ BEGIN ISOLATION LEVEL REPEATABLE READ;
/* T2 */ SELECT balance FROM accounts WHERE id = 1; -- 100.00
/* T1 */ BEGIN;
/* T1 */ UPDATE accounts SET balance = balance + 10 WHERE id = 1;
/* T1 */ UPDATE accounts SET balance = balance + 10 WHERE id = 1;
/* T1 */ COMMIT;
/* T2 */ SELECT balance FROM accounts WHERE id = 1; -- ?- T2 is in a transaction but hasn't taken a snapshot yet:
BEGINdoesn't take one, the first query does.
T2 opens a repeatable read transaction. no snapshot yet: BEGIN doesn't take one.
see? nobody waited. T2 kept reading 100.00 while T1 wrote two new versions and committed, because the old version never moved and never changed. from step 9 on, with T2 gone, the two old versions are dead to every snapshot, and the VACUUM in the last step removes them.
the price
nothing is ever overwritten, so the old versions pile up until something removes them. a DELETE is the same stamp without the new version: it sets xmax and leaves the tuple where it is.
SELECT pg_relation_size('notes'); -- 40960
DELETE FROM notes;
SELECT pg_relation_size('notes'); -- ?SELECT pg_relation_size('notes') AS bytes;
DELETE FROM notes;
SELECT pg_relation_size('notes') AS bytes;
SELECT count(*) AS tuples, count(*) FILTER (WHERE t_xmax <> 0) AS stamped
FROM heap_page_items(get_raw_page('notes', 0));
VACUUM notes;
SELECT pg_relation_size('notes') AS bytes;| bytesint8 |
|---|
| 40960 |
| bytesint8 |
|---|
| 40960 |
| tuplesint8 | stampedint8 |
|---|---|
| 97 | 97 |
| bytesint8 |
|---|
| 0 |
every tuple on page 0 is still there after the DELETE, each one stamped. then VACUUM did shrink the file, but only because every page ended up empty. it can cut empty pages off the end of the file, and that's it. keep one row on the last page and the file doesn't shrink at all:
DELETE FROM notes WHERE id < 400;
VACUUM notes;
SELECT pg_relation_size('notes') AS bytes, count(*) AS rows_left FROM notes;| bytesint8 | rows_leftint8 |
|---|---|
| 40960 | 1 |
the space inside is free for new rows now, it just isn't given back. more in VACUUM. one thing that's different here: the lab engine has no autovacuum, so no VACUUM runs unless you type it. a real server runs autovacuum in the background and gets there on its own, eventually.
why not just overwrite?
InnoDB and oracle do overwrite. the row is changed in place, and the old values go into an undo log off to the side. a reader that needs the old version rebuilds it from the undo, by rolling the change back in its own copy (mysql 8.4 manual, oracle concepts). postgres keeps every version in the table itself. both designs let readers skip waiting for writers. they pay in different places:
| postgres | undo log (InnoDB, oracle) | |
|---|---|---|
| old versions live | in the table, next to the new one | in the undo log |
| an UPDATE writes | a whole new tuple, plus index entries if it can't stay HOT | the changed values in place, plus an undo record |
| ROLLBACK | marks the xid aborted. the table isn't touched | applies the undo to put the row back |
| cleanup | VACUUM, later | purge (InnoDB) or undo reuse, later |
| when cleanup falls behind | bloat: dead tuples in tables and indexes | the undo grows, or a long query gets "snapshot too old" (oracle) |
that ROLLBACK row is real. watch one in the lab:
BEGIN;
UPDATE accounts SET balance = 0 WHERE id = 3;
ROLLBACK;
SELECT lp, t_xmin, t_xmax, t_ctid, pg_xact_status(t_xmin::text::xid8) AS xmin_status
FROM heap_page_items(get_raw_page('accounts', 0))
ORDER BY lp;| lpint2 | t_xminxid | t_xmaxxid | t_ctidtid | xmin_statustext |
|---|---|---|---|---|
| 1 | 752 | 0 | (0,1) | committed |
| 2 | 752 | 0 | (0,2) | committed |
| 3 | 752 | 753 | (0,4) | committed |
| 4 | 753 | 0 | (0,4) | aborted |
the rolled-back version is still sitting in slot 4, and carol's tuple still has the aborted xid in xmax. nothing was undone. the commit log says that xid aborted, so every reader ignores both, and VACUUM takes slot 4 away later. rollback is instant, cleanup is someone else's job. the other side of this design, and how it compares, is in MVCC with undo logs.
one level deepera t_ctid that points at itself, HOT, and pruning
the newest version of a row has its own address in t_ctid. that's how you know you're at the end of the chain: follow t_ctid until it points at itself. (the flags say the same thing: HEAP_XMAX_INVALID, nobody replaced it.)
the two updates you ran were different, though. in the lab the new alice stayed on page 0, and the tuple card shows HOT_UPDATED on the old one and ONLY_TUPLE on the new one. that's a HOT update (heap-only tuple): the new version is on the same page and no indexed column changed, so the index isn't touched. it still points at slot 1, and anyone who arrives there follows t_ctid to slot 4.
on bank the new version went to another page, so it can't be HOT, and the primary key got a second entry for key 1 (01 00 00 00 00 00 00 00 is the int 1, as the index stores it):
SELECT itemoffset, ctid, dead
FROM bt_page_items('accounts_pkey', 1)
WHERE data = '01 00 00 00 00 00 00 00'
ORDER BY itemoffset;| itemoffsetint2 | ctidtid | deadbool |
|---|---|---|
| 2 | (0,1) | f |
| 3 | (9,46) | f |
one more entry per index, per update. that's the cost HOT avoids.
and old versions don't always wait for VACUUM. in the browser nobody else is ever looking, so the moment that UPDATE committed, alice's old version was dead to everyone. read her once more, then look at slot 1:
SELECT ctid, balance FROM accounts WHERE id = 1;
SELECT lp, lp_flags, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0)) WHERE lp = 1;
SELECT upper - lower AS free_bytes FROM page_header(get_raw_page('accounts', 0));| ctidtid | balancenumeric |
|---|---|
| (9,46) | 110.00 |
| lpint2 | lp_flagsint2 | t_xminxid | t_xmaxxid | t_ctidtid |
|---|---|---|---|---|
| 1 | 3 | null | null | null |
| free_bytesint4 |
|---|
| 108 |
gone. page 0 was nearly full, so the read pruned it on the way through: the 64 bytes are free again (page 0 had 44 free a moment ago), and slot 1 is left as a dead line pointer (lp_flags 3), kept only until VACUUM clears the index entry that still points there. on a busy server the same read would leave it alone for as long as any snapshot might still need it.
a HOT chain whose row is still alive can't do that to its first slot, because the index entry has to keep leading to the live version. so that slot becomes a redirect instead. here's the page from the two-session figure, after its VACUUM:
| lp | ctid | xmin | xmax | t_ctid | id | owner | balance |
|---|---|---|---|---|---|---|---|
| redirect → lp 4 | |||||||
| (0,2) | 758 | 0 | (0,2) | 2 | bob | 100.00 | |
| unused | |||||||
| (0,4) | 759 | 0 | (0,4) | 1 | alice | 120.00 | |
all of that is HOT updates and pruning.
keylines
- an UPDATE writes a new version of the whole row. the old one gets xmax and a t_ctid pointing at the new one.
- a DELETE is just the stamp. nothing is removed, the file doesn't shrink.
- every reader picks the version its snapshot allows, so readers never wait for writers.
- the price is dead tuples, and VACUUM is what pays it.
SET x = xstill writes a row.suppress_redundant_updates_trigger()stops that.