Sub topic 7
Snapshots: what a query can see
A snapshot is the data structure PostgreSQL uses to decide which transactions a query counts as finished. The earlier chapters covered row versions, transaction IDs (XIDs) and the xmin and xmax columns. This chapter assumes you know those, and that you can open two psql sessions against one database. It shows how a snapshot turns those columns into a visible set of rows, and when PostgreSQL takes one.
What a snapshot records
A snapshot is a short description of which transactions were still running when it was taken. It does not copy any data. It holds three things:
xmin: the lowest XID that was still active. Every XID below it had already finished, either committed or aborted.xmax: the first XID not yet assigned. Every XID at or above it had not started yet.xip_list: the XIDs betweenxminandxmaxthat were in progress.
You can read the current snapshot with pg_current_snapshot(). Its text form is xmin:xmax:xip_list.
SELECT pg_current_snapshot();
-- example result: 748:752:748,750This example says that XIDs below 748 are finished. XIDs 748 and 750 are still running. XID 749 has finished, even though it is higher than 748. XID 751 has finished or was never given an XID to run. XID 752 and above has not started.
The visibility test for one XID
Given a snapshot, any XID falls into one of three cases:
| XID compared with the snapshot | Meaning | Changes visible? |
|---|---|---|
| Below `xmin` | Finished before the snapshot | Yes, if it committed |
| Between `xmin` and `xmax`, not in `xip_list` | Finished before the snapshot | Yes, if it committed |
| In `xip_list` | Running when the snapshot was taken | No |
| At or above `xmax` | Started after the snapshot | No |
A snapshot answers only "was this transaction finished when I looked?" It does not say whether the transaction committed. PostgreSQL gets that from the commit status records, which a later chapter covers. A finished transaction that aborted is still invisible.
Since PostgreSQL 13 you can run this test yourself with pg_visible_in_snapshot(). It takes an xid8 and a pg_snapshot. You can write a snapshot as a literal, which makes it easy to experiment:
SELECT
pg_visible_in_snapshot('747'::xid8, '748:752:748,750'::pg_snapshot) AS xid_747,
pg_visible_in_snapshot('749'::xid8, '748:752:748,750'::pg_snapshot) AS xid_749,
pg_visible_in_snapshot('750'::xid8, '748:752:748,750'::pg_snapshot) AS xid_750,
pg_visible_in_snapshot('752'::xid8, '748:752:748,750'::pg_snapshot) AS xid_752;The result is true, true, false, false. The function reports only whether the transaction had completed, so a completed transaction that aborted would still return true.
How a query uses the snapshot
For each row version, the executor reads xmin and xmax from the tuple header and applies the test above. A version is visible to the query when both of these hold:
- Its creator (
xmin) is visible and committed. - Its deleter (
xmax) is missing, aborted, or not visible in the snapshot.
The diagram shows the creator side of the check.
The deleter check runs the same way on xmax, with the result reversed. If the deleting transaction counts as committed in your snapshot, the version is gone for you. If it is still running, aborted, or started after your snapshot, you still see the row.
Your own transaction is a special case. You see your own earlier changes even though your XID is not committed. PostgreSQL tracks a command counter inside the transaction, so a statement does not see rows that it creates itself. It sees only rows from earlier statements.
A worked example
Use this table. It matches the examples from earlier chapters.
CREATE TABLE accounts (id int PRIMARY KEY, balance int NOT NULL);
INSERT INTO accounts VALUES (1, 100);Run the steps in the order shown.
-- Session A
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT pg_current_snapshot(); -- the snapshot is taken here
SELECT balance FROM accounts WHERE id = 1; -- 100
-- Session B
UPDATE accounts SET balance = 200 WHERE id = 1; -- autocommits
-- Session A
SELECT pg_current_snapshot(); -- same value as before
SELECT balance FROM accounts WHERE id = 1; -- still 100
COMMIT;Session B's update gets an XID that is at or above the xmax of A's snapshot. A's snapshot says that transaction has not started. The new row version is invisible to A, and the old version's xmax points at a transaction A treats as not started, so A still sees the old version.
If you run Session A at Read Committed, the second SELECT returns 200. Each statement then gets a new snapshot, and B's XID is below the new xmax.
When the snapshot is taken
The isolation level controls only the timing of the snapshot. The visibility test is the same in every level.
| Isolation level | Snapshot timing |
|---|---|
| Read Committed | A new snapshot at the start of every statement |
| Repeatable Read | One snapshot at the first query that is not a transaction control command, reused until the end |
| Serializable | Same as Repeatable Read, plus conflict tracking |
Two details often surprise people:
BEGINdoes not take the snapshot. The snapshot is taken by the first real query. If you runBEGIN ISOLATION LEVEL REPEATABLE READ;and wait ten minutes before your firstSELECT, the snapshot reflects the database ten minutes later.- At Read Committed, a statement that is waiting on a row lock can still see newer data. When the lock is released, PostgreSQL re-checks that one row. The next chapter covers this.
Snapshots and XID assignment
A transaction that has only read data does not need an XID. PostgreSQL assigns one lazily, at the first write, or when you ask for it with pg_current_xact_id(). This has two effects.
- A read-only transaction never appears in anyone's
xip_list. Other snapshots do not need to track it. - The
xminof a snapshot is not an XID that you can always find inpg_stat_activity.backend_xid. Each backend also reportsbackend_xmin, which is the oldest XID its snapshot still needs. Vacuum uses that value as the horizon for removing dead rows. Long-running transactions hold it back, which a later chapter explains.
SELECT pid, state, backend_xid, backend_xmin
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL;Common errors
- Expecting
BEGINto freeze the view. At Repeatable Read the view freezes at the first query, not atBEGIN. - Reading
xip_listas "all uncommitted transactions". It lists transactions that were running at snapshot time and lie betweenxminandxmax. A transaction that aborted before the snapshot is not listed, and it is still invisible because of its commit status. - Assuming a visible XID means a committed one. The snapshot test and the commit status test are two separate checks.
- Comparing XIDs as plain numbers across long periods. The 32-bit
xidtype wraps around. Thexid8type used bypg_current_xact_id()andpg_snapshothas 64 bits and does not wrap, which is why those functions are safer for experiments.
Exercise
A snapshot reads 100:105:101,103. Which of the XIDs 99, 101, 102, 103 and 105 are finished from this snapshot's point of view? Which one can never be visible, whatever its commit status, and why?
Show answer
XIDs 99 and 102 are finished. 99 is below xmin. 102 lies between xmin and xmax and is not in xip_list. XIDs 101 and 103 were running when the snapshot was taken, so their changes are invisible. XID 105 equals xmax, so it had not started yet, and its changes are invisible too. A finished XID can still be invisible if it aborted, because the commit status check comes after the snapshot test.
Exercise
In Session A you run BEGIN ISOLATION LEVEL REPEATABLE READ and then wait. Session B commits an update to a row. Then Session A runs its first SELECT on that row. Does A see B's update? What changes if A ran SELECT 1 before B committed?
Show answer
A sees B's update. The snapshot is taken at the first query, and B had already committed, so its XID is below the new snapshot's xmax and not in xip_list. If A ran SELECT 1 before B committed, the snapshot was taken then, and A would not see the update for the rest of the transaction.