In PostgreSQL, SELECT pg_current_snapshot() returns 742:750:742,745; what do the three parts mean, and whose changes does that snapshot see?
answer
- lowest still running, one past newest finished
- the list is what was in flight
- finished is not committed
- your own XID is not listed
basics
~20 sThe format is xmin:xmax:xip_list. XIDs below 742 had finished, 742 and 745 were in progress, 743, 744 and 746 to 749 had finished, and 750 or higher had not. Finished XIDs are seen only if pg_xact says they committed.
solid answer
~40 s`pg_current_snapshot()` returns a `pg_snapshot` printed as `xmin:xmax:xip_list`. `xmin` (742) is the lowest XID still active, so everything below it had finished; `xmax` (750) is one past the highest finished XID, so 750 and above are treated as in progress; the list (742, 745) names the XIDs in flight between them. So this snapshot treats 743, 744 and 746 to 749 as finished, and 742, 745 and 750 onward as invisible. Finished still has to mean committed: a version created by 744 is visible only if `pg_xact` records 744 as committed, which is why `pg_visible_in_snapshot()` answers only half the question. The list holds top-level XIDs only, never subtransaction IDs or the caller's own XID, and the values are 64-bit `xid8`, unlike the 32-bit `xid` stored in tuple headers.
code
sql · 7 linesBEGIN ISOLATION LEVEL REPEATABLE READ;
SELECT pg_current_snapshot(); -- 742:750:742,745
SELECT * FROM pg_snapshot_xip(pg_current_snapshot()); -- 742, 745
SELECT pg_visible_in_snapshot('743'::xid8,
'742:750:742,745'::pg_snapshot); -- true
SELECT pg_current_xact_id_if_assigned(); -- NULL, nothing written yet
COMMIT;go deeper
Recall the xmin:xmax:xip_list format and that a snapshot lists the transactions still running when it was taken.
Decode a snapshot XID by XID, separate finished from committed, and name the gaps: own XID, subtransaction IDs and commit status are not in it.
Use pg_current_xact_id_if_assigned, pg_xact_status and exported snapshots to solve real problems: in-doubt commits, XID burn from read-only code, standby errors.
Judge when an application should lean on XIDs and snapshots, for example in change capture, and where XID order versus commit order makes such a design subtly wrong.
## The snapshot as PostgreSQL stores it A **snapshot** is PostgreSQL's record of which transactions had finished at one instant. `pg_current_snapshot()` returns the active one as a `pg_snapshot` value whose text form is `xmin:xmax:xip_list`: | Part | Meaning | |---|---| | `xmin` | Lowest transaction ID still active. Every XID below it had either committed or rolled back | | `xmax` | One past the highest completed XID. Every XID at or above it had not completed, so its work is invisible | | `xip_list` | XIDs in progress at the time; an XID between `xmin` and `xmax` that is **not** in the list had already completed | The functions return the 64-bit type `xid8`, which carries an epoch and never wraps during an installation's life. Tuple headers store the 32-bit `xid`, which wraps every four billion transactions. ## Reading 742:750:742,745 1. **XIDs below 742** had all finished. 2. **742** was running: its inserts are invisible and its deletes have not happened, as far as this snapshot is concerned. 3. **743 and 744** had finished. 4. **745** was running, like 742. 5. **746 to 749** had finished. 6. **750 and above** had not finished, or had not been assigned yet; treated as running. *Finished* is not *committed*. The snapshot only says whether a transaction was still in flight. For a version with `xmin = 744`, the reader also asks the commit log, `pg_xact`, whether 744 committed or aborted. A version is visible when its creator finished per the snapshot **and** committed per `pg_xact`, and its deleter, if any, did not. A worked check: a tuple has `xmin = 744` and `xmax = 745`. Its creator, 744, is in range and not listed, so it had finished; `pg_xact` says committed, so the version exists for this snapshot. Its deleter, 745, is in the in-progress list, so the delete has not happened as far as this snapshot is concerned, and the row is returned. A second tuple with `xmin = 745`, the replacement that 745 wrote, is invisible for the same reason. One logical row, two versions, and the snapshot picks exactly one of them. If 745 commits a moment later, a snapshot taken after that commit makes the opposite choice. ## What the snapshot leaves out - **Your own transaction.** The caller's XID is never in its own snapshot. Your own changes are recognised separately, and command IDs (`cmin`, `cmax`) decide which of them the current statement sees. - **Subtransaction IDs.** Only top-level XIDs appear. The manual warns that `pg_visible_in_snapshot()` gives the wrong answer for a subtransaction's XID. - **Commit status.** `pg_visible_in_snapshot('743'::xid8, snap)` returns `true` even if 743 aborted, because it checks only the snapshot. - **Timing rules.** When a new snapshot is taken, per statement or per transaction, depends on the isolation level and is covered there. ## The functions around it | Function | Returns | |---|---| | `pg_current_xact_id()` | The top-level `xid8`; **assigns one** if the transaction has none | | `pg_current_xact_id_if_assigned()` | The `xid8`, or `NULL` while the transaction has not written | | `pg_xact_status(xid8)` | `in progress`, `committed`, `aborted`, or `NULL` once the status is no longer retained | | `pg_snapshot_xmin()`, `pg_snapshot_xmax()`, `pg_snapshot_xip()` | The parts of a `pg_snapshot` | | `txid_current()`, `txid_current_snapshot()` | Deprecated pre-13 `bigint` versions of the above, kept for compatibility | A transaction that has only read has just a **virtual transaction ID** such as `4/12532`; a real XID is handed out from a cluster-wide counter at its first write. Calling `pg_current_xact_id()` in read-only code burns an XID for nothing, and on a hot standby it raises an error, because a standby cannot assign XIDs. Use the `_if_assigned` variant there. ## Where this helps in practice - **An in-doubt `COMMIT`.** Record `pg_current_xact_id()` before committing. If the connection drops before the reply, a new session runs `pg_xact_status()` on that value. Prepared transactions show as `in progress`, so check `pg_prepared_xacts` too. - **Debugging a surprising read.** Compare a row's `xmin` with the snapshot's parts to see why a just-committed row is or is not visible to a long transaction. - **Change-capture by XID.** Export `pg_current_snapshot()` with each batch so a consumer can tell which transactions had finished at that point, never trusting XID order as commit order.
- In PostgreSQL, why prefer pg_current_xact_id_if_assigned() over pg_current_xact_id() for logging inside request code?`pg_current_xact_id()` assigns an XID if the transaction has none, so a read-only request consumes one, adding to XID burn, and on a hot standby the call fails because recovery cannot assign XIDs. `pg_current_xact_id_if_assigned()` returns `NULL` until the transaction has written, which is exactly the information logging needs.
- Can PostgreSQL's pg_xact_status() always say whether an old transaction committed?No. It returns `NULL` once the transaction is old enough that its commit status has been discarded, and it reports prepared transactions as `in progress`, so callers must also check `pg_prepared_xacts`. It is designed for recent transactions, such as one whose `COMMIT` reply was lost.
- Every XID between the snapshot's xmin and xmax was still running.
- pg_visible_in_snapshot returning true means the row version is visible.
- The snapshot lists the calling transaction's own XID among the running ones.
- A read-only transaction already has a real XID that pg_current_xact_id simply reports.
- XIDs are handed out at BEGIN, so XID order matches commit order.