Sub topic 6
The Tuple Header: What Every Row Carries
This chapter assumes that you know basic CREATE TABLE, INSERT, UPDATE, and SELECT, and that you have a general idea of transactions and files on disk. The focus is PostgreSQL heap storage, not index entries or SQL result formatting.
A PostgreSQL table stores each row as a heap tuple. A tuple is the row's stored representation. It contains the column values, but it also carries a tuple header. The header records information PostgreSQL needs to find the row and decide whether a transaction can see it.
The fixed header fields
Every heap tuple begins with a fixed header. The important fields are:
| Field | Meaning |
|---|---|
| `t_xmin` | Transaction ID that inserted this row version |
| `t_xmax` | Transaction ID that deleted or replaced this row version, or zero if it is still current |
| `t_ctid` | Physical item pointer for this tuple version, often pointing to a newer version after an update |
| `t_infomask` | Flags describing visibility and tuple properties |
| `t_infomask2` | Flags and metadata such as the number of attributes |
| `t_hoff` | Offset from the tuple start to the user data |
The fixed header is followed by optional parts. A null bitmap appears when at least one column is null. Padding may then be added so that the first column value meets its alignment requirement. The actual column data follows.
The header is not a complete, permanent visibility decision. PostgreSQL also checks transaction status and the snapshot used by the current query. A snapshot is the transaction's view of which other transactions are visible.
`xmin` and `xmax` record row history
PostgreSQL uses multi-version concurrency control, or MVCC. MVCC keeps multiple versions of a row so readers and writers can usually proceed without blocking each other.
When a transaction inserts a row, its transaction ID becomes the row version's xmin. When an update replaces that version, PostgreSQL creates a new tuple. The old tuple gets an xmax containing the updating transaction's ID. A delete also sets xmax, but does not create a replacement tuple.
CREATE TABLE accounts (
id integer PRIMARY KEY,
owner text,
balance numeric
);
BEGIN;
INSERT INTO accounts VALUES (1, 'alice', 100);
SELECT xmin, xmax, ctid, id, owner, balance
FROM accounts;
COMMIT;The xmin and xmax columns in this query are system columns. They expose values derived from the tuple header. The displayed values are transaction IDs, not timestamps. An xmax of zero commonly means that no deleting or replacing transaction has been recorded for that version.
The meaning of a nonzero xmax depends on transaction state. If the transaction is still in progress, a reader may continue to see the old version. If the transaction committed, the old version is no longer visible to snapshots that should see the change. PostgreSQL also handles special cases such as multitransactions, locking-only xmax values, and transaction ID wraparound.
How versions connect
An update normally leaves the old tuple in the heap and inserts a new tuple. The old tuple's t_ctid points to the new tuple's location. The new tuple's t_ctid usually points to itself.
The ctid system column displays a tuple's physical address as (block number, item offset). It is useful for inspection, but it is not a stable row identifier. Updates can change it, and VACUUM can reuse storage after old versions are no longer needed.
Inspecting the header with `pageinspect`
The pageinspect extension exposes low-level page information. Creating it requires suitable database privileges. Run this example in a test database because page contents can change as other transactions modify the table.
CREATE EXTENSION IF NOT EXISTS pageinspect;
CREATE TABLE header_demo (
id integer PRIMARY KEY,
note text
);
INSERT INTO header_demo VALUES (1, 'first');
SELECT ctid, xmin, xmax, id, note
FROM header_demo;
SELECT lp, lp_flags, t_xmin, t_xmax, t_ctid,
t_infomask2, t_infomask, t_hoff
FROM heap_page_items(get_raw_page('header_demo', 0));heap_page_items reports one result for each line pointer on the page. The lp value identifies the line pointer. The t_* columns describe the tuple header. Some rows may be unused, redirected, or already dead, so not every result represents a currently visible SQL row.
Exercise
An old tuple has xmin = 40, xmax = 75, and t_ctid = (3,5). What does this usually indicate after an UPDATE committed by transaction 75?
Show answer
Transaction 40 inserted the old version. Transaction 75 replaced it, and (3,5) identifies the newer tuple version that readers should follow.