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.
orders_pkey as shop builds it: the metapage says the root is page 3, one level above the leaves.
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
SELECT * FROM bt_metap('orders_pkey');| magicint4 | versionint4 | rootint8 | levelint8 | fastrootint8 | fastlevelint8 | last_cleanup_num_delpagesint8 | last_cleanup_num_tuplesfloat8 | allequalimagebool |
|---|---|---|---|---|---|---|---|---|
| 340322 | 4 | 3 | 1 | 3 | 1 | 0 | -1 | t |
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):
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;| blknoint8 | typechar | live_itemsint4 | avg_item_sizeint4 | free_sizeint4 | prevint8 | nextint8 | levelint8 |
|---|---|---|---|---|---|---|---|
| 1 | l | 367 | 16 | 808 | 0 | 2 | 0 |
| 2 | l | 367 | 16 | 808 | 1 | 4 | 0 |
| 3 | r | 28 | 15 | 7596 | 0 | 0 | 1 |
| 4 | l | 367 | 16 | 808 | 2 | 5 | 0 |
| 5 | l | 367 | 16 | 808 | 4 | 6 | 0 |
| 6 | l | 367 | 16 | 808 | 5 | 7 | 0 |
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.
SELECT live_items FROM bt_page_stats('orders_pkey', 1); -- 367inside 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.
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;| blknoint4 | lowerint4 | upperint4 | specialint4 | live_itemsint4 | free_sizeint4 | btpo_flagsint4 |
|---|---|---|---|---|---|---|
| 1 | 1492 | 2304 | 8176 | 367 | 808 | 1 |
| 3 | 136 | 7736 | 8176 | 28 | 7596 | 2 |
| 29 | 496 | 6288 | 8176 | 118 | 5788 | 1 |
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:
SELECT itemoffset, ctid, itemlen, htid, data
FROM bt_page_items('orders_pkey', 1)
WHERE itemoffset <= 3 OR itemoffset = 367
ORDER BY itemoffset;| itemoffsetint2 | ctidtid | itemlenint2 | htidtid | datatext |
|---|---|---|---|---|
| 1 | (3,1) | 16 | null | 6f 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 |
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
SELECT itemoffset, ctid AS downlink, itemlen, data
FROM bt_page_items('orders_pkey', 3)
WHERE itemoffset <= 4 OR itemoffset = 28
ORDER BY itemoffset;| itemoffsetint2 | downlinktid | itemlenint2 | datatext |
|---|---|---|---|
| 1 | (1,0) | 8 | '' |
| 2 | (2,1) | 16 | 6f 01 00 00 00 00 00 00 |
| 3 | (4,1) | 16 | dd 02 00 00 00 00 00 00 |
| 4 | (5,1) | 16 | 4b 04 00 00 00 00 00 00 |
| 28 | (29,1) | 16 | 9b 26 00 00 00 00 00 00 |
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.
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;| blknoint8 | typechar | live_itemsint4 | avg_item_sizeint4 | free_sizeint4 |
|---|---|---|---|---|
| 1 | l | 10 | 686 | 1244 |
| 2 | l | 10 | 730 | 804 |
| 3 | r | 9 | 29 | 7848 |
| 4 | l | 10 | 730 | 804 |
| 5 | l | 10 | 730 | 804 |
| 6 | l | 10 | 730 | 804 |
| 7 | l | 10 | 730 | 804 |
| 8 | l | 10 | 730 | 804 |
| 9 | l | 10 | 730 | 804 |
| 10 | l | 7 | 635 | 3672 |
| itemoffsetint2 | itemlenint2 | rowsint4 | htidtid | datatext |
|---|---|---|---|---|
| 1 | 32 | null | (6,104) | 15 64 65 6c 69 76 65 |
| 2 | 808 | 130 | (0,1) | 15 63 61 6e 63 65 6c |
| 3 | 808 | 130 | (30,99) | 15 63 61 6e 63 65 6c |
| 4 | 368 | 57 | (66,103) | 15 63 61 6e 63 65 6c |
| 5 | 808 | 130 | (0,2) | 15 64 65 6c 69 76 65 |
| 6 | 808 | 130 | (1,18) | 15 64 65 6c 69 76 65 |
| 7 | 808 | 130 | (2,41) | 15 64 65 6c 69 76 65 |
| 8 | 808 | 130 | (3,56) | 15 64 65 6c 69 76 65 |
| 9 | 808 | 130 | (4,75) | 15 64 65 6c 69 76 65 |
| 10 | 808 | 130 | (5,90) | 15 64 65 6c 69 76 65 |
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.
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,
-1for 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.