Sub topic 3
Transaction IDs
This chapter assumes you know basic SQL and can use BEGIN, COMMIT, and ROLLBACK in two psql sessions. PostgreSQL uses transaction IDs, or XIDs, to track which transaction created or replaced each row version.
What a transaction ID represents
An XID is an internal number assigned by PostgreSQL to a transaction. The number is not a timestamp and does not identify a client session. XIDs are assigned across the PostgreSQL cluster, not separately for each table.
PostgreSQL assigns an XID when a transaction needs one, usually because it changes data. A read-only transaction might not receive an XID at all. This saves work because readers can often use an existing snapshot without becoming part of transaction-status tracking.
A row version stores the XID of the transaction that created it in xmin. If a later transaction updates or deletes that version, PostgreSQL records the later transaction's XID in xmax. These columns are metadata stored with the row version.
| Value | Meaning |
|---|---|
| `xmin` | Transaction that inserted this row version |
| `xmax` | Transaction that deleted or replaced it, or zero if still current |
| XID | Internal transaction identifier |
XIDs are finite
An XID is a 32-bit value. PostgreSQL eventually reuses the numeric range, so XIDs wrap around rather than increasing forever. PostgreSQL compares transaction IDs within a limited “recent history” window. It cannot safely treat an old numeric value as newer simply by comparing integers.
This creates two requirements:
- PostgreSQL must remove or freeze old row-version metadata before XID comparisons become ambiguous.
- Administrators must monitor transaction age and prevent a database from approaching wraparound danger.
Freezing replaces sufficiently old transaction metadata with a special meaning: the creating transaction is old enough that it can be treated as committed forever. VACUUM performs this work as part of normal maintenance.
Observing an assigned XID
The following example creates a temporary table, checks for an assigned XID, then writes a row and inspects the resulting metadata.
CREATE TEMP TABLE xid_demo (
id integer PRIMARY KEY,
note text NOT NULL
);
BEGIN;
-- A read-only transaction may not have an XID yet.
SELECT pg_current_xact_id_if_assigned();
INSERT INTO xid_demo VALUES (1, 'created by this transaction');
-- The write requires an XID.
SELECT
pg_current_xact_id() AS current_xid,
xmin::text AS row_xmin,
xmax::text AS row_xmax,
ctid,
id,
note
FROM xid_demo;
COMMIT;The row's xmin should identify the transaction that performed the INSERT. Its xmax normally appears as zero because no transaction has deleted or replaced that row version. The exact numeric value depends on activity elsewhere in the cluster.
xmin and xmax have the xid data type. PostgreSQL may display them as numbers, but their meaning depends on transaction status. A number alone cannot tell you whether a transaction committed, aborted, or is still running.
How XIDs fit into visibility
When a query examines a row version, PostgreSQL compares its xmin and xmax with the query's snapshot. It also checks whether those transactions committed. A row version created by an uncommitted transaction is normally invisible to other transactions. A committed old version may remain invisible because a later committed version replaced it.
The next chapters use these XIDs to explain snapshots, visibility rules, and why VACUUM must eventually freeze old metadata.
Exercise
A row version has xmin 500 and xmax 0. What does this tell you, and what does it not tell you?
Show answer
It says transaction 500 created the version and no transaction has recorded a deletion or replacement in xmax. It does not by itself prove that transaction 500 committed or that the version is visible to your query.