the 8 KB page
a header, line pointers growing down, tuples growing up, free space in the middle. ctid, finally explained.
- address
- everything.storage.the_page
- part
- part 12, pages and the buffer pool
- chapter
- chapter 3 of 12
- reading time
- 9 min
a table is a file of 8 KB pages. every page is laid out the same way: a 24-byte header, an array of 4-byte line pointers growing from the front, the tuples packed in from the back, and the free space in the middle.
a row's address, its ctid, is (page number, line pointer number). not a byte offset. that one level of indirection is what lets postgres move tuples around inside a page without breaking a single index.
the picture
you know how ctid shows up as (0,1) and looks like a coordinate? it is one, just not the one you'd guess. the second number isn't "the first row on the page" or "byte 1". it's the number of an entry in a little table of contents at the top of the page.
think of a page in a notebook where you keep recipes. you write a contents list from the top down (the line pointers), one short line per recipe saying where on the page it starts and how long it is. the recipes themselves you write from the bottom up (the tuples). the page is full when the two meet in the middle. and when you tell a friend "recipe 3 on page 0", you mean contents entry 3, so you're free to recopy recipe 3 somewhere else on the page later, as long as you fix its contents line.
here is that, for real. an empty table, rows of different widths going in one at a time, then a delete, a VACUUM and one more insert. every byte offset is from the browser's engine, captured as it ran:
| lp | ctid | xmin | xmax | t_ctid | id | note |
|---|---|---|---|---|---|---|
| (0,1) | 754 | 0 | (0,1) | 1 | hi | |
| (0,2) | 755 | 0 | (0,2) | 2 | --------------------------------… | |
| (0,3) | 756 | 0 | (0,3) | 3 | medium =========================… | |
| (0,4) | 757 | 0 | (0,4) | 4 | yo |
another short one. four slots, four tuples, in the order they arrived.
four things to take from it. a row's line pointer is always 4 bytes, however wide the row. the tuples are packed tight against the end of the page, newest lowest. a delete moves nothing. and VACUUM can slide tuples around to close a hole, because everything outside the page refers to them by slot number, never by offset.
the header
every page starts with the same 24 bytes. page_header() from pageinspect decodes them:
SELECT lsn, checksum, flags, lower, upper, special, pagesize, version, prune_xid
FROM page_header(get_raw_page('accounts', 0));| lsnpg_lsn | checksumint2 | flagsint2 | lowerint4 | upperint4 | specialint4 | pagesizeint4 | versionint2 | prune_xidxid |
|---|---|---|---|---|---|---|---|---|
| 0/17C7530 | 11440 | 0 | 36 | 8072 | 8192 | 8192 | 4 | 0 |
lower and upper are the two ends of the free space. lower is 36: the 24-byte header plus three 4-byte line pointers. upper is 8,072: where the lowest tuple starts. so this page has 8,036 bytes free in the middle. special is 8,192, the very end of the page: heap pages don't use the special space, index pages do (more below). lsn is the position in the write-ahead log of the last change to this page, which is how recovery knows whether a logged change already made it onto the page.
those are just bytes, though. here are the raw ones:
SELECT encode(substr(get_raw_page('accounts', 0), 1, 24), 'hex') AS header,
encode(substr(get_raw_page('accounts', 0), 25, 12), 'hex') AS line_pointers;| headertext | line_pointerstext |
|---|---|
| 0000000030757c01b02c00002400881f0020042000000000 | d89f4e00b09f4a00889f4e00 |
| byte | field | raw bytes | value |
|---|---|---|---|
| 0 | pd_lsn the WAL position of the last change to this page. page_header() says 0/17C7530 | 0000000030757c01 | 0/17C7530 |
| 8 | pd_checksum a checksum of the page, computed when it's written out | b02c | 11,440 |
| 10 | pd_flags a few hint bits about the whole page | 0000 | 0 |
| 12 | pd_lower where the line pointer array ends | 2400 | 36 |
| 14 | pd_upper where the tuples start | 881f | 8,072 |
| 16 | pd_special where the special space starts. 8192 means none: heap pages don't use it | 0020 | 8,192 |
| 18 | pd_pagesize_version page size and layout version, in one field | 0420 | 8,192 | 4 |
| 20 | pd_prune_xid the oldest xid that might have left something to prune. 0: nothing | 00000000 | 0 |
| 24 | line pointer 1 15 bits of offset, 2 bits of flags (1 = normal), 15 bits of length | d89f4e00 | off 8,152 · flags 1 · len 39 |
| 28 | line pointer 2 15 bits of offset, 2 bits of flags (1 = normal), 15 bits of length | b09f4a00 | off 8,112 · flags 1 · len 37 |
| 32 | line pointer 3 15 bits of offset, 2 bits of flags (1 = normal), 15 bits of length | 889f4e00 | off 8,072 · flags 1 · len 39 |
2400 is 0x0024 = 36). each line pointer packs an offset, two flag bits and a length into 32 bits.the checksum is computed when the page is written out to its file, which is why it's sitting there even though nobody asked for it. checksums are on by default since postgres 18 pg 18+, and the lab engine has them on too.
line pointers, and what ctid means
heap_page_items() reads the line pointer array and follows each pointer to its tuple:
SELECT lp, lp_off, lp_flags, lp_len, t_ctid
FROM heap_page_items(get_raw_page('accounts', 0))
ORDER BY lp;
SELECT ctid, id, owner FROM accounts ORDER BY id;| lpint2 | lp_offint2 | lp_flagsint2 | lp_lenint2 | t_ctidtid |
|---|---|---|---|---|
| 1 | 8152 | 1 | 39 | (0,1) |
| 2 | 8112 | 1 | 37 | (0,2) |
| 3 | 8072 | 1 | 39 | (0,3) |
| ctidtid | idint4 | ownertext |
|---|---|---|
| (0,1) | 1 | alice |
| (0,2) | 2 | bob |
| (0,3) | 3 | carol |
| lp | ctid | xmin | xmax | t_ctid | id | owner | balance |
|---|---|---|---|---|---|---|---|
| (0,1) | 752 | 0 | (0,1) | 1 | alice | 100.00 | |
| (0,2) | 752 | 0 | (0,2) | 2 | bob | 100.00 | |
| (0,3) | 752 | 0 | (0,3) | 3 | carol | 50.00 |
slot 1 points at byte 8,152 and says the tuple there is 39 bytes long. slot 2 points at 8,112, slot 3 at 8,072. alice is 39 bytes, bob 37, carol 39 (bob has a shorter name), and each tuple starts on an 8-byte boundary, so they sit 40 bytes apart.
and there's the ctid: (0,1) is "page 0, line pointer 1". that's the address everything else uses. an index entry for alice holds (0,1). a Tid Scan goes straight there:
EXPLAIN (COSTS OFF) SELECT owner FROM accounts WHERE ctid = '(0,2)';
SELECT owner FROM accounts WHERE ctid = '(0,2)';| ownertext |
|---|
| bob |
page 0, slot 2: one page read, one hop through the line pointer, bob.
lp_flags is 1 for a normal tuple. the other values are what a slot becomes later in life: 0 unused (free to reuse), 2 a redirect (it points at another slot, part of a HOT chain), 3 dead (the tuple is gone, but an index might still point here).
why the indirection
the figure did this, and here it is as a query you can run. four rows, delete the second, vacuum, insert a fifth:
CREATE TABLE slots (id int, note text);
INSERT INTO slots VALUES (1, 'hi');
INSERT INTO slots VALUES (2, repeat('-', 200));
INSERT INTO slots VALUES (3, 'medium ' || repeat('=', 60));
INSERT INTO slots VALUES (4, 'yo');
DELETE FROM slots WHERE id = 2;
VACUUM slots;
SELECT lp, lp_off, lp_flags, lp_len FROM heap_page_items(get_raw_page('slots', 0)) ORDER BY lp;| lpint2 | lp_offint2 | lp_flagsint2 | lp_lenint2 |
|---|---|---|---|
| 1 | 8160 | 1 | 31 |
| 2 | 0 | 0 | 0 |
| 3 | 8064 | 1 | 96 |
| 4 | 8032 | 1 | 31 |
slot 2 is unused now (flags 0, no offset). and slots 3 and 4 point somewhere new: their tuples were at 7,832 and 7,800 before the VACUUM, and now they're at 8,064 and 8,032, pushed up against slot 1's tuple so the free space is one gap again. an index on slots would still say (0,3) and (0,4), and it would still be right. nobody outside the page had to hear about the move.
INSERT INTO slots VALUES (5, 'new');
SELECT ctid, id FROM slots ORDER BY id;INSERT INTO slots VALUES (5, 'new');
SELECT ctid, id FROM slots ORDER BY id;| ctidtid | idint4 |
|---|---|
| (0,1) | 1 |
| (0,3) | 3 |
| (0,4) | 4 |
| (0,2) | 5 |
how many rows fit
a tuple costs its own length (rounded up to 8 bytes) plus its 4-byte line pointer. the lab's notes rows are short, an int and about 50 characters of text:
SELECT (ctid::text::point)[0]::int AS page, count(*) AS rows
FROM notes GROUP BY 1 ORDER BY 1;| pageint4 | rowsint8 |
|---|---|
| 0 | 97 |
| 1 | 97 |
| 2 | 97 |
| 3 | 97 |
| 4 | 12 |
97 to a page, and the 400th row spills onto a fifth page. and there's a ceiling no matter how small the rows get. a table with no columns at all still has a 24-byte tuple header and a 4-byte line pointer per row:
CREATE TABLE nothing ();
INSERT INTO nothing SELECT FROM generate_series(1, 600);
SELECT (ctid::text::point)[0]::int AS page, count(*) AS rows
FROM nothing GROUP BY 1 ORDER BY 1;| pageint4 | rowsint8 |
|---|---|
| 0 | 291 |
| 1 | 291 |
| 2 | 18 |
291 rows, and not one more. (8,192 − 24) / (24 + 4) = 291.7, and postgres sizes its per-page arrays for exactly that: 291 is the most line pointers a heap page can ever have. what the 24 bytes of a tuple header hold, and why wider rows waste more than you'd think, is the next chapter.
not just tables
indexes are files of pages too, with the same header and the same line pointers. the difference is the special space at the end, which heap pages leave empty. a b-tree page keeps its sibling links and its level there:
SELECT 'accounts' AS rel, lower, upper, special FROM page_header(get_raw_page('accounts', 0))
UNION ALL
SELECT 'accounts_pkey', lower, upper, special FROM page_header(get_raw_page('accounts_pkey', 1))
ORDER BY rel;| reltext | lowerint4 | upperint4 | specialint4 |
|---|---|---|---|
| accounts | 36 | 8072 | 8192 |
| accounts_pkey | 36 | 8128 | 8176 |
the index page ends 16 bytes early: special is 8,176. how b-tree pages use the rest is in a real b-tree, page by page.
keylines
- a page is 8 KB: a 24-byte header, line pointers growing from the front, tuples from the back, free space between pd_lower and pd_upper.
- a line pointer is 4 bytes: a 15-bit offset, 2 flag bits, a 15-bit length.
- ctid = (page, line pointer). indexes store it, so tuples can move inside a page and nothing outside notices.
- a deleted row's slot gets reused after VACUUM, which is why ctids aren't in insertion order.
- at most 291 tuples fit on a heap page, however small they are.