skip to content

the visibility rules

is this version visible to me? the whole decision as a flowchart, checked against real rows.

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

for every tuple a query touches, postgres answers one question: is this version visible to me? it looks at the tuple's xmin and xmax, asks the commit log how those transactions ended, and checks them against the reader's snapshot. eight checks, always in the same order, and the first one that applies decides.

the picture

you know how a page can hold three versions of the same row, and a query still returns exactly one? something picks. it isn't the newest version, or the oldest. it's whichever one this reader's snapshot says was current.

think of a ledger where every line has "written by" and "struck out by" in the margin, and a reader holding a list of whose signatures to trust. a line counts if its writer is trusted and its striker isn't. that's it. the eight checks are just that sentence with the edge cases spelled out: aborted writers, your own lines, strikes that are only bookmarks (row locks).

the capture behind this chapter builds one row per case on a real postgres 18.6 server. three sessions write, commit, roll back or stay open, and then a repeatable read transaction R takes its snapshot, writes a little itself, and reads the whole table:

T1
·
T2
·
T3
·
R
·
1
BEGIN; DELETE FROM cases WHERE id = 2; COMMIT
committed in the database
idwhat
1plain old row
3deleted, still running
4deleted, rolled back
8locked, not deleted
9deleted by R itself

T1 deletes row 2 and commits, before anyone else starts.

step 1/9
real output, a postgres 18.6 server · visibility-cases
how the cases were made. step through: T3 and T2 are still open when R takes its snapshot, and T1's last insert commits after it. the side panel shows what's committed after each step.

pick a version, and pick who's reading:

reader =
snapshot 761:764:761,762 · me = 765
version =
  1. 1xmin aborted?no
  2. 2xmin is me?no
  3. 3xmin not done at snapshot time?no
  4. 4no xmax, or xmax only locked it?yes → visible
  5. 5xmax is me?yes → invisible
  6. 6xmax aborted?yes → visible
  7. 7xmax not done at snapshot time?yes → visible
  8. 8otherwise: deleted before my snapshotyes → invisible

(0,1) visible: xmin 758 committed before the snapshot, and nobody deleted it.

predicted(0,1)(0,3)(0,4)(0,5)(0,9)(0,11)
postgres said(0,1)(0,3)(0,4)(0,5)(0,9)(0,11)
same 6 versions: the model agrees with the server for R.
the check that decidesvisible to this readerinvisiblectid
model: src/lib/db/visibility.ts · checked against real postgres 18.6 output · a native server · traces/visibility-cases
pick a version and a reader. the checks run top to bottom; the hot one decides. underneath, every version's prediction next to what postgres actually returned to that reader. "a new transaction" is the capture's rows probe, a fresh transaction reading the same page.

the predictions come from src/lib/db/visibility.ts, this book's model of postgres's HeapTupleSatisfiesMVCC(), and they match the server for both readers: six rows for R, seven for a new transaction, not the same six and seven. the model leaves one thing out (command ids, below), which is why it's a model.

the eight checks

in the order postgres runs them, with the row from the capture that hits each one:

  1. xmin aborted? then this version never existed. invisible. row 5: inserted, then rolled back.
  2. xmin is me? my own insert: visible, unless I also deleted it. row 10 visible, row 9 (inserted by someone else, deleted by R) is check 5.
  3. xmin not done at snapshot time? in the snapshot's in-progress list, or at or past its xmax: the insert hasn't happened for me. invisible. row 6 (T3 still running) and row 7 (committed after the snapshot).
  4. no xmax, or xmax only locked it? visible. row 1, and row 8 (locked FOR UPDATE, not deleted).
  5. xmax is me? I deleted it. invisible. row 9.
  6. xmax aborted? the delete never happened. visible. row 4.
  7. xmax not done at snapshot time? the delete hasn't happened for me yet. visible. row 3, deleted by T3, which is still running.
  8. otherwise: deleted by a transaction that committed before my snapshot. invisible. row 2.

R's snapshot was 761:764:761,762. T3 (761) and T2 (762) were running, 763 had committed, and 764 committed after R started. that's why row 11 (by 763) is visible to R and row 7 (by 764) isn't, though both committed: one was done when R looked, the other wasn't.

byte 0 header line pointerstuples, byte 8,192
lower 68free 7,548 B, unlitupper 7616
lpctidxminxmaxt_ctididwhat
(0,1)7580(0,1)1plain old row
(0,2)758759(0,2)2deleted, committed
(0,3)758761(0,3)3deleted, still running
(0,4)758760(0,4)4deleted, rolled back
(0,5)758762(0,5)8locked, not deleted
(0,6)758765(0,6)9deleted by R itself
(0,7)7600(0,7)5inserted, rolled back
(0,8)7610(0,8)6inserted, still running
(0,9)7630(0,9)11inserted, committed
(0,10)7640(0,10)7inserted after the snapshot
(0,11)7650(0,11)10inserted by R itself
tuple (0,5) · off 7936 · len 48 B
t_xmin758 committed
t_xmax762 in progress
t_ctid(0,5) itself: the newest version
infomaskHASVARWIDTHXMAX_EXCL_LOCKXMAX_LOCK_ONLYXMIN_COMMITTEDKEYS_UPDATED
t_hoff24 B
dataid 8what locked, not deleted

