inside a tuple
a 23-byte header, the null bitmap, alignment padding, and why column order changes table size.
- address
- everything.storage.tuple_layout
- part
- part 12, pages and the buffer pool
- chapter
- chapter 4 of 12
- reading time
- 8 min
every tuple starts with a 23-byte header (who wrote it, who deleted it, where the next version is, a few flag words), rounded up to 24. then a null bitmap, only if some column is NULL. then the columns, each one starting at a multiple of its type's alignment, with padding bytes in front if it has to.
so a bool followed by a bigint wastes 7 bytes, every row. put the wide columns first and the padding goes away.
the picture
you know how a table with four tiny columns somehow takes 60 bytes a row? it's not the data. it's the header, and the gaps.
think of a shelf with marks every 8 cm, and a rule that a big box (a bigint) has to start on a mark. put a matchbox (a bool) first and the big box can't go right after it: it waits for the next mark, and 7 cm of shelf stays empty. put the two big boxes first and the matchboxes after them, and nothing is wasted.
here are the same four columns in two orders, straight off the page:
56 bytes for loose, 42 for tight, same values. 15 of loose's 56 bytes are padding: one rounds the 23-byte header up to 24, and 7 sit in front of each int8. and the one with a NULL is the smallest of all: a NULL costs a bit, not a column.
the header
the 23 bytes are the same for every heap tuple. heap_page_items() shows them for the lab's accounts:
SELECT lp, t_xmin, t_xmax, t_field3 AS t_cid, t_ctid, t_infomask2, t_infomask, t_hoff, t_bits
FROM heap_page_items(get_raw_page('accounts', 0))
ORDER BY lp;| lpint2 | t_xminxid | t_xmaxxid | t_cidint4 | t_ctidtid | t_infomask2int4 | t_infomaskint4 | t_hoffint2 | t_bitstext |
|---|---|---|---|---|---|---|---|---|
| 1 | 752 | 0 | 207 | (0,1) | 3 | 2050 | 24 | null |
| 2 | 752 | 0 | 207 | (0,2) | 3 | 2050 | 24 | null |
| 3 | 752 | 0 | 207 | (0,3) | 3 | 2050 | 24 | null |
- t_xmin, t_xmax (4 + 4 bytes)
- the transaction that inserted this version, and the one that deleted or locked it (0 if none). they're what visibility is decided on.
- t_cid (4 bytes)
- which command inside the inserting transaction wrote it, so a statement doesn't see its own new rows. 207 here: the lab's data load ran a few hundred statements in one transaction.
- t_ctid (6 bytes)
- 4 bytes of page number, 2 of slot. itself, or the newer version after an update.
- t_infomask2 (2 bytes)
- the number of columns (3 here) in the low bits, and the HOT flags.
- t_infomask (2 bytes)
- flags: has NULLs, has variable-width columns, and the commit hint bits.
- t_hoff (1 byte)
- where the data starts: the header plus bitmap, rounded up to 8. 24 here.
t_infomask 2050 is two flags, and pageinspect can name them:
SELECT lp, raw_flags
FROM heap_page_items(get_raw_page('accounts', 0)),
heap_tuple_infomask_flags(t_infomask, t_infomask2)
ORDER BY lp;| lpint2 | raw_flagstext[] |
|---|---|
| 1 | {HEAP_HASVARWIDTH,HEAP_XMAX_INVALID} |
| 2 | {HEAP_HASVARWIDTH,HEAP_XMAX_INVALID} |
| 3 | {HEAP_HASVARWIDTH,HEAP_XMAX_INVALID} |
HEAP_HASVARWIDTH (there's a text column) and HEAP_XMAX_INVALID (nobody deleted it). no HEAP_HASNULL, so no bitmap, so t_bits is NULL and the data starts right at byte 24.
alignment
every type has an alignment in the catalog, typalign: c is 1 byte (can start anywhere), s is 2, i is 4, d is 8.
SELECT typname, typlen, typalign
FROM pg_type
WHERE typname IN ('bool', 'int2', 'int4', 'int8', 'float8', 'timestamptz', 'uuid', 'text', 'numeric')
ORDER BY typlen, typname;| typnamename | typlenint2 | typalignchar |
|---|---|---|
| numeric | -1 | i |
| text | -1 | i |
| bool | 1 | c |
| int2 | 2 | s |
| int4 | 4 | i |
| float8 | 8 | d |
| int8 | 8 | d |
| timestamptz | 8 | d |
| uuid | 16 | c |
a column starts at the next multiple of its alignment, counted from the start of the tuple. so in loose (a bool, b int8, c bool, d int8): a goes at byte 24, b can't start before 32, c goes at 40, d can't start before 48. 7 + 7 bytes of nothing. reorder the columns biggest-alignment first and they pack:
CREATE TABLE loose (a bool, b int8, c bool, d int8);
CREATE TABLE tight (b int8, d int8, a bool, c bool);
INSERT INTO loose VALUES (true, 1, false, 2), (true, NULL, false, 2);
INSERT INTO tight VALUES (1, 2, true, false);
SELECT 'loose' AS t, lp, lp_len, t_hoff, t_bits FROM heap_page_items(get_raw_page('loose', 0))
UNION ALL
SELECT 'tight', lp, lp_len, t_hoff, t_bits FROM heap_page_items(get_raw_page('tight', 0))
ORDER BY t, lp;| ttext | lpint2 | lp_lenint2 | t_hoffint2 | t_bitstext |
|---|---|---|---|---|
| loose | 1 | 56 | 24 | null |
| loose | 2 | 40 | 24 | 10110000 |
| tight | 1 | 42 | 24 | null |
14 bytes a row doesn't sound like much.
INSERT INTO loose SELECT true, g, false, g FROM generate_series(1, 10000) AS g;
INSERT INTO tight SELECT g, g, true, false FROM generate_series(1, 10000) AS g;
SELECT pg_relation_size('loose'), pg_relation_size('tight');CREATE TABLE loose (a bool, b int8, c bool, d int8);
CREATE TABLE tight (b int8, d int8, a bool, c bool);
INSERT INTO loose SELECT true, g, false, g FROM generate_series(1, 10000) AS g;
INSERT INTO tight SELECT g, g, true, false FROM generate_series(1, 10000) AS g;
SELECT pg_relation_size('loose') AS loose_bytes, pg_relation_size('tight') AS tight_bytes,
round(100.0 * pg_relation_size('loose') / pg_relation_size('tight') - 100, 1) AS pct_bigger;| loose_bytesint8 | tight_bytesint8 | pct_biggernumeric |
|---|---|---|
| 606208 | 524288 | 15.6 |
NULLs cost a bit
a NULL column has no bytes in the data at all. what it needs is the null bitmap: one bit per column, 1 for "has a value", stored right after the 23-byte header. and there's a free byte there already, the one that rounds 23 up to 24. so up to 8 columns, the bitmap fits in the padding and a row with NULLs is never bigger than one without. the 9th column needs a second byte, and t_hoff jumps to 32:
CREATE TABLE eight (c1 int, c2 int, c3 int, c4 int, c5 int, c6 int, c7 int, c8 int);
CREATE TABLE nine (c1 int, c2 int, c3 int, c4 int, c5 int, c6 int, c7 int, c8 int, c9 int);
INSERT INTO eight VALUES (1, 2, 3, 4, 5, 6, 7, 8), (1, 2, 3, 4, 5, 6, 7, NULL);
INSERT INTO nine VALUES (1, 2, 3, 4, 5, 6, 7, 8, 9), (1, 2, 3, 4, 5, 6, 7, 8, NULL);
SELECT 'eight' AS t, lp, lp_len, t_hoff, t_bits FROM heap_page_items(get_raw_page('eight', 0))
UNION ALL
SELECT 'nine', lp, lp_len, t_hoff, t_bits FROM heap_page_items(get_raw_page('nine', 0))
ORDER BY t, lp;| ttext | lpint2 | lp_lenint2 | t_hoffint2 | t_bitstext |
|---|---|---|---|---|
| eight | 1 | 56 | 24 | null |
| eight | 2 | 52 | 24 | 11111110 |
| nine | 1 | 60 | 24 | null |
| nine | 2 | 64 | 32 | 1111111100000000 |
with eight columns, the NULL row is 4 bytes smaller (one int fewer). with nine, it's 4 bytes bigger: the bitmap needed 2 bytes, the header grew from 24 to 32, and losing one int didn't pay for that. the bitmap itself is always as wide as the table, NULLs or not, once any column in that row is NULL.
variable-width columns
text, numeric, jsonb and friends are varlena: a length header, then the bytes. a short value (under 127 bytes) gets a 1-byte header and no alignment at all; longer ones get 4 bytes and int alignment. so 'alice' is 6 bytes on the page:
SELECT owner, octet_length(owner) AS chars, pg_column_size(owner) AS stored,
balance, pg_column_size(balance) AS numeric_stored
FROM accounts ORDER BY id;| ownertext | charsint4 | storedint4 | balancenumeric | numeric_storedint4 |
|---|---|---|---|---|
| alice | 5 | 6 | 100.00 | 5 |
| bob | 3 | 4 | 100.00 | 5 |
| carol | 5 | 6 | 50.00 | 5 |
one byte of header for each name, and numeric 100.00 in 5 bytes. a value of a couple of kilobytes or more is another story: it gets compressed, or moved out of the row entirely. that's TOAST.
keylines
- every tuple has a 23-byte header (xmin, xmax, cid, ctid, two flag words, t_hoff), rounded up to 24.
- each column starts at a multiple of its typalign: 1, 2, 4 or 8 bytes. the gaps are padding.
- put 8-byte columns first and bools last, and the padding mostly goes away.
- a NULL takes no data bytes, only a bit in the bitmap, which fits in the header's spare byte up to 8 columns.
- short varlena values (under 127 bytes) have a 1-byte header and no alignment.