Sub topic 6
The tuple header: what every row carries
This chapter assumes you have read the earlier chapters on 8 KB pages, line pointers, and ctid. You should be able to run pageinspect queries in psql as a superuser, or as a role that has been granted access to the extension's functions. By the end you will be able to read every field of a row's header, say which bytes belong to the header and which to your data, and explain why a row costs more than the sum of its columns.
Header and data
A heap page stores each row version as a tuple. A tuple has two parts. The header holds bookkeeping that PostgreSQL needs to decide who can see the row. The data holds your column values, packed one after another.
The fixed part of the header is 23 bytes:
| Field | Size | Purpose |
|---|---|---|
| `t_xmin` | 4 bytes | ID of the transaction that created this version |
| `t_xmax` | 4 bytes | ID of the transaction that deleted, updated, or locked it, or 0 |
| `t_cid` (shares space with `t_xvac`) | 4 bytes | Command number inside the creating or deleting transaction |
| `t_ctid` | 6 bytes | Block and offset of this version, or of its newer version |
| `t_infomask2` | 2 bytes | Number of attributes, plus HOT-related flags |
| `t_infomask` | 2 bytes | Flags: has NULLs, has variable-width columns, hint bits, lock state |
| `t_hoff` | 1 byte | Offset from the start of the tuple to the user data |
If the row contains at least one NULL, a null bitmap follows the fixed fields. It has one bit per column. The header is then padded to a multiple of 8 bytes (MAXALIGN on typical 64-bit builds), and t_hoff records where the padding ends. A row with no NULLs therefore has a 24-byte header, with one byte of padding after the 23 fixed bytes.
The cost per row is at least 4 bytes for the line pointer plus 24 bytes for the header, so 28 bytes before any column data. For a table of one million rows, that is about 28 MB of overhead, whatever the columns hold.
Seeing the header
Create a small table with two integer columns and insert two rows in one transaction. The second row has a NULL.
CREATE EXTENSION IF NOT EXISTS pageinspect;
CREATE TABLE hdr_demo (a int, b int);
BEGIN;
INSERT INTO hdr_demo VALUES (1, 10);
INSERT INTO hdr_demo VALUES (2, NULL);
SELECT ctid, xmin, xmax, cmin, a, b FROM hdr_demo;
SELECT lp, lp_len, t_xmin, t_xmax, t_field3 AS t_cid, t_ctid,
to_hex(t_infomask) AS infomask, to_hex(t_infomask2) AS infomask2,
t_hoff, t_bits
FROM heap_page_items(get_raw_page('hdr_demo', 0));
COMMIT;On a fresh table you should see output like this:
- Row 1 has
lp_len = 32: a 24-byte header plus two 4-byte integers.t_hoffis 24 andt_bitsis empty. - Row 2 has
lp_len = 28andt_hoff = 24. The null bitmap takes one byte, and it fits in the byte that would otherwise be padding, so the header does not grow. The row is smaller only because the NULL stores no data.t_bitsshows10000000: column 1 is present, column 2 is NULL, and the remaining bits are unused. - Both rows have the same
t_xmin, the ID of your transaction.t_xmaxis 0. t_cidis 0 for the first insert and 1 for the second. The command counter increments with each statement inside a transaction.t_ctidis(0,1)and(0,2). A tuple that has not been replaced points at itself.
The SQL system columns xmin, xmax, cmin, cmax, and ctid are read directly from these fields. cmin and cmax are two names for the same t_cid slot, so selecting both shows the same number twice in many cases.
The null bitmap does not make the header grow for every NULL. It adds bytes only when the bitmap crosses an alignment boundary. A bitmap of 1 byte makes the header 24 bytes, which still fits in the existing alignment. With 9 columns the bitmap takes 2 bytes, so the header is 25 bytes and is padded to 32. A table that is 8 columns wide and has NULLs in some rows is cheaper than one with 9 columns.
Reading the flag words
The two info masks pack many facts into 16 bits each. Printing them in hexadecimal is hard to read, so use heap_tuple_infomask_flags, available in PostgreSQL 13 and later:
SELECT lp, t_xmin, t_xmax, f.raw_flags, f.combined_flags
FROM heap_page_items(get_raw_page('hdr_demo', 0)) AS h,
LATERAL heap_tuple_infomask_flags(h.t_infomask, h.t_infomask2) AS f;The flags you will meet most often are these:
| Flag | Meaning |
|---|---|
| `HEAP_HASNULL` | The tuple has a null bitmap |
| `HEAP_HASVARWIDTH` | At least one column is variable-length |
| `HEAP_HASEXTERNAL` | At least one value is stored out of line (TOAST) |
| `HEAP_XMIN_COMMITTED` / `HEAP_XMIN_INVALID` | Hint bits about the creating transaction |
| `HEAP_XMAX_COMMITTED` / `HEAP_XMAX_INVALID` | Hint bits about the deleting transaction |
| `HEAP_XMAX_LOCK_ONLY` | `xmax` is a row lock, not a delete |
| `HEAP_XMAX_IS_MULTI` | `xmax` is a multixact ID, because several transactions hold locks |
| `HEAP_UPDATED` | This version was created by an UPDATE |
| `HEAP_HOT_UPDATED` | This version was replaced by a HOT update |
| `HEAP_ONLY_TUPLE` | This version has no index entries, because it was created by a HOT update |
When you ran the query above inside the transaction, the row had no xmax, so HEAP_XMAX_INVALID was set. After the commit, the first reader that checks visibility usually sets HEAP_XMIN_COMMITTED.
Hint bits
The commit status of every transaction lives in a separate structure (pg_xact). Looking it up for each row would be slow. The first time a query finds that the transaction in xmin is committed, it records that fact in the tuple with HEAP_XMIN_COMMITTED. Later readers skip the lookup.
This has two side effects. A plain SELECT can dirty a page, because setting a hint bit is a write to the buffer. So the first read after a large bulk load can be slower and can generate I/O. Also, two copies of a table, for example on a primary and a replica, can differ in their hint bits while holding the same logical data.
What xmax means
A nonzero xmax does not prove that the row is deleted. It records the last transaction that touched the version in a way that needed a marker:
- A
DELETEor anUPDATEsetsxmaxto the transaction's ID. For anUPDATE, the new version gets that ID as itsxmin. SELECT ... FOR UPDATE,FOR SHARE, and foreign key checks also setxmax, together with the lock-only flag.- If the transaction that set
xmaxrolled back, the field keeps its value, but the row is still live. The hint bitHEAP_XMAX_INVALID, or the status inpg_xact, tells readers to ignore it.
A reader decides whether a version is visible by combining xmin, xmax, the hint bits, and its own snapshot. Chapter 8 covers the visibility rules in full. Here it is enough to know that the header holds all the inputs.
ctid inside the header
The t_ctid field points to the newest version of the row when this version has been replaced. After an UPDATE, the old version's t_ctid holds the address of the new version. A reader can follow that chain from an old version to the current one. The newest version points at itself.
The ctid column you select from SQL is different. It is computed from the line pointer's position, not read from the header. For the live version the two agree. For a dead version, only t_ctid in the header shows where the row went.
Related header details
- Frozen rows. To avoid transaction ID wraparound,
VACUUMfreezes old tuples. Since PostgreSQL 9.4 it does this by setting bothHEAP_XMIN_COMMITTEDandHEAP_XMIN_INVALID, andt_xminkeeps its original value. A frozen row is visible to all snapshots. - Combo command IDs. If one transaction creates and deletes the same row, both
cminandcmaxare needed, but there is only one slot. PostgreSQL stores a combo ID and setsHEAP_COMBOCID. The real pair lives in backend memory, so it is not visible in the page. t_xvac. This is a legacy use of the same slot, from the pre-9.0VACUUM FULL. You can ignore it on current versions.- No OID. Older versions could add a 4-byte object ID to the header with
WITH OIDS. That option was removed in PostgreSQL 12.
Common mistakes
- Estimating row size from column types alone. Add 24 bytes of header, plus 4 for the line pointer, plus alignment padding between columns.
- Reading
xmax != 0as "deleted". Check the lock and hint flags first. - Treating
xminas a timestamp or a global counter. It is a 32-bit transaction ID that wraps around.pg_xact_commit_timestampgives commit times, but only iftrack_commit_timestampis on. - Expecting
pageinspectto show only committed data. It shows every tuple in the page, including uncommitted and dead ones. That is the point of the tool, but it can mislead you if you compare the output with a normal query. - Forgetting that
get_raw_pagereads the buffer. The result reflects the page as it is in shared memory at that moment, so a concurrent session can change it between two calls.
Exercise
A table has a single bigint column and no NULLs. What are lp_len and t_hoff for a row, how many bytes does the row use on the page including its line pointer, and what is the largest number of rows that fit on one 8192-byte page with a 24-byte page header (ignoring fillfactor)?
Show answer
t_hoff is 24 (23 fixed bytes plus 1 byte of padding), and lp_len is 24 + 8 = 32. The row uses 32 bytes of tuple plus 4 bytes of line pointer, which is 36 bytes. The usable space is 8192 - 24 = 8168 bytes, and 8168 / 36 = 226.9, so at most 226 rows fit.
Exercise
You select a row and see xmax = 4821, but the row is still returned by a normal query. Name two explanations.
Show answer
The transaction 4821 may have rolled back, so its delete or update never took effect. Or 4821 may be holding a row lock (for example from SELECT ... FOR UPDATE) and not have deleted anything. Both cases are distinguishable through the xmax flags, HEAP_XMAX_INVALID for an aborted transaction and HEAP_XMAX_LOCK_ONLY for a lock.
Exercise
A table has 9 columns and a row contains one NULL. What is t_hoff for that row, and why?
Show answer
t_hoff is 32. The null bitmap needs 9 bits, which is 2 bytes. The fixed header of 23 bytes plus 2 bytes of bitmap is 25 bytes, and this is padded up to the next multiple of 8, which is 32.