B-Tree Fundamentals

The other family, and the more widely deployed one. Same goal as Part I — sorted keys, cheap lookups, range scans — reached by the opposite means: fixed-size pages overwritten in place. That single decision cascades into everything that follows: splits and merges instead of compaction, latches instead of atomic swaps, a heap separate from the index, MVCC tuples instead of sequence numbers, and VACUUM instead of compaction. Worked through PostgreSQL, because it is the engine where all of this is observable.

How it differs from Part I, structurally

AspectLog-structured (Part I)B-tree (Part II)
Segment sizeVariable — several MB or moreFixed-size pages, typically 4–16 KiB
WritesWrite-once, immutableOverwrite pages in place
Page sizeN/APostgreSQL 8 KiB, MySQL/InnoDB 16 KiB

Both keep key-value pairs sorted by key, so both support efficient lookups and range queries. The design philosophies are opposites.

What a file is, what a page is

A file is a named sequence of bytes. When the database opens data.db, the OS hands it a byte stream it can seek into at any position. How those bytes are organized is entirely the database's decision — a B-tree is one such organization.

A page is a fixed-size slice of that file, treated as one unit — one read, one write.

The file data.db drawn as five consecutive 4 KB pages, numbered 0 to 4, starting at byte offsets 0, 4096, 8192, 12288 and 16384. Reading page 3 is one pread of 4096 bytes at offset 3 times 4096, which is 12288: one seek, one read, no scanning.
A file is a row of fixed-size pages — page N starts at N × 4096

byte offset of page N = N × page_size

to read page 3: seek to 3 × 4096 = 12288, read 4096 bytes. One disk I/O, O(1), no scanning.

Page on disk vs page in memory

The CPU cannot read from disk. To use a page, the OS first copies it into RAM. That copy is expensive — disk reads take microseconds to milliseconds, RAM access nanoseconds. Once copied, the OS keeps the page in the page cache as long as it can, so the next access skips the disk entirely. This is why databases "warm up" after a restart.

The entire design of B-trees — wide, shallow, fixed-size pages — exists to minimize how many pages must be loaded from disk per query. Every structural decision in this part and the Splits, borrows and merges part is downstream of that.

What an index page actually stores

This applies to root and internal pages, not leaves. An index page stores boundary keys and child pointers, interleaved — never full records:

A root index page stores boundary keys and child pointers interleaved: p0, dave, p1, mike, p2, sandy, p3. Pointer p0 leads to the child page for keys below dave, p1 to keys from dave up to mike, p2 to keys from mike up to sandy, and p3 to keys from sandy upward. No full records are stored.
An index page is a sorted array of boundaries, each one a fork to a child

It is a sorted array of (boundary_key, child_pointer) pairs. Finding the right child is a binary search over those boundaries — O(log branching_factor). With a branching factor of 500 that's ~9 comparisons, all in RAM once the page is loaded. Leaf pages store the actual keys plus either inline values or pointers to where the values live.

Tree navigation — looking up "jack"

Looking up jack. The root page, always cached in RAM, holds dave, mike, sandy; jack is not below dave but is below mike, so follow p1, costing no disk read. The child page covering dave to mike holds fred, henry, jack; jack is at or above jack, so follow p3, one disk read. The leaf page holds jack and returns its value, one more disk read. Total: two disk reads, regardless of database size.
Looking up "jack" — one comparison chain per level, two disk reads in total
Three range bars, one per level. The root page splits the key space into 0–200, 200–300, 300–400 and more; key 251 falls in 200–300. That child, an internal page, subdivides 200–300 into 200–250, 250–270 and 270–300; 251 falls in 250–270. That child is a leaf page covering 250–270, which holds key 251 and its value.
The same descent as ranges — each level zooms into one slice of the key space

Branching factor and depth

The branching factor is the number of child references in one page — typically several hundred, bounded by how much space the boundary keys and pointers need. The algorithm keeps the tree balanced: a B-tree with n keys always has depth O(log n), and most real databases sit at 3–4 levels.

4 levels × 4 KiB pages × branching factor 500 ⇒ up to ~250 TB addressable

which is why "how many disk reads is a lookup?" has the same answer at every realistic scale

The Invariants

  • Guarantees: O(log n) lookups with a small constant — 3–4 page reads at any realistic data size — plus ordered access, because leaves hold keys in sorted order.
  • Does not guarantee: That logically adjacent leaves are physically adjacent on disk (see Leaf traversal and latches), or that a page write is atomic — which is the entire reason the WAL of the In-place writes part must exist.