visible to R. xmin 758 committed; xmax 762 only locked the row, it didn't delete it.

headerline pointer → tuplefree space, unlitvisible to the snapshotdeadin progress
real postgres 18.6 output · a native server · traces/visibility-cases, R's read
the same page as R saw it: green is visible to R's snapshot. pick a slot for its header: xmin and xmax by commit status, and the infomask flags, including the hint bits earlier readers left behind.
predict the output
T3 deleted row 3 and is still running. a brand new transaction reads the table. does it see row 3?
sql
/* T3 */ BEGIN; DELETE FROM cases WHERE id = 3;   -- still open
/* new */ SELECT * FROM cases WHERE id = 3;        -- ?

your own changes

checks 2 and 5 are the only ones the browser engine can show on its own. inside one transaction:

sqldb=lab · session mine
BEGIN;
INSERT INTO accounts VALUES (4, 'dave', 10.00);
DELETE FROM accounts WHERE id = 3;
SELECT ctid, xmin, xmax, owner FROM accounts ORDER BY id;
SELECT lp, t_xmin, t_xmax FROM heap_page_items(get_raw_page('accounts', 0)) ORDER BY lp;
COMMIT;
output, from postgres 18.6
#1 · BEGIN
#2 · INSERT 0 1
#3 · DELETE 1
#4 · SELECT 3
ctidtidxminxidxmaxxidownertext
(0,1)7520alice
(0,2)7520bob
(0,4)7530dave
(3 rows)
#5 · SELECT 4
lpint2t_xminxidt_xmaxxid
17520
27520
3752753
47530
(4 rows)
#6 · COMMIT

the page has four tuples. our SELECT returns three: dave, because his xmin is us (check 2), and not carol, because her xmax is us (check 5). no snapshot was consulted for either; "is it mine?" comes first.

the part the model leaves out: command ids

"mine" isn't the whole story. a statement must not see the rows it is itself writing, or INSERT INTO t SELECT * FROM t would chase its own tail forever. postgres numbers the statements inside a transaction (command ids), stores the inserting one in the tuple as cmin, and a version written by a later command of your own transaction is invisible to an earlier one.

sqldb=lab · session cid
CREATE TABLE nums (n int);
INSERT INTO nums VALUES (1), (2), (3);
INSERT INTO nums SELECT n + 10 FROM nums;
SELECT count(*) FROM nums;
BEGIN;
INSERT INTO nums VALUES (100);
INSERT INTO nums VALUES (200);
SELECT cmin, xmin, n FROM nums WHERE n >= 100 ORDER BY n;
COMMIT;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 3
#3 · INSERT 0 3
#4 · SELECT 1
countint8
6
(1 row)
#5 · BEGIN
#6 · INSERT 0 1
#7 · INSERT 0 1
#8 · SELECT 2
cmincidxminxidnint4
0756100
1756200
(2 rows)
#9 · COMMIT

the doubling insert read the 3 rows that existed when it started and added 3, not more. and inside the transaction, the two inserts got command ids 0 and 1. visibility.ts treats every write of your own transaction as visible, which is only wrong mid-statement, and none of the captured readers is.

the shortcut: hint bits

every check that asks "aborted?" or "committed?" is a lookup in the commit log, pg_xact, and doing that for every tuple of every scan would be slow. so the first reader to find out writes the answer into the tuple header: HEAP_XMIN_COMMITTED, HEAP_XMIN_INVALID, HEAP_XMAX_COMMITTED, HEAP_XMAX_INVALID. in the page above, the tuples written in setup carry HEAP_XMIN_COMMITTED; row 5's tuple carries HEAP_XMIN_INVALID (aborted); the ones by still-running transactions carry nothing yet. readers check the hint bits first and only fall back to the commit log when they're missing. that, and why it makes a SELECT write to disk, is the commit log and hint bits.

the rules here are what makes isolation levels work. read committed is these checks with a new snapshot per statement; repeatable read and serializable are the same checks with one snapshot per transaction, plus, for serializable, extra bookkeeping on top (SSI).

keylines

  • visible = xmin committed before my snapshot (or is me) and xmax didn't commit before it.
  • eight checks in a fixed order, and the first one that applies decides.
  • "mine" comes before the snapshot. command ids decide within a transaction.
  • a row lock in xmax doesn't hide the row. an aborted xmax doesn't either.
  • the model's predictions match the real server, row for row, for two different readers.