Leaf Traversal: Sibling Pointers and Latch Coupling

Physical consecutiveness vs logical order

Every relation's file — heap and index alike — is a flat sequence of blocks numbered 0, 1, 2, … packed back-to-back. That seems to contradict "B-tree pages are not consecutive." The resolution: physical consecutiveness and logical order are independent things.

Within a heap file — they happen to match

Heap rows have no inherent sort order. Page 0 holds whatever was inserted first, page 1 the next batch. Physical block order and "logical" data order are both unsorted, so there is no tension to resolve.

Within an index file — they diverge

Leaf pages must be visited in key order during a range scan. But leaves are allocated at whatever block number happened to be free at the time, so there's no guarantee the leaf covering keys 200–300 sits above the one covering 100–200.

PostgreSQL resolves this with sibling pointers in every page header:

Top row, key order: four leaf pages holding keys 0–99, 100–199, 200–299 and 300–399, linked left and right by sibling pointers. They live at blocks 7, 3, 11 and 5. Bottom row, the same index file in block order, 0 to 11: block 0 is the meta page, block 1 the root, block 2 an internal node, and the four leaves sit at blocks 3, 5, 7 and 11. A range scan follows the right-sibling pointers from block 7 to 3 to 11 to 5, jumping back and forth through the file.
Key order lives in sibling pointers; block order is whatever was free Block numbers follow the illustrative index file listed below. A scan reads blocks 7 → 3 → 11 → 5 — it never reads "the next block".

The engine follows right_sibling / left_sibling to reach the next leaf. It does not read the next block number. Physical layout is irrelevant to correctness.

Why page headers carry a type field

A heap file holds only heap pages. An index file holds a mix — one meta page, a root, several internal nodes, many leaves — all interleaved among block numbers as the tree grows. The header's page type field (plus a "special area" at the end of the page holding B-tree-specific metadata like sibling pointers) tells PostgreSQL how to interpret whatever block it just read.

Index file blocks (illustrative):
  block 0:  B-tree meta page
  block 1:  root (internal node)
  block 2:  internal node
  block 3:  leaf [keys 100–199]   ← right_sibling → block 11
  block 5:  leaf [keys 300–399]
  block 7:  leaf [keys 0–99]      ← right_sibling → block 3
  block 11: leaf [keys 200–299]   ← right_sibling → block 5

Why not use the parent to find the next leaf?

The parent knows its children — but two adjacent leaves often have different parents. Going via the parent means climbing to the common ancestor and back down: O(log n) per step instead of O(1). Over a range scan touching many leaves, that compounds fast.

A tree: the root has two children, internal A and internal B. A has leaf 1 covering c–d and leaf 2 covering d–e; B has leaf 3 covering e–f and leaf 4 covering f–g. Leaf 2 and leaf 3 are adjacent in key order but have different parents. Reaching leaf 3 from leaf 2 via the parents means climbing to A, then the root, then descending to B and leaf 3: four hops, O(log n). The sibling pointer reaches it in one hop, O(1).
Adjacent leaves often have different parents — the sibling pointer skips the climb

Why latches exist during a scan

While you are reading leaf X and about to follow its sibling pointer to leaf Y, another transaction could be splitting Y — moving half its keys to a new page and rewriting sibling pointers. Without coordination you could read Y mid-split and silently miss keys.

The fix is a lightweight page latch (not a transaction lock — no commit/rollback, held for microseconds):

latch coupling — acquire the next before releasing the current

latch(current_leaf, SHARED)
read keys
latch(right_sibling, SHARED)   ← acquire next BEFORE releasing current
unlatch(current_leaf)          ← now safe to drop
read keys
...

Holding two latches briefly at the transition is what prevents the sibling pointer going stale under you.

Could you identify all leaf pages upfront and read them in parallel?

In theory yes — descend to find the first and last leaf, walk internal nodes to collect all leaf block numbers in range, then read in parallel. Two problems in practice:

  1. Enumeration still costs a traversal. Internal nodes say "keys in [X,Y] live in this subtree" — not a flat list of block numbers. Collecting them all means walking every overlapping subtree, which is no simpler than following siblings.
  2. Small ranges don't benefit. Most range queries touch 2–10 leaf pages. Thread coordination and I/O merging cost more than a handful of sequential sibling hops.

PostgreSQL handles genuinely large ranges with dedicated mechanisms instead:

Bitmap Index Scan

Phase 1: scan index sequentially
         → collect matching TIDs into a bitmap
Phase 2: sort TIDs by physical heap page order
Phase 3: read heap pages in physical order
         → sequential I/O, prefetch-friendly

Decouples index traversal from heap access and avoids random I/O on the heap. Chosen when the range is large or many rows match.

Parallel Index Scan (PG 10+)

Multiple workers split the leaf level and scan different key sub-ranges simultaneously — exactly the parallel idea above, applied when the planner decides the range is large enough to be worth the coordination cost.

Why latches, not MVCC — and why I/O sits outside the latch window

MVCCLatches
SolvesData visibility — which version of a row a transaction should seeStructural integrity — preventing a concurrent split from rewriting pointers on a page you're mid-read on
MechanismPer-row xmin/xmax stamps + snapshotShort mutex on an in-memory page

A B-tree page split cannot be versioned the way a heap row can. After a split there is exactly one correct structure — both children exist, sibling pointers updated, parent has a new entry. You cannot show transaction A the pre-split structure and transaction B the post-split structure at the same time. The latch is what makes the split atomic to everyone else.

Why I/O is outside the latch window: the latch is a mutex on an in-memory page. Holding it across a disk read (milliseconds) would block every other reader of that page for ~1000× longer than necessary. The buffer manager separates the phases:

✓ Phase 1 — bring the page into RAM (no content latch)

pin the buffer slot   ← lightweight reservation,
                        doesn't block other readers
read from disk        ← ~1 ms; readers of other
                        pages are unaffected
signal: page is ready

✓ Phase 2 — read page contents (latch held)

acquire content latch ← page already in RAM,
                        takes microseconds
scan keys
release latch

Transactions waiting for the same page during phase 1 block on a condition variable, not on the content latch. The latch window is only the in-memory scan.

The Invariants

  • Guarantees: A range scan visits every leaf in key order and never observes a page mid-split — regardless of where those leaves physically sit in the file.
  • Does not guarantee: Sequential disk I/O for a range scan. Logically adjacent leaves can be anywhere on disk, which is why bitmap scans (which sort TIDs into physical order) exist as a separate strategy.