skip to content

a real b-tree, page by page

bt_metap, bt_page_items, high keys and sibling links, read off a 10,000-row index.

address
everything.index_internals.nbtree_pages
part
part 13, inside indexes
chapter
chapter 2 of 12
reading time
10 min

a postgres b-tree is a file of 8 KB pages. page 0 is the metapage, which says where the root is. every other page is a root, inner or leaf page with the same layout: a header, line pointers, index tuples, and a 16-byte special space holding the page's level and its left and right neighbours. a non-rightmost page's first item is its high key. pageinspect's bt_metap, bt_page_stats and bt_page_items read all of it.

the picture

you know the B+tree shape: separators on top, keys in linked leaves. that's the drawing. on disk it's just a numbered list of pages in one file, and the tree lives in pointers between them: downlinks that name a child page, sibling links that name a neighbour.

think of a binder of loose sheets (the index file). sheet 0 is a sticky note on the cover (the metapage): "the table of contents is on sheet 3". the contents sheet (the root) says which sheet each range starts on. every sheet also has a footer (the special space) with "previous sheet, next sheet", so you can flip through in order even though the sheets were filed in whatever order they were written.

here's a real one: the primary key of shop's orders, 10,000 ids.

1… 364 more366367367… 364 more7327339517… 364 more988298839883… 116 more10000367… 25 more9883rootleaves
child and sibling linkshigh key

orders_pkey as shop builds it: the metapage says the root is page 3, one level above the leaves.

real output, postgres 18.6 (PGlite 0.5.12) · orders-pkey
orders_pkey: a root (page 3) over 28 leaves, drawn with a few keys per page. every leaf but the last ends in its high key, and every page links to its right neighbour. notice the page numbers: leaves 1 and 2, then the root, then leaves 4 to 29.

page 3 is the root because of how CREATE INDEX builds: it writes leaves left to right, and when leaf 2 fills it starts the level above on the next free page. the page numbers say nothing about the order, only the links do.

the metapage

sqldb=shop · ext=pageinspect · session pk
SELECT * FROM bt_metap('orders_pkey');
output, from postgres 18.6
SELECT 1
magicint4versionint4rootint8levelint8fastrootint8fastlevelint8last_cleanup_num_delpagesint8last_cleanup_num_tuplesfloat8allequalimagebool
340322431310-1t
(1 row)

page 0 never holds keys. root is 3 and level is 1: the root is one level above the leaves, so this is a two-level tree, and a lookup reads page 0, page 3 and one leaf. fastroot is a shortcut for trees whose top levels have shrunk to a single page each after deletions; here it's the root itself. allequalimage says every key type in the index can be compared byte for byte, which is what lets the index deduplicate. (magic and version are a format stamp: 340322 is nbtree, and version 4 is the format since postgres 12.)

every page at once

bt_multi_page_stats pg 16+ reads a range of pages (-1 means to the end):

sqldb=shop · ext=pageinspect · session pk
continues session pk (1 earlier block run first)
SELECT blkno, type, live_items, avg_item_size, free_size,
       btpo_prev AS prev, btpo_next AS next, btpo_level AS level
FROM bt_multi_page_stats('orders_pkey', 1, -1)
ORDER BY blkno;
output, from postgres 18.6
SELECT 29
blknoint8typecharlive_itemsint4avg_item_sizeint4free_sizeint4prevint8nextint8levelint8
1l36716808020
2l36716808140
3r28157596001
4l36716808250
5l36716808460
6l36716808570
(29 rows)showing 6 ·

28 leaves (l) with 367 items each and the root (r) with 28, one per leaf. each full leaf has 808 bytes free: CREATE INDEX filled them to the default leaf fillfactor of 90 and stopped, so the first inserts after it have somewhere to go. the last leaf, page 29, is the leftover: 118 items. the prev and next columns are the sibling links, and they're the true order: 1 → 2 → 4 → 5 → … → 29.

predict the output
leaf 1 of orders_pkey has 367 items. how many order ids does it hold?
sql
SELECT live_items FROM bt_page_stats('orders_pkey', 1);  -- 367

inside a leaf

