skip to content

snapshots

xmin, xmax and the in-progress list. three numbers that decide everything your query can see.

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

a snapshot is three things: xmin, the oldest transaction still running when it was taken; xmax, one past the newest one that had finished; and xip, the list of transactions in between that were still running. a transaction's work counts for you if it finished before your snapshot, and that's decided from those three numbers alone.

the picture

you know how a query on a busy table returns a consistent answer, even while other sessions are halfway through changing the rows it reads? nobody locks anything for that. the query just carries a note of which transactions it's allowed to believe.

think of a photo of the departures board at an airport. flights that had landed when you took it (committed), flights still in the air (in progress, the xip list), and flights that hadn't even taken off yet (at or past xmax). you plan your whole trip from the photo. the real board keeps changing; your photo doesn't.

postgres prints a snapshot as xmin:xmax:xip. here's one from a real server, taken by T3 while T1 (xid 759) was still open and T2 (760) had just committed:

snapshot 759:761:759
[ xmin
xmax )

759 invisible: 759 is in the xip list: it was still running at snapshot time. invisible, even if it commits a second later.

xmin..xmax, running at snapshot time (●)writes visible to this snapshotin progressaborted
T3's snapshot, 759:761:759. click an xid. the bracket is xmin to xmax, the dot marks an xid in the in-progress list, and green is work this snapshot can see.
below xmin
finished before anything still running began. committed means visible, aborted means not. no list to check.
in xip
was running when the snapshot was taken. invisible, even if it commits a millisecond later.
between xmin and xmax, not in xip
had finished. visible if it committed.
xmax or above
hadn't started yet, as far as this snapshot knows. invisible, whatever it does later.

notice what a snapshot doesn't say: whether 760 committed or aborted. it only says 760 was done. the outcome comes from the commit log. the visibility rules use both.

reading one

the browser engine is one session, so its snapshots are dull: nothing else is ever running.

sqldb=lab · session snap
SELECT pg_current_snapshot() AS snap,
       pg_snapshot_xmin(pg_current_snapshot()) AS xmin,
       pg_snapshot_xmax(pg_current_snapshot()) AS xmax,
       ARRAY(SELECT pg_snapshot_xip(pg_current_snapshot())) AS xip;
output, from postgres 18.6
SELECT 1
snappg_snapshotxminxid8xmaxxid8xipxid8[]
753:753:753753{}
(1 row)

753:753 with an empty list: everything up to 752 is settled, 753 is the next xid anyone will get. now with an xid of our own:

sqldb=lab · session snap
continues session snap (1 earlier block run first)
BEGIN;
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
SELECT pg_current_snapshot() AS snap, pg_current_xact_id_if_assigned() AS me;
COMMIT;
output, from postgres 18.6
#1 · BEGIN
#2 · UPDATE 1
#3 · SELECT 1
snappg_snapshotmexid8
753:753:753
(1 row)
#4 · COMMIT

still 753:753, and we're 753. a transaction is never in its own xip list, and by the numbers alone our own xid looks like "not started yet". that's why the visibility rules ask "is it mine?" before they look at the snapshot at all.

pg_visible_in_snapshot() runs the snapshot half of the test, so you can check the table above against T3's snapshot from the server:

sqldb=lab · session snap
continues session snap (2 earlier blocks run first)
SELECT x, pg_visible_in_snapshot(x::text::xid8, '759:761:759') AS done_for_t3
FROM (VALUES (758), (759), (760), (761)) AS v(x)
ORDER BY x;
output, from postgres 18.6
SELECT 4
xint4done_for_t3bool
758t
759f
760t
761f
(4 rows)

758 is below xmin, 759 is in the list, 760 is between and done, 761 is at xmax. only 758 and 760 count for T3.

when a snapshot is taken

