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
| Aspect | Log-structured (Part I) | B-tree (Part II) |
|---|---|---|
| Segment size | Variable — several MB or more | Fixed-size pages, typically 4–16 KiB |
| Writes | Write-once, immutable | Overwrite pages in place |
| Page size | N/A | PostgreSQL 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.
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:
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"
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.