a b-tree page has the same skeleton as a heap page: line pointers growing down from the 24-byte header, tuples growing up from the end, free space in between. the end is different: 16 bytes of special space that heap pages leave empty.

sqldb=shop · ext=pageinspect · session pk
continues session pk (2 earlier blocks run first)
SELECT p.blkno, h.lower, h.upper, h.special, s.live_items, s.free_size, s.btpo_flags
FROM (VALUES (1), (3), (29)) AS p(blkno),
     page_header(get_raw_page('orders_pkey', p.blkno)) AS h,
     bt_page_stats('orders_pkey', p.blkno) AS s
ORDER BY p.blkno;
output, from postgres 18.6
SELECT 3
blknoint4lowerint4upperint4specialint4live_itemsint4free_sizeint4btpo_flagsint4
11492230481763678081
3136773681762875962
294966288817611857881
(3 rows)
leaf, page 1 · 367 itemsfree 812 of 8,192 bytes
014928192
root, page 3 · 28 itemsfree 7,600 of 8,192 bytes
13677368192
rightmost leaf, page 29 · 118 itemsfree 5,792 of 8,192 bytes
49662888192
■ header and special space■ line pointers, 4 bytes each■ index tuples▭ free
real output, postgres 18.6 (PGlite 0.5.12) · the run block above
three pages of orders_pkey, to scale. a full leaf is mostly index tuples (16 bytes each) and line pointers (4 bytes each); the root is nearly empty; the last leaf is about a third full. all three end in the same 16 bytes of special space.

the arithmetic is exact: 24 bytes of header plus 367 line pointers of 4 bytes is 1,492, which is lower. 367 tuples of 16 bytes is 5,872, and 8,176 minus that is 2,304, which is upper. a 16-byte tuple is an 8-byte index tuple header (the heap address, plus flags and a length) and the int4 key padded to 8. (free_size is 4 bytes less than upper minus lower: room for one more line pointer is held back.) btpo_flags is 1 for a leaf and 2 for the root. the special space holds btpo_prev, btpo_next, the level, those flags, and a cycle id that VACUUM uses to notice pages that split under it.

now the items themselves:

sqldb=shop · ext=pageinspect · session pk
continues session pk (3 earlier blocks run first)
SELECT itemoffset, ctid, itemlen, htid, data
FROM bt_page_items('orders_pkey', 1)
WHERE itemoffset <= 3 OR itemoffset = 367
ORDER BY itemoffset;
output, from postgres 18.6
SELECT 4
itemoffsetint2ctidtiditemlenint2htidtiddatatext
1(3,1)16null6f 01 00 00 00 00 00 00
2(0,1)16(0,1)01 00 00 00 00 00 00 00
3(0,2)16(0,2)02 00 00 00 00 00 00 00
367(3,5)16(3,5)6e 01 00 00 00 00 00 00
(4 rows)

item 1 is the high key: 6f 01 00 00 is 367 (little-endian, padded to 8 bytes), and it has no htid because it isn't a row. items 2 to 367 are ids 1 to 366, each with the heap address of its row in htid. (on a leaf, ctid is that same address. on a high key it's not an address, and on a posting-list tuple, below, it means something else again, which is why pageinspect shows htid too.)

inside an inner page

sqldb=shop · ext=pageinspect · session pk
continues session pk (4 earlier blocks run first)
SELECT itemoffset, ctid AS downlink, itemlen, data
FROM bt_page_items('orders_pkey', 3)
WHERE itemoffset <= 4 OR itemoffset = 28
ORDER BY itemoffset;
output, from postgres 18.6
SELECT 5
itemoffsetint2downlinktiditemlenint2datatext
1(1,0)8''
2(2,1)166f 01 00 00 00 00 00 00
3(4,1)16dd 02 00 00 00 00 00 00
4(5,1)164b 04 00 00 00 00 00 00
28(29,1)169b 26 00 00 00 00 00 00
(5 rows)
root, page 3
−∞ → page 11367 → page 22733 → page 431099 → page 54… 9883 → page 2928
real output, postgres 18.6 (PGlite 0.5.12) · the run block above
the root's items in slot order. slot 1 is the minus infinity downlink to page 1, with no key. every other item is a separator and the page it starts.