predict the output
T3 (repeatable read) read alice before T1 committed. T1 commits. T3 reads alice again. what does it see?
sql
/* T1 */ BEGIN; UPDATE accounts SET balance = balance + 10 WHERE id = 1;
/* T3 */ BEGIN ISOLATION LEVEL REPEATABLE READ;
/* T3 */ SELECT balance FROM accounts WHERE id = 1;   -- 100.00
/* T1 */ COMMIT;
/* T3 */ SELECT balance FROM accounts WHERE id = 1;   -- ?

T4 is read committed, so each of its statements gets a new snapshot. here are the two it used, before and after T1 committed:

snapshot 759:761:759
[ xmin
xmax )

759 invisible: 759 is in the xip list: it was still running at snapshot time. invisible, even if it commits a second later.

xmin..xmax, running at snapshot time (●)writes visible to this snapshotin progressaborted
snapshot 761:761:
[ xmin = xmax )

758 visible: 758 < xmin 761: finished before every running transaction began, so its fate is settled. committed: visible.

xmin..xmax, running at snapshot time (●)writes visible to this snapshotin progressaborted
T4's two snapshots, 759:761:759 for its first read and 761:761: for its second. click 759 in each: in the first it's in the in-progress list, in the second it's visible history. the commit statuses are the ones at the end of the capture.

that's the whole difference between the two isolation levels most people use, in terms of snapshots:

  • read committed (the default) takes a fresh snapshot for every statement.
  • repeatable read and serializable take one at the first statement of the transaction and keep it. BEGIN itself doesn't take one. (serializable adds conflict tracking on top; which anomalies each level allows is in isolation levels.)

the capture below has both. T1 is a transfer that stays open, T2 commits a change, then T3 (repeatable read) and T4 (read committed) both read. then T1 commits and both read again.

snapshot of =
T1
snapshot 759:761:
T2
·
T3
snapshot 759:761:759
T4
snapshot 759:761:759
1
BEGIN; UPDATE accounts SET balance = balance + 10 WHERE id = 1→ UPDATE 1
2
BEGIN; UPDATE accounts SET balance = balance - 10 WHERE id = 2→ UPDATE 1
3
COMMIT
4
BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT balance FROM accounts WHERE id = 1→ 100.00
5
BEGIN; SELECT balance FROM accounts WHERE id = 1→ 100.00
T3's snapshot
snapshot 759:761:759
[ xmin
xmax )

759 invisible: 759 is in the xip list: it was still running at snapshot time. invisible, even if it commits a second later.

xmin..xmax, running at snapshot time (●)writes visible to this snapshotin progressaborted

T4, at read committed, reads alice too. its snapshot says the same thing, for now.

step 5/9
real postgres 18.6 output · a native server · traces/snapshot-xip
step through and switch between T3 and T4. T3's snapshot never moves after its first read. T4's moves forward on its next statement, and T1's xid drops out of the in-progress list into the visible past. before a session's first statement there's no snapshot to show.

after T1 commits, the same table read by two sessions, one step apart: T3 gets alice at 100.00, T4 gets 110.00. both are correct for their snapshot. (for a read committed session that's idle, the figure shows the snapshot its next statement would get, which is what the capture's probe read.)

what a snapshot costs

building one means walking every backend's slot in shared memory to find who's running, which is why postgres caches the result until any transaction ends. a snapshot's size is the number of running transactions with xids, not the size of your data. and its xmin matters to everyone else: while your snapshot is alive, nothing that you might still see can be cleaned up. a repeatable read transaction left open for an hour pins that hour of dead versions. that's the xmin horizon.

keylines

  • a snapshot is xmin, xmax and the in-progress list. postgres prints it as xmin:xmax:xip.
  • below xmin: settled. in the list or at xmax and above: invisible. otherwise: visible if committed.
  • read committed: a snapshot per statement. repeatable read: one per transaction, taken by the first query.
  • the snapshot says who was done; the commit log says how it ended.
  • an old snapshot holds back cleanup for everyone.