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:
759 invisible: 759 is in the xip list: it was still running at snapshot time. invisible, even if it commits a second later.
- 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.
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;| snappg_snapshot | xminxid8 | xmaxxid8 | xipxid8[] |
|---|---|---|---|
| 753:753: | 753 | 753 | {} |
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:
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;| snappg_snapshot | mexid8 |
|---|---|
| 753:753: | 753 |
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:
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;| xint4 | done_for_t3bool |
|---|---|
| 758 | t |
| 759 | f |
| 760 | t |
| 761 | f |
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
/* 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:
759 invisible: 759 is in the xip list: it was still running at snapshot time. invisible, even if it commits a second later.
758 visible: 758 < xmin 761: finished before every running transaction began, so its fate is settled. committed: visible.
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.
BEGINitself 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.
759 invisible: 759 is in the xip list: it was still running at snapshot time. invisible, even if it commits a second later.
T4, at read committed, reads alice too. its snapshot says the same thing, for now.
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.