Sub topic 8
How a Row Version Is Judged Visible
Assume you know basic SQL and can use BEGIN, COMMIT, and ROLLBACK. You should also know how to open two PostgreSQL sessions. This chapter focuses on the rules PostgreSQL uses to decide whether a stored row version belongs in a query result.
The visibility decision
PostgreSQL stores transaction metadata with each heap tuple, or stored row version:
xminis the transaction ID that created the version.xmaxis the transaction ID that deleted the version or placed a relevant lock on it. A value of0means no such transaction is recorded.- The transaction's commit status says whether its work committed, aborted, or is still in progress.
- The query snapshot says which transaction IDs the query is allowed to observe.
A version is visible only when both its creator and its removal status pass the snapshot's rules.
| Tuple state | Typical visibility result |
|---|---|
| Creator committed before the snapshot, and no committed remover is visible | Visible |
| Creator is still running or aborted | Not visible |
| A remover committed before the snapshot | Not visible |
| Creator committed after the snapshot | Not visible |
The snapshot is a view of transaction activity at a particular time. It contains a lower boundary, an upper boundary, and the transaction IDs that were active when the snapshot was taken. PostgreSQL does not simply compare an ID with the boundaries. It also checks whether the transaction was in progress and consults commit status when necessary.
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
INSERT INTO accounts VALUES (1, 100);
BEGIN;
SELECT xmin, xmax, ctid, balance
FROM accounts
WHERE id = 1;
COMMIT;The displayed xmin identifies the inserting transaction, not the session or user. The value is useful for investigation, but transaction IDs are internal values and eventually wrap around. Do not use them as permanent application identifiers.
Applying the rules to an update
An UPDATE creates a new tuple and marks the old tuple as replaced. For a short time, both versions may exist physically. A query chooses the version that is valid for its snapshot.
Suppose transaction A updates a row but has not committed. Other transactions normally continue seeing the previously committed version. After A commits, a new statement under Read Committed can see the new version. A statement whose snapshot was taken earlier still follows that earlier snapshot.
xmax needs careful interpretation. It can identify a deleting or updating transaction, but it can also record row-lock information. Therefore, a nonzero xmax does not by itself prove that the tuple is invisible. PostgreSQL checks the transaction status and the kind of operation represented by the metadata.
A practical mental model
For each tuple, ask these questions in order:
- Did the creator transaction commit?
- Was that committed creation visible to this snapshot?
- Did a deleting or updating transaction replace the tuple?
- If so, did that replacement commit and become visible?
- If the replacement is not visible, should the old tuple remain visible?
This explains why two sessions can query the same table and receive different results without either query being incorrect. They may be using different snapshots.
Exercise
A tuple has a committed creator. Its xmax contains an in-progress transaction that tried to update it. Should a query automatically hide the tuple?
Show answer
No. An in-progress updater is not yet a committed replacement. Visibility depends on the snapshot and transaction status, so the old tuple can remain visible until the update commits.