Sub topic 7
Snapshots: What a Query Can See
This chapter assumes basic SQL, transactions, BEGIN/COMMIT/ROLLBACK, and the ability to use two psql sessions. You should also understand that an UPDATE creates a new row version instead of replacing the old version in place.
A snapshot is a visibility boundary
A PostgreSQL snapshot is the transaction-time view used to decide which row versions a query may see. It is not a copy of the table. It is metadata about transaction IDs, combined with commit-status information.
When a query starts, PostgreSQL records a snapshot containing:
xmin: the oldest transaction ID that was still active.xmax: the first transaction ID that had not yet been assigned.xip_list: transaction IDs that were active betweenxminandxmax.
A transaction ID in xip_list is still in progress from the snapshot's point of view. A transaction that commits after the snapshot was taken is also invisible to that snapshot.
For normal transaction IDs, the simplified interpretation is:
| Transaction ID position | Snapshot interpretation |
|---|---|
| Less than `xmin` | It finished before the snapshot and can be visible if committed |
| In `xip_list` | It was active and is not visible |
| At least `xmax` | It had not started when the snapshot was taken |
The commit status still matters. A transaction ID below xmin may have committed or aborted. PostgreSQL checks that status before accepting a row version.
Observing a snapshot
You can inspect the current snapshot with pg_current_snapshot(). The result uses the form xmin:xmax:xip_list. The values vary between databases and are not meaningful as fixed numbers.
DROP TABLE IF EXISTS snapshot_demo;
CREATE TABLE snapshot_demo (id integer PRIMARY KEY, note text);
INSERT INTO snapshot_demo VALUES (1, 'initial');
BEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT pg_current_snapshot() AS snapshot;
SELECT * FROM snapshot_demo;
COMMIT;To observe the timing more clearly, use two sessions:
- In session A, start a transaction and run
SELECT pg_current_snapshot();. - In session B, insert a row and commit.
- In session A, query the table again.
Under Repeatable Read, session A keeps its original snapshot, so it does not see the row committed by session B. The snapshot defines the query's past, even though the row exists when the second query runs.
How snapshots work with row versions
Suppose an old row version was created by transaction 100 and a new version was created by transaction 105. A snapshot may see the old version when transaction 100 committed before the snapshot and transaction 105 was still active. After transaction 105 commits, a newer snapshot can see the new version instead.
This process lets readers continue while another transaction writes. Readers do not wait for every writer to finish. They select the row version whose creating and deleting transaction IDs are valid for their snapshot.
Exercise
Session A takes a snapshot while transaction 200 is still active. Transaction 200 then commits. Can a later query in session A see rows created by transaction 200?
Show answer
No, not when session A continues using that same snapshot. A new snapshot would be needed to see the committed rows.