In PostgreSQL, a row returned by SELECT xmin, xmax, ctid has a nonzero xmax; what do these columns mean, and why is it visible?
answer
- xmax is a claim, not a verdict
- did that transaction finish?
- locks write to the row too
- foreign keys lock parent rows
basics
~20 sIn PostgreSQL, xmin is the creating XID, xmax the XID that deleted, updated or locked the version, and ctid its address. A visible row keeps a nonzero xmax when that transaction is still running, rolled back, only locked it, or committed after your snapshot.
solid answer
~50 s`xmin` is the XID that created this row version, `xmax` is zero or the XID (or MultiXact ID) of a transaction that deleted, updated or locked it, and `ctid` is the version's `(page, item)` address. `xmax` alone proves nothing: the reader still asks `pg_xact` whether that XID committed and asks its snapshot whether the XID had finished. So a visible row with a nonzero `xmax` usually means one of four things: the deleter or updater is still in progress; it rolled back, and PostgreSQL never clears `xmax` on rollback; the row was only locked, because `SELECT ... FOR UPDATE`, `FOR SHARE` and the `FOR KEY SHARE` locks taken by foreign-key checks record the locker in `xmax` with a lock-only flag; or, under `REPEATABLE READ`, the change committed after your snapshot. `pgrowlocks` or `pageinspect` tells the lock case apart.
code
sql · 11 lines-- Session A
BEGIN;
SELECT id, status FROM order_import_batch WHERE id = 42 FOR UPDATE;
-- Session B, while A is still open
SELECT xmin, xmax, ctid, id, status FROM order_import_batch WHERE id = 42;
-- the row is returned; xmax holds session A's XID
-- Session B: confirm it is only a lock (needs CREATE EXTENSION pgrowlocks)
SELECT locked_row, locker, multi, modes, pids
FROM pgrowlocks('order_import_batch');go deeper
Know that xmin is the creating transaction, xmax the deleting or locking one, and ctid the physical address. A set xmax does not by itself mean the row is gone.
Walk through the four causes of a visible row with xmax set: in-progress writer, rolled-back writer, a lock-only xmax, and a change committed after your snapshot.
Diagnose it on a live system: pgrowlocks for locks, backend_xid for running writers, and the foreign-key KEY SHARE locks that put xmax and MultiXacts on parent rows.
Use these columns to explain to a team why row locks cost writes and MultiXact traffic, and when a schema with heavy foreign-key fan-in needs that load budgeted.
## The system columns on every row Every PostgreSQL table carries hidden **system columns** you can select by name. Three of them describe a row version's life: | Column | Holds | Changes when | |---|---|---| | `xmin` | XID of the transaction that created this version (`INSERT` or `UPDATE`) | Not by later updates; each new version gets its own `xmin` | | `xmax` | `0`, or the XID or MultiXact ID of a transaction that deleted, updated or locked this version | A `DELETE`, `UPDATE` or row lock touches the version | | `ctid` | Physical location `(page, item)` of this version | Every update writes a new version at a new address | Two more, `cmin` and `cmax`, hold **command IDs** inside the creating or deleting transaction, which lets a transaction see its own earlier statements' changes but not the rows the current statement is writing. ## Why a set xmax does not mean deleted `xmax` records a **claim**: some transaction wanted to delete, replace or lock this version. Whether that claim matters to you depends on three questions the visibility check asks: - Is the XID in `xmax` **finished according to my snapshot**? If it was still running when the snapshot was taken, or started after it, its effects are invisible to me. - Did it **commit or abort**? The answer comes from the commit log, `pg_xact`, or from a hint bit cached on the tuple. - Was it **only a lock**? A lock-only `xmax` never hides the row. ## The four usual causes 1. **The deleter or updater is still running.** Until it commits, every other session keeps seeing the row with that XID in `xmax`. 2. **It rolled back.** PostgreSQL does no cleanup on abort. `xmax` keeps the aborted XID; readers learn from `pg_xact` that it aborted and treat the version as live. 3. **The row was locked, not changed.** `SELECT ... FOR UPDATE`, `FOR NO KEY UPDATE`, `FOR SHARE` and `FOR KEY SHARE` store the locker in `xmax` with a lock-only flag; the manual warns that row locking therefore causes disk writes. A foreign-key check on an `INSERT` into a child table takes `FOR KEY SHARE` on the parent row, so parent rows show a nonzero `xmax` even though nobody ran `FOR UPDATE`. When several transactions hold the lock at once, `xmax` holds a **MultiXact ID** naming the group instead of a single XID. Which lock mode conflicts with which is a separate topic. 4. **The change committed after your snapshot.** In a `REPEATABLE READ` or `SERIALIZABLE` transaction, a delete that committed after your snapshot is still invisible to you, so you see the row with the deleter's XID in `xmax`. ## Telling the cases apart - **`pgrowlocks`** (a contrib extension): `SELECT * FROM pgrowlocks('order_import_batch')` lists locked rows with `locker`, `multi`, `xids`, `modes` and `pids`. A row there is case 3. - **`pg_stat_activity.backend_xid`**: if a live session shows that XID, the transaction is still running, case 1 or 3. - **`pageinspect`**: `heap_page_items()` with `heap_tuple_infomask_flags()` decodes the header flags, including the lock-only one; the header layout itself is a storage topic. - **Nothing matches and the row is visible**: the XID finished; it rolled back (case 2), or your snapshot predates its commit (case 4). ## What not to read into these columns - **XID order is not commit order.** An XID is assigned when a transaction first writes, not when it starts or commits, so a lower `xmin` does not mean an earlier commit. - **A transaction that only reads has no XID.** It runs under a virtual transaction ID such as `4/12532`, so its reads never appear in `xmin` or `xmax`. - **`xmax = 0` is not a promise the row is current for you.** A newer version may exist that your snapshot cannot see yet; you are looking at the version your snapshot is entitled to. - **A lock-only `xmax` outlives the lock.** When the locker commits or rolls back, the lock is gone, but its XID stays in the header until the row is updated again or a `VACUUM` that freezes the page clears it. Readers ignore it because the flag says it was only a lock and the locker has finished. ## Why interviewers ask this The question checks whether a candidate has the PostgreSQL model rather than a generic one. A candidate who reads `xmax` as a deletion marker will misdiagnose locked rows as deleted, blame vacuum for rows that are merely locked by foreign-key checks, and expect a rollback to tidy the heap. A candidate who knows that visibility is `xmin`/`xmax` plus snapshot plus `pg_xact` can reason through any of the four cases on the spot.
- In PostgreSQL, can xmin be used as a version token for optimistic concurrency, as in UPDATE ... WHERE id = $1 AND xmin = $2?It works mechanically: every update creates a version with a new `xmin`, so a concurrent change makes the predicate match nothing. The caveats are that `xmin` is a 32-bit `xid` that wraps, it changes on updates the application does not care about, and it is PostgreSQL-only. A dedicated version column is clearer; the optimistic-versus-pessimistic choice itself is another topic.
- Why might every row of a PostgreSQL parent table show a nonzero xmax after a large child-table load?Each foreign-key check on an inserted child row takes `FOR KEY SHARE` on the referenced parent row, and that lock is recorded in the parent tuple's `xmax`. With several loaders referencing the same parent, `xmax` holds a MultiXact ID. The rows are not deleted; the lock-only flag tells readers to ignore it.
- A nonzero xmax means the row has been deleted and is gone for every reader.
- PostgreSQL resets xmax to zero when the deleting transaction rolls back.
- SELECT FOR UPDATE keeps row locks only in memory, so it writes nothing to the table.
- A lower xmin always means that transaction committed earlier.
- Every transaction gets an XID as soon as it starts, even if it only reads.