on an inner page ctid is the downlink: the block number of a child page (the offset part is unused). item 1 is an 8-byte tuple with empty data: the "minus infinity" key, smaller than everything, so there's nothing to store. the root has no high key, because it's the rightmost page on its level (it's the only one). every separator is the first id on its child: 367, 733 (dd 02), 1,099 (4b 04), each 366 apart.

many rows, one key

ids are unique. a column like status isn't: 10,000 orders, six values.

sqldb=shop · ext=pageinspect · session pk
continues session pk (5 earlier blocks run first)
CREATE INDEX orders_status ON orders (status);
SELECT blkno, type, live_items, avg_item_size, free_size
FROM bt_multi_page_stats('orders_status', 1, -1)
ORDER BY blkno;
SELECT itemoffset, itemlen, cardinality(tids) AS rows, htid, left(data, 20) AS data
FROM bt_page_items('orders_status', 1)
ORDER BY itemoffset;
output, from postgres 18.6
#1 · CREATE INDEX
#2 · SELECT 10
blknoint8typecharlive_itemsint4avg_item_sizeint4free_sizeint4
1l106861244
2l10730804
3r9297848
4l10730804
5l10730804
6l10730804
7l10730804
8l10730804
9l10730804
10l76353672
(10 rows)
#3 · SELECT 10
itemoffsetint2itemlenint2rowsint4htidtiddatatext
132null(6,104)15 64 65 6c 69 76 65
2808130(0,1)15 63 61 6e 63 65 6c
3808130(30,99)15 63 61 6e 63 65 6c
436857(66,103)15 63 61 6e 63 65 6c
5808130(0,2)15 64 65 6c 69 76 65
6808130(1,18)15 64 65 6c 69 76 65
7808130(2,41)15 64 65 6c 69 76 65
8808130(3,56)15 64 65 6c 69 76 65
9808130(4,75)15 64 65 6c 69 76 65
10808130(5,90)15 64 65 6c 69 76 65
(10 rows)

ten pages for 10,000 rows, and each leaf holds about 10 items. most of them are 808 bytes: posting list tuples, one key followed by a sorted array of 130 heap addresses (tids). delivered covers most of the table, so it takes dozens of them, spread over several leaves. that's deduplication, and what it saves is in deduplication and bottom-up deletion.

orders_status, leaf page 1
delivered (6,104)1cancelled ×1302cancelled ×1303cancelled ×574delivered ×1305delivered ×1306… delivered ×13010
real output, postgres 18.6 (PGlite 0.5.12) · the run block above
leaf 1 of orders_status in slot order: a high key, then posting lists of up to 130 row addresses each. the high key carries a heap address too: the boundary between leaf 1 and the next falls in the middle of the delivered rows.

look at the high key: delivered, with an htid of (6,104). since postgres 12 the heap address is part of the key, as a last tiebreaker column, so even equal values have one exact place in the order and a page boundary can fall between two rows with the same status. that's what lets a b-tree find one duplicate's entry again when VACUUM comes to remove it, instead of scanning every delivered entry.

one level deeperthe functions, in one place
bt_metap(index)
page 0: root, level, fastroot, allequalimage.
bt_page_stats(index, blkno)
one page: type (r, i, l, or d for deleted), live and dead items, free space, prev, next, level, flags.
bt_multi_page_stats(index, from, count)
the same for a run of pages, -1 for all of them. postgres 16+.
bt_page_items(index, blkno)
every item on a page: itemoffset, ctid, itemlen, nulls, vars, data (the key's bytes), dead (the LP_DEAD hint), htid, tids.
page_header(get_raw_page(index, blkno))
the generic header: lower, upper, special, the page LSN and checksum.
bt_index_check(index)
from amcheck: verifies key order within and across pages, under a light lock.

keylines

  • page 0 is the metapage: it names the root and the tree's height.
  • every page has a header, line pointers, tuples, and 16 bytes of special space with its level and both sibling links.
  • item 1 of a non-rightmost page is the high key, an upper bound that points at no row.
  • an inner page's first item is the key-less "minus infinity" downlink.
  • repeated keys are stored once with a posting list of heap addresses, and the heap address is part of the key.