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:
| id | what |
|---|---|
| 1 | plain old row |
| 3 | deleted, still running |
| 4 | deleted, rolled back |
| 8 | locked, not deleted |
| 9 | deleted by R itself |
T1 deletes row 2 and commits, before anyone else starts.
pick a version, and pick who's reading:
- 1xmin aborted?no
- 2xmin is me?no
- 3xmin not done at snapshot time?no
- 4no xmax, or xmax only locked it?yes → visible
- 5xmax is me?yes → invisible
- 6xmax aborted?yes → visible
- 7xmax not done at snapshot time?yes → visible
- 8otherwise: deleted before my snapshotyes → invisible
(0,1) visible: xmin 758 committed before the snapshot, and nobody deleted it.
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:
- xmin aborted? then this version never existed. invisible. row 5: inserted, then rolled back.
- 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.
- 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).
- no xmax, or xmax only locked it? visible. row 1, and row 8 (locked FOR UPDATE, not deleted).
- xmax is me? I deleted it. invisible. row 9.
- xmax aborted? the delete never happened. visible. row 4.
- xmax not done at snapshot time? the delete hasn't happened for me yet. visible. row 3, deleted by T3, which is still running.
- 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.
| lp | ctid | xmin | xmax | t_ctid | id | what |
|---|---|---|---|---|---|---|
| (0,1) | 758 | 0 | (0,1) | 1 | plain old row | |
| (0,2) | 758 | 759 | (0,2) | 2 | deleted, committed | |
| (0,3) | 758 | 761 | (0,3) | 3 | deleted, still running | |
| (0,4) | 758 | 760 | (0,4) | 4 | deleted, rolled back | |
| (0,5) | 758 | 762 | (0,5) | 8 | locked, not deleted | |
| (0,6) | 758 | 765 | (0,6) | 9 | deleted by R itself | |
| (0,7) | 760 | 0 | (0,7) | 5 | inserted, rolled back | |
| (0,8) | 761 | 0 | (0,8) | 6 | inserted, still running | |
| (0,9) | 763 | 0 | (0,9) | 11 | inserted, committed | |
| (0,10) | 764 | 0 | (0,10) | 7 | inserted after the snapshot | |
| (0,11) | 765 | 0 | (0,11) | 10 | inserted by R itself |
visible to R. xmin 758 committed; xmax 762 only locked the row, it didn't delete it.
/* 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:
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;| ctidtid | xminxid | xmaxxid | ownertext |
|---|---|---|---|
| (0,1) | 752 | 0 | alice |
| (0,2) | 752 | 0 | bob |
| (0,4) | 753 | 0 | dave |
| lpint2 | t_xminxid | t_xmaxxid |
|---|---|---|
| 1 | 752 | 0 |
| 2 | 752 | 0 |
| 3 | 752 | 753 |
| 4 | 753 | 0 |
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.
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;| countint8 |
|---|
| 6 |
| cmincid | xminxid | nint4 |
|---|---|---|
| 0 | 756 | 100 |
| 1 | 756 | 200 |
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.