skip to content

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:

T1INSERT INTO slots VALUES (4, 'yo')
byte 0 header line pointerstuples, byte 8,192
lower 40free 7,760 B, unlitupper 7800
pd_lower
40
pd_upper
7,800
free between them
7,760 B
line pointers
4
lpctidxminxmaxt_ctididnote
(0,1)7540(0,1)1hi
(0,2)7550(0,2)2--------------------------------…
(0,3)7560(0,3)3medium =========================…
(0,4)7570(0,4)4yo
tuple (0,1) · off 8160 · len 31 B
t_xmin754 committed
t_xmax0 nobody deleted it
t_ctid(0,1) itself: the newest version
infomaskHASVARWIDTHXMAX_INVALID
t_hoff24 B
dataid 1note hi

another short one. four slots, four tuples, in the order they arrived.

page headerline pointer → tuplefree space, unused slotchanged in this stepdeleted, waiting for vacuum
step 5/8
real output, postgres 18.6 (PGlite 0.5.12) · page-fill
step through. line pointers grow right from byte 24, tuples grow left from byte 8192, and the dashed gap between pd_lower and pd_upper is the free space. what changed is hot. watch slots 3 and 4 after the VACUUM: their tuples move, their numbers don't.

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:

sqldb=lab
SELECT lsn, checksum, flags, lower, upper, special, pagesize, version, prune_xid
FROM page_header(get_raw_page('accounts', 0));
output, from postgres 18.6
SELECT 1
lsnpg_lsnchecksumint2flagsint2lowerint4upperint4specialint4pagesizeint4versionint2prune_xidxid
0/17C75301144003680728192819240
(1 row)

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:

sqldb=lab
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;
output, from postgres 18.6
SELECT 1
headertextline_pointerstext
0000000030757c01b02c00002400881f0020042000000000d89f4e00b09f4a00889f4e00
(1 row)
fieldraw bytesvalue
pd_lsn0000000030757c010/17C7530
pd_checksumb02c11,440
pd_flags00000
pd_lower240036
pd_upper881f8,072
pd_special00208,192
pd_pagesize_version04208,192 | 4
pd_prune_xid000000000
line pointer 1d89f4e00off 8,152 · flags 1 · len 39
line pointer 2b09f4a00off 8,112 · flags 1 · len 37
line pointer 3889f4e00off 8,072 · flags 1 · len 39
page header, 24 bytesline pointers, 4 bytes each
real output, postgres 18.6 (PGlite 0.5.12) · page-bytes
the same 36 bytes, split into fields. the header is eight fields, little-endian (the low byte first, so 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:

sqldb=lab
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;
output, from postgres 18.6
#1 · SELECT 3
lpint2lp_offint2lp_flagsint2lp_lenint2t_ctidtid
18152139(0,1)
28112137(0,2)
38072139(0,3)
(3 rows)
#2 · SELECT 3
ctidtididint4ownertext
(0,1)1alice
(0,2)2bob
(0,3)3carol
(3 rows)
byte 0 header line pointerstuples, byte 8,192
lower 36free 8,036 B, unlitupper 8072
lpctidxminxmaxt_ctididownerbalance
(0,1)7520(0,1)1alice100.00
(0,2)7520(0,2)2bob100.00
(0,3)7520(0,3)3carol50.00
tuple (0,2) · off 8112 · len 37 B
t_xmin752 committed
t_xmax0 nobody deleted it
t_ctid(0,2) itself: the newest version
infomaskHASVARWIDTHXMAX_INVALID
t_hoff24 B
dataid 2owner bobbalance 100.00
headerline pointer → tuplefree space, unlitdeadin progress
real output, postgres 18.6 (PGlite 0.5.12) · page-bytes
the lab's accounts page, drawn from the same capture as the bytes above. three line pointers right after the header, three tuples at the very end, and nearly 8 KB of free space between them. pick a slot to see its tuple's header.

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:

sqldb=lab
EXPLAIN (COSTS OFF) SELECT owner FROM accounts WHERE ctid = '(0,2)';
SELECT owner FROM accounts WHERE ctid = '(0,2)';
output, from postgres 18.6
#1 · EXPLAIN
Tid Scan on accounts TID Cond: (ctid = '(0,2)'::tid)
#2 · SELECT 1
ownertext
bob
(1 row)

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:

sqldb=lab · session slots
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 1
#3 · INSERT 0 1
#4 · INSERT 0 1
#5 · INSERT 0 1
#6 · DELETE 1
#7 · VACUUM
#8 · SELECT 4
lpint2lp_offint2lp_flagsint2lp_lenint2
18160131
2000
38064196
48032131
(4 rows)

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.

predict the output
now insert one more row. what's its ctid?
sql
INSERT INTO slots VALUES (5, 'new');
SELECT ctid, id FROM slots ORDER BY id;
sqldb=lab · session slots
continues session slots (1 earlier block run first)
INSERT INTO slots VALUES (5, 'new');
SELECT ctid, id FROM slots ORDER BY id;
output, from postgres 18.6
#1 · INSERT 0 1
#2 · SELECT 4
ctidtididint4
(0,1)1
(0,3)3
(0,4)4
(0,2)5
(4 rows)

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:

sqldb=lab
SELECT (ctid::text::point)[0]::int AS page, count(*) AS rows
FROM notes GROUP BY 1 ORDER BY 1;
output, from postgres 18.6
SELECT 5
pageint4rowsint8
097
197
297
397
412
(5 rows)

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:

sqldb=lab
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;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · INSERT 0 600
#3 · SELECT 3
pageint4rowsint8
0291
1291
218
(3 rows)

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:

sqldb=lab
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;
output, from postgres 18.6
SELECT 2
reltextlowerint4upperint4specialint4
accounts3680728192
accounts_pkey3681288176
(2 rows)

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.