Sub topic 4
Reading xmin, xmax and ctid
This chapter assumes you know what a row version (tuple) is, that every writing transaction gets a transaction ID (XID), and how to open two psql sessions on the same database. Here you learn to read the three system columns that tie these ideas together, and to avoid the misreadings they invite.
The hidden columns
Every table has system columns that SELECT * does not return. You must name them explicitly. Three of them identify a row version:
| Column | Type | Meaning |
|---|---|---|
| `xmin` | `xid` | ID of the transaction that created this version |
| `xmax` | `xid` | ID of the transaction that deleted or locked this version, or `0` if none has |
| `ctid` | `tid` | Physical location of the version, as `(page, item)` |
Two more exist: tableoid, the OID of the table that holds the row, and cmin/cmax, which order commands inside one transaction. This chapter does not use cmin and cmax.
CREATE TABLE accounts (id int PRIMARY KEY, balance int NOT NULL);
INSERT INTO accounts VALUES (1, 100);
SELECT xmin, xmax, ctid, * FROM accounts;Typical output (your numbers will differ):
xmin | xmax | ctid | id | balance
------+------+-------+----+---------
748 | 0 | (0,1) | 1 | 100Transaction 748 created this version, no transaction has deleted or locked it, and it sits on page 0, item 1.
xmin: who created this version
xmin is the XID of the transaction that performed the INSERT, or the UPDATE that wrote this version as the new copy. It never changes during the life of the version.
xmin is a 32-bit xid. The function pg_current_xact_id() returns a 64-bit xid8 that includes an epoch counter. To compare the two, cast the result to xid.
xmax: who deleted or locked this version
xmax is 0 while nobody has acted on the version. A nonzero value only says that some transaction tried to delete or lock the row. It does not say the row is gone. A nonzero xmax can mean any of these:
- A committed
DELETEorUPDATE. The version is dead for new snapshots. - A
DELETEorUPDATEstill in progress. Other sessions still see the version. - A
DELETEorUPDATEthat rolled back. The stalexmaxstays in the tuple and is ignored. - A row lock taken by
SELECT ... FOR UPDATEorFOR SHARE. The row is not deleted.
When several transactions lock the same row, PostgreSQL stores a MultiXact ID in xmax instead of a plain XID.
You can watch a lock appear in xmax. In session A:
BEGIN;
SELECT pg_current_xact_id()::xid;
SELECT * FROM accounts WHERE id = 1 FOR UPDATE;Now in session B, while A is still open:
SELECT xmin, xmax, ctid, * FROM accounts;Session B sees the row as normal, but xmax equals session A's XID. Nothing was deleted. After A runs ROLLBACK, the stale xmax may remain on the tuple, and PostgreSQL ignores it once it knows A is not running.
ctid: where the version lives
ctid is the tuple identifier: a page number (counted from 0) and an item number within that page (counted from 1). The item number refers to a line pointer, a slot at the start of the page that points to the tuple's bytes. Because it is a physical address, you can fetch a row by it:
SELECT * FROM accounts WHERE ctid = '(0,1)';That is useful for debugging, but never treat ctid as a row identifier. It changes whenever a new version is written, and VACUUM FULL or CLUSTER rewrite the table and move rows. After vacuum frees a slot, a later insert can reuse the same ctid for a different row. On a partitioned table, ctid is unique only within one partition, so pair it with tableoid.
Watching an UPDATE create a new version
An UPDATE does not edit the row in place. It writes a new version and marks the old one.
BEGIN;
SELECT pg_current_xact_id()::xid;
UPDATE accounts SET balance = 90 WHERE id = 1;
SELECT xmin, xmax, ctid, * FROM accounts;
COMMIT; pg_current_xact_id
--------------------
749
xmin | xmax | ctid | id | balance
------+------+-------+----+---------
749 | 0 | (0,2) | 1 | 90The visible row now has xmin equal to the current XID, xmax of 0, and a new ctid. The old version still exists. You cannot see it with ordinary SQL, but you can with pageinspect.
Seeing both versions with pageinspect
The pageinspect extension reads raw heap pages. By default only a superuser can create it and call its functions.
CREATE EXTENSION IF NOT EXISTS pageinspect;
SELECT lp, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0)); lp | t_xmin | t_xmax | t_ctid
----+--------+--------+--------
1 | 748 | 749 | (0,2)
2 | 749 | 0 | (0,2)lp is the line pointer number, which is the item half of ctid. Read the two rows like this:
- Item 1, the old version, was created by 748. Transaction 749 set its
t_xmax, and itst_ctidpoints to item 2. - Item 2, the new version, was created by 749. Its
t_xmaxis 0, and itst_ctidpoints to itself, which marks it as the newest version.
The chain of t_ctid pointers is how PostgreSQL follows an old version to its successor. It uses this when a concurrent update must find the latest version of a row.
Interpreting combinations
When you see a version in pageinspect or in a query, these combinations cover most cases. The commit status of the XIDs matters, and the visibility chapter covers exactly how it is checked.
| xmin transaction | xmax | Meaning |
|---|---|---|
| Committed | 0 | Live version, visible to snapshots that started after the commit |
| In progress | 0 | Visible only to the transaction that created it |
| Rolled back | any | Never visible. Vacuum can reclaim it |
| Committed | Committed deleter or updater | Dead for new snapshots. Older snapshots may still see it |
| Committed | In progress deleter | Still visible to other sessions |
| Committed | Rolled back | Live. The `xmax` is stale |
| Committed | Locker only | Live, and the row is locked |
Common mistakes
- Reading nonzero
xmaxas "deleted". Check whether that transaction committed, and whether it deleted or only locked. - Using
ctidas a key. It changes on every update. Use a primary key. - Comparing
xminwithpg_current_xact_id()without a cast. One isxid, the otherxid8. - Expecting a read-only transaction to have an XID. PostgreSQL assigns an XID lazily, at the first write or when you call
pg_current_xact_id(). Calling that function inside an experiment changes what you are measuring only slightly, but note that it allocates an XID. - Reading
pageinspectoutput after vacuum. Vacuum may have removed the old version, so item 1 can be gone or reused.
Practice
Exercise
A query returns a row with xmax = 812. A colleague says the row has been deleted. List two other explanations you would check before agreeing.
Show answer
Transaction 812 may still be in progress, so other sessions still see the row. It may have rolled back, leaving a stale xmax that PostgreSQL ignores. It may also have only locked the row, for example with SELECT ... FOR UPDATE. You need to know whether 812 committed, and whether it deleted or only locked the row.
Exercise
After an UPDATE on one row, heappageitems shows item 1 with txmax = 900 and tctid = (0,3), and item 3 with txmin = 900 and tctid = (0,3). Which item is the current version, and how can you tell?
Show answer
Item 3. Its tctid points to itself and its txmax is 0 (the same transaction 900 created it), which marks it as the newest version. Item 1 is the older version, which transaction 900 marked through txmax, and its tctid points forward to item 3.