skip to content

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).

sqldb=bank · ext=pageinspect · session ledger
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;
output, from postgres 18.6
#1 · SELECT 1
ctidtidxminxidxmaxxidownertextbalancenumeric
(0,1)7520alice100.00
(1 row)
#2 · UPDATE 1
ctidtidxminxidxmaxxidownertextbalancenumeric
(9,46)7970alice110.00
(1 row)

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:

sqldb=bank · ext=pageinspect · session ledger
continues session ledger (1 earlier block run first)
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;
output, from postgres 18.6
#1 · SELECT 1
lpint2lp_lenint2t_xminxidt_xmaxxidt_ctidtid
164752797(9,46)
(1 row)
#2 · SELECT 1
lpint2lp_lenint2t_xminxidt_xmaxxidt_ctidtid
46647970(9,46)
(1 row)

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:

sqldb=bank · ext=pageinspect · session ledger
continues session ledger (2 earlier blocks run first)
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;
output, from postgres 18.6
SELECT 10
pageint4free_bytesint4
044
124
240
348
448
548
632
716
856
94632
(10 rows)

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:

sqldb=lab
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;
output, from postgres 18.6
#1 · UPDATE 1
#2 · SELECT 4
lpint2lp_offint2lp_lenint2t_xminxidt_xmaxxidt_ctidtid
1815239752753(0,4)
28112377520(0,2)
38072397520(0,3)
48032397530(0,4)
(4 rows)

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).

the page =
byte 0 header line pointerstuples, byte 8,192
lower 40free 7,992 B, unlitupper 8032
lpctidxminxmaxt_ctididownerbalance
(0,1)752753(0,4)1alice100.00
(0,2)7520(0,2)2bob100.00
(0,3)7520(0,3)3carol50.00
(0,4)7530(0,4)1alice110.00
tuple (0,1) · off 8152 · len 39 B
t_xmin752 committed
t_xmax753 committed
t_ctid(0,4) → the next version
infomaskHASVARWIDTHXMIN_COMMITTEDHOT_UPDATED
t_hoff24 B
dataid 1owner alicebalance 100.00
headerline pointer → tuplefree space, unlitdeadin progresswhat this step changed
real postgres 18.6 output · PGlite 0.5.12, the engine this page runs · traces/lab-update

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.

switch between before and after. what changed is hot: a new slot, and slot 1's header. the card on the right is that header, with a t_ctid that now points at the next version.

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.

predict the output
T2 reads alice's balance again, in the same transaction, after T1 has committed. what does it get?
sql
/* 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;   -- ?
as seen by =
no snapshot
T1
·
T2
·
1
BEGIN ISOLATION LEVEL REPEATABLE READ
versions of id = 1 in accounts
  • T2 is in a transaction but hasn't taken a snapshot yet: BEGIN doesn't take one, the first query does.

T2 opens a repeatable read transaction. no snapshot yet: BEGIN doesn't take one.

t_ctid linkvisible to this snapshotwriter still in progressdead to every snapshotheader fields
step 1/11
real postgres 18.6 output · a native server · traces/update-twice
step through, and switch who is looking. green is the version that reader sees, and the line under the chain says which rule decided it. versions no snapshot can see anymore turn into dead ghosts.

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.

predict the output
notes has 400 rows. you delete all of them. how big is the table's file afterwards?
sql
SELECT pg_relation_size('notes');  -- 40960
DELETE FROM notes;
SELECT pg_relation_size('notes');  -- ?
sqldb=lab
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;
output, from postgres 18.6
#1 · SELECT 1
bytesint8
40960
(1 row)
#2 · DELETE 400
#3 · SELECT 1
bytesint8
40960
(1 row)
#4 · SELECT 1
tuplesint8stampedint8
9797
(1 row)
#5 · VACUUM
#6 · SELECT 1
bytesint8
0
(1 row)

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:

sqldb=lab
DELETE FROM notes WHERE id < 400;
VACUUM notes;
SELECT pg_relation_size('notes') AS bytes, count(*) AS rows_left FROM notes;
output, from postgres 18.6
#1 · DELETE 399
#2 · VACUUM
#3 · SELECT 1
bytesint8rows_leftint8
409601
(1 row)

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:

postgresundo log (InnoDB, oracle)
old versions livein the table, next to the new onein the undo log
an UPDATE writesa whole new tuple, plus index entries if it can't stay HOTthe changed values in place, plus an undo record
ROLLBACKmarks the xid aborted. the table isn't touchedapplies the undo to put the row back
cleanupVACUUM, laterpurge (InnoDB) or undo reuse, later
when cleanup falls behindbloat: dead tuples in tables and indexesthe undo grows, or a long query gets "snapshot too old" (oracle)

that ROLLBACK row is real. watch one in the lab:

sqldb=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;
output, from postgres 18.6
#1 · BEGIN
#2 · UPDATE 1
#3 · ROLLBACK
#4 · SELECT 4
lpint2t_xminxidt_xmaxxidt_ctidtidxmin_statustext
17520(0,1)committed
27520(0,2)committed
3752753(0,4)committed
47530(0,4)aborted
(4 rows)

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):

sqldb=bank · ext=pageinspect · session ledger
continues session ledger (3 earlier blocks run first)
SELECT itemoffset, ctid, dead
FROM bt_page_items('accounts_pkey', 1)
WHERE data = '01 00 00 00 00 00 00 00'
ORDER BY itemoffset;
output, from postgres 18.6
SELECT 2
itemoffsetint2ctidtiddeadbool
2(0,1)f
3(9,46)f
(2 rows)

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:

sqldb=bank · ext=pageinspect · session ledger
continues session ledger (4 earlier blocks run first)
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));
output, from postgres 18.6
#1 · SELECT 1
ctidtidbalancenumeric
(9,46)110.00
(1 row)
#2 · SELECT 1
lpint2lp_flagsint2t_xminxidt_xmaxxidt_ctidtid
13nullnullnull
(1 row)
#3 · SELECT 1
free_bytesint4
108
(1 row)

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:

byte 0 header line pointerstuples, byte 8,192
lower 40free 8,072 B, unlitupper 8112
lpctidxminxmaxt_ctididownerbalance
redirect → lp 4
(0,2)7580(0,2)2bob100.00
unused
(0,4)7590(0,4)1alice120.00
line pointer 1 is redirect: it points at lp 4, the live end of a HOT chain, so index entries for lp 1 still work.
headerline pointer → tuplefree space, unlitdeadin progress
real postgres 18.6 output · a native server · traces/update-twice, after the VACUUM
after VACUUM: slot 1 is a redirect to slot 4, slot 3 is free again, and the index entry for (0,1) still works.

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 = x still writes a row. suppress_redundant_updates_trigger() stops that.