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.
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:
SELECT ctid, xmin, xmax, id, owner, balance FROM accounts ORDER BY id;| ctidtid | xminxid | xmaxxid | idint4 | ownertext | balancenumeric |
|---|---|---|---|---|---|
| (0,1) | 752 | 0 | 1 | alice | 100.00 |
| (0,2) | 752 | 0 | 2 | bob | 100.00 |
| (0,3) | 752 | 0 | 3 | carol | 50.00 |
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:
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;| my_xidxid8 |
|---|
| null |
| ownertext |
|---|
| alice |
| my_xidxid8 |
|---|
| null |
| my_xidxid8 |
|---|
| 753 |
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:
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;| locktypetext | virtualxidtext | transactionidxid | modetext |
|---|---|---|---|
| virtualxid | 0/5 | null | ExclusiveLock |
| locktypetext | virtualxidtext | transactionidxid | modetext |
|---|---|---|---|
| transactionid | null | 754 | ExclusiveLock |
| virtualxid | 0/5 | null | ExclusiveLock |
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.
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.
| lp | ctid | xmin | xmax | t_ctid | id | owner | balance |
|---|---|---|---|---|---|---|---|
| (0,1) | 758 | 0 | (0,1) | 1 | alice | 100.00 | |
| (0,2) | 758 | 0 | (0,2) | 2 | bob | 100.00 |
- T1ExclusiveLock4/4
T1 begins. it holds a virtual xid, a number only this server's lock table knows. no real xid yet.
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.
| lp | ctid | xmin | xmax | t_ctid | id | owner | balance |
|---|---|---|---|---|---|---|---|
| (0,1) | 758 | 760 | (0,4) | 1 | alice | 100.00 | |
| (0,2) | 758 | 759 | (0,3) | 2 | bob | 100.00 | |
| (0,3) | 759 | 0 | (0,3) | 2 | bob | 90.00 | |
| (0,4) | 760 | 0 | (0,4) | 1 | alice | 110.00 |
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:
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;| ownertext |
|---|
| carol |
| ctidtid | xminxid | xmaxxid | ownertext |
|---|---|---|---|
| (0,3) | 752 | 755 | carol |
| lpint2 | t_xmaxxid | raw_flagstext[] |
|---|---|---|
| 3 | 755 |
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):
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;| xminxid | idint4 | ownertext |
|---|---|---|
| 756 | 4 | dave |
| 757 | 5 | erin |
| xminxid | idint4 | ownertext |
|---|---|---|
| 756 | 4 | dave |
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.