Skip to content

Lesson 2 of 9 · 27m

B-Trees: Pages, Fanout, Splits, and Why Indexes Age

Learn how B-tree pages stay balanced, what happens during inserts and splits, why key order matters, and how write-heavy indexes fragment over time.

B-trees are not theoretical tree diagrams. They are page-management machines. Every root-to-leaf search is a small number of page reads because each internal page holds many keys and child pointers. That fanout — hundreds or thousands of children per level — is why a billion-row table can often be searched in three or four page touches.

A B-tree page is a directory

A simplified leaf page contains sorted key entries plus pointers to rows. Internal pages contain separator keys that route searches down the tree.

root page
[ 10 | 20 | 30 ]
  |    |    |
  v    v    v
leaf  leaf  leaf
[1..9] [10..19] [20..29]

The important property is not just sorted order. It is that every leaf sits at roughly the same depth. The tree stays balanced by splitting full pages and redistributing entries.

Inserts can be cheap or catastrophic

Inserting increasing keys at the right edge is usually friendly: pages fill predictably and the rightmost leaf splits. Inserting random UUID keys spreads writes across the entire tree. Every insert may require a different leaf page in memory, increasing cache pressure and write amplification.

CREATE TABLE events_uuid (
    id uuid PRIMARY KEY,
    created_at timestamptz NOT NULL,
    payload jsonb NOT NULL
);

CREATE INDEX events_created_at_idx ON events_uuid (created_at);

If id is random and every insert touches a random page, the primary key index becomes a random-write workload. A time-ordered or k-sortable identifier such as a UUIDv7, ULID, or database sequence often improves locality. This is not ideology; measure it under your write pattern.

Splits, fragmentation, and fill factor

When a leaf fills, the engine splits it and allocates another page. Repeated random insertion and deletion leaves partially filled pages. The index still works, but range scans touch more pages than necessary.

CREATE INDEX orders_customer_idx
ON orders (customer_id)
WITH (fillfactor = 90);

PostgreSQL's fillfactor reserves space in index pages so updates can place new versions nearby before a split is needed. It is not free performance. Lower fill factor consumes more pages and memory. Use it when update patterns justify the trade.

Composite indexes are lexicographic directories

A composite key (tenant_id, created_at) is sorted first by tenant, then timestamp within each tenant:

CREATE INDEX events_tenant_time_idx
ON events (tenant_id, created_at DESC);

That index supports equality on tenant_id plus a range on created_at. It is much less useful for a query filtering only created_at across all tenants because the leading column is unconstrained. The order of keys is part of the storage layout, not a hint to the optimizer.

B-trees love reads and tolerate writes

B-trees excel when reads need point lookup and ordered ranges. They pay for that structure during writes: random page updates, split logic, latching, and dirty-page writeback. Under extreme write ingestion, that cost pushed systems toward LSM trees, which the next lesson covers.

Field notes

  • Index depth is usually small; page cache residency matters more than the theoretical number of levels.
  • Random-key insertion can make an index write-heavy even when table rows are append-only.
  • Index bloat shows up as more pages for the same logical key count, not incorrect query results.
  • REINDEX can shrink a bloated index, but it treats symptoms; changing write locality may prevent recurrence.

Was this lesson helpful?

Sign in to rate