skip to content

transaction ids, xmin and xmax

who made this version and who ended it. virtual xids, lazy assignment, and SELECT xmin, xmax, ctid.

address
everything.mvcc.transaction_ids
part
part 14, MVCC and vacuum
chapter
chapter 2 of 12
reading time
8 min

every tuple carries two transaction ids in its header: xmin, the transaction that wrote it, and xmax, the one that deleted it, replaced it or locked it (0 if nobody has). a transaction only gets a real xid when it first writes. before that, and forever if it only reads, it gets by on a virtual xid that costs nothing.

the picture

you know how an UPDATE writes a new row and stamps the old one? the stamp is a number. so is the birth certificate on the new one. those numbers are transaction ids, and they're the whole vocabulary MVCC is written in.

think of a library where every book has two stamps inside the cover: who shelved it, and who pulled it (blank while it's still on the shelf). a reader doesn't need either stamp to read, so nobody gives readers a stamp number at the door. you get one at the moment you shelve or pull something (your first write).

hover a stage.

an xid's life. hover a stage. the virtual xid exists from the first statement, the real xid only from the first write, and whether it committed is decided in one place, the commit log, not in the tuples.

here's lab's accounts table, with the three system columns every table has. ctid is where the version lives, xmin and xmax are the stamps:

sqldb=lab · session xids
SELECT ctid, xmin, xmax, id, owner, balance FROM accounts ORDER BY id;
output, from postgres 18.6
SELECT 3
ctidtidxminxidxmaxxididint4ownertextbalancenumeric
(0,1)75201alice100.00
(0,2)75202bob100.00
(0,3)75203carol50.00
(3 rows)

all three rows were written by xid 752, the transaction that loaded the dataset, and nobody has touched them since: xmax 0.

lazy assignment

an xid isn't handed out at BEGIN. pg_current_xact_id_if_assigned() tells you whether you have one yet, without taking one:

sqldb=lab · session xids
continues session xids (1 earlier block run first)
BEGIN;
SELECT pg_current_xact_id_if_assigned() AS my_xid;
SELECT owner FROM accounts WHERE id = 1;
SELECT pg_current_xact_id_if_assigned() AS my_xid;
UPDATE accounts SET balance = balance - 10 WHERE id = 1;
SELECT pg_current_xact_id_if_assigned() AS my_xid;
COMMIT;
output, from postgres 18.6
#1 · BEGIN
#2 · SELECT 1
my_xidxid8
null
(1 row)
#3 · SELECT 1
ownertext
alice
(1 row)
#4 · SELECT 1
my_xidxid8
null
(1 row)
#5 · UPDATE 1
#6 · SELECT 1
my_xidxid8
753
(1 row)
#7 · COMMIT

nothing after BEGIN, nothing after the read, 753 the moment it wrote. that's on purpose: xids are a finite resource (32 bits, and they wrap), every assigned one has to be tracked in the commit log, and every running one makes snapshots bigger. most transactions on a busy server only read, so most never cost an xid.

what a transaction has from the start is a virtual xid. it's in the lock table as a lock on itself:

sqldb=lab · session xids
continues session xids (2 earlier blocks run first)
BEGIN;
SELECT locktype, virtualxid, transactionid, mode
FROM pg_locks
WHERE locktype IN ('virtualxid', 'transactionid')
ORDER BY locktype;
UPDATE accounts SET balance = balance + 10 WHERE id = 2;
SELECT locktype, virtualxid, transactionid, mode
FROM pg_locks
WHERE locktype IN ('virtualxid', 'transactionid')
ORDER BY locktype;
ROLLBACK;
output, from postgres 18.6
#1 · BEGIN
#2 · SELECT 1
locktypetextvirtualxidtexttransactionidxidmodetext
virtualxid0/5nullExclusiveLock
(1 row)
#3 · UPDATE 1
#4 · SELECT 2
locktypetextvirtualxidtexttransactionidxidmodetext
transactionidnull754ExclusiveLock
virtualxid0/5nullExclusiveLock
(2 rows)
#5 · ROLLBACK

0/5 is backend 0, local transaction 5. (the browser engine has one backend, so it's always backend 0, and every lock in pg_locks is ours. on a server, filter by pid.) after the UPDATE the transaction holds a second lock, on its real xid, 754. other sessions that need to wait for this transaction wait on that lock: that's what "waits for ShareLock on transaction 754" means in lock waits.

predict the output
a transaction runs 1,000 SELECTs and then COMMITs. how many xids did it use?
sql
BEGIN;
SELECT ... ;   -- a thousand times
COMMIT;

two sessions, captured

the browser engine has one session, so it can't show the part that surprises people: xids are numbered in the order transactions first write, not the order they begin. this is a real postgres 18.6 server with two sessions. T1 begins first and reads. T2 begins later and writes first.

T1
snapshot 759:759:
T2
·
1
BEGIN
accounts · page 0
byte 0 header line pointerstuples, byte 8,192
lower 32free 8,080 B, unlitupper 8112
lpctidxminxmaxt_ctididownerbalance
(0,1)7580(0,1)1alice100.00
(0,2)7580(0,2)2bob100.00
locks
lock manager
  • T1ExclusiveLock4/4

T1 begins. it holds a virtual xid, a number only this server's lock table knows. no real xid yet.

step 1/8
real output, a postgres 18.6 server · xid-lazy
step through. watch the locks panel: each session holds its virtual xid from BEGIN, and a transactionid lock appears only at its first write. on the page, the new versions are hot when they appear, and nothing on the page changes at ROLLBACK or COMMIT.

T2 got 759 and T1 got 760, the other way round from their BEGINs. and look at the last step: bob's original tuple still says xmax 759, the xid of a transaction that rolled back.

byte 0 header line pointerstuples, byte 8,192
lower 40free 7,992 B, unlitupper 8032
lpctidxminxmaxt_ctididownerbalance
(0,1)758760(0,4)1alice100.00
(0,2)758759(0,3)2bob100.00
(0,3)7590(0,3)2bob90.00
(0,4)7600(0,4)1alice110.00
tuple (0,2) · off 8112 · len 37 B
t_xmin758 committed
t_xmax759 aborted
t_ctid(0,3) → the next version
infomaskHASVARWIDTHXMIN_COMMITTEDXMAX_INVALIDHOT_UPDATED
t_hoff24 B
dataid 2owner bobbalance 100.00
headerline pointer → tuplefree space, unlitdeadin progress
real postgres 18.6 output · a native server · traces/xid-lazy, the last step
after both are done. bob's original version (slot 2) carries T2's xmax, but T2 aborted, so nothing was deleted. slot 3 is T2's rolled-back version, never visible to anyone.

what xmax really means

xmax is "the transaction that last laid a claim on this version". that can be:

0
nobody. the version is current.
a committed xid
deleted, or replaced by an UPDATE (then t_ctid points at the new version).
an aborted xid
someone tried and rolled back. the version is alive. the number stays until something overwrites it.
a locker's xid
SELECT FOR UPDATE or FOR SHARE (row-locking clauses). a flag in t_infomask says lock only.
a MultiXact id
several lockers at once. a different counter entirely, see MultiXacts.

the lock case is the one that fools people, because the number stays after the lock is gone:

sqldb=lab · session xids
continues session xids (3 earlier blocks run first)
BEGIN;
SELECT owner FROM accounts WHERE id = 3 FOR UPDATE;
COMMIT;
SELECT ctid, xmin, xmax, owner FROM accounts WHERE id = 3;
SELECT lp, t_xmax, (heap_tuple_infomask_flags(t_infomask, t_infomask2)).raw_flags
FROM heap_page_items(get_raw_page('accounts', 0)) WHERE lp = 3;
output, from postgres 18.6
#1 · BEGIN
#2 · SELECT 1
ownertext
carol
(1 row)
#3 · COMMIT
#4 · SELECT 1
ctidtidxminxidxmaxxidownertext
(0,3)752755carol
(1 row)
#5 · SELECT 1
lpint2t_xmaxxidraw_flagstext[]
3755
(1 row)

carol is fine, and her xmax is 755. the flags say why it doesn't count: HEAP_XMAX_LOCK_ONLY and HEAP_XMAX_EXCL_LOCK, a row lock, not a delete. the visibility rules read those flags before they read the number.

subtransactions get their own

a SAVEPOINT that writes gets an xid of its own, a subtransaction xid, so that rolling back to the savepoint can undo just its part (by marking just that xid aborted):

sqldb=lab · session xids
continues session xids (4 earlier blocks run first)
BEGIN;
INSERT INTO accounts VALUES (4, 'dave', 10.00);
SAVEPOINT before_erin;
INSERT INTO accounts VALUES (5, 'erin', 10.00);
SELECT xmin, id, owner FROM accounts WHERE id >= 4 ORDER BY id;
ROLLBACK TO before_erin;
COMMIT;
SELECT xmin, id, owner FROM accounts WHERE id >= 4 ORDER BY id;
output, from postgres 18.6
#1 · BEGIN
#2 · INSERT 0 1
#3 · SAVEPOINT
#4 · INSERT 0 1
#5 · SELECT 2
xminxididint4ownertext
7564dave
7575erin
(2 rows)
#6 · ROLLBACK
#7 · COMMIT
#8 · SELECT 1
xminxididint4ownertext
7564dave
(1 row)

dave was written by 756, erin by 757, inside the same transaction. after ROLLBACK TO, 757 is aborted and erin is gone; 756 commits with the rest. postgres keeps the child → parent links in pg_subtrans, so a reader that finds xmin 757 can find out which top-level transaction it belonged to. it's also why a loop that opens a savepoint per row (some drivers and ORMs do this for you) burns xids fast.

that second column is the other thing to know: pg_current_xact_id() returns an xid8, a 64-bit id with an epoch in the high half that never wraps. the xid in tuple headers is only the low 32 bits. why that's fine most of the time, and what happens when it isn't, is freezing and wraparound.

keylines

  • xmin = who wrote this version, xmax = who ended or locked it, 0 = nobody.
  • a real xid is assigned at the first write. readers live on a virtual xid and never cost one.
  • xids are in the order of first writes, not BEGINs.
  • a non-zero xmax can be an aborted xid or a row lock. the infomask flags tell you which.
  • savepoints that write get their own subtransaction xids.