Sub topic 4
Reading `xmin`, `xmax`, and `ctid`
This chapter assumes basic SQL and transaction commands such as BEGIN, COMMIT, and ROLLBACK. It also assumes that you can open two psql sessions connected to the same PostgreSQL database.
PostgreSQL stores each table row as a physical tuple. With multiversion concurrency control (MVCC), an update usually creates a new tuple instead of overwriting the old one. PostgreSQL adds hidden system columns to each tuple so it can track these versions.
The three system columns
| Column | Meaning | Important limitation |
|---|---|---|
| `xmin` | Transaction ID that created this tuple version | It does not by itself prove that the transaction committed |
| `xmax` | Transaction ID that deleted or locked this tuple version | A value can represent a lock, deletion, or an aborted action |
| `ctid` | Physical location of the tuple as `(block, item)` | It can change after an update or table rewrite |
You can select these columns by name even though they are not listed in the table definition:
DROP TABLE IF EXISTS accounts;
CREATE TABLE accounts (
id integer PRIMARY KEY,
balance integer NOT NULL
);
INSERT INTO accounts VALUES (1, 100);
SELECT xmin, xmax, ctid, id, balance
FROM accounts;For a newly inserted row, xmin identifies the inserting transaction. xmax is normally zero because no transaction has deleted or locked the version. The ctid might look like (0,1), but its value depends on the table's physical layout.
Transaction IDs are internal identifiers. They increase as transactions are assigned IDs, but they are not permanent business identifiers. PostgreSQL can freeze old row versions, so an old xmin may later appear as a special frozen value.
What an update does
An update creates a replacement tuple. The old tuple remains in the heap until PostgreSQL can remove it safely.
BEGIN;
SELECT pg_current_xact_id() AS updating_transaction;
UPDATE accounts
SET balance = 90
WHERE id = 1;
SELECT xmin, xmax, ctid, id, balance
FROM accounts
WHERE id = 1;
COMMIT;The visible version now has a new ctid, and its xmin identifies the transaction that created it. The old version has its xmax set to the updating transaction. A normal query hides that old version because MVCC visibility rules select only versions valid for the query's snapshot.
The ctid is useful while investigating a table, but it is not a stable row identifier. An update gives the replacement row a different location. Operations such as VACUUM FULL, CLUSTER, and some table rewrites can also change locations. Use the primary key, not ctid, in application queries.
Inspecting physical versions
The pageinspect extension can show tuple headers on a heap page. It requires suitable privileges, commonly superuser access.
CREATE EXTENSION IF NOT EXISTS pageinspect;
SELECT lp, t_xmin, t_xmax, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0))
WHERE t_xmin IS NOT NULL;This output may include both the old and new versions. The exact page and item numbers are physical details and can differ between databases.
Exercise
After an UPDATE, which system column identifies the transaction that created the replacement tuple?
Show answer
The replacement tuple's xmin identifies the transaction that created it.