LESSON 03 · B-trees

Update pages in place with a B-tree

A B-tree gives every key one home on a fixed-size page and relies on a log to survive a crash mid-update.

SYSTEM VIEW

The write path

Diagram view
Dirty pages—
Past checkpoint—

Follow parcel 8841 down a B-tree to its page and change it in memory, then see the log and a checkpoint bring a torn page back.

  1. Postgres and InnoDB keep data in B-trees of fixed-size pages, 8 KB and 16 KB by default. Each page holds sorted keys and references to child pages; leaves hold the rows or pointers to them. At an example 400 entries a page, three levels reach 64 million entries, four 25.6 billion.

    Page size
    8 KB or 16 KB
    3 levels
    64 million entries
    4 levels
    25.6 billion

    What changed

    • Root, inner and leaf pages
    • 400 entries a page
  2. The App reads parcel 8841. The engine walks from the root to the one leaf whose key range holds 8841. The root and inner pages are already in the buffer pool, so only the leaf costs a disk read: one read, sometimes two. Each key has one home, so reads are predictable.

    In memory
    root and inner pages
    Disk reads
    1, sometimes 2

    What changed

    • App and buffer pool
    • 1 disk read, for the leaf
  3. Parcel 8841 ships. Its leaf page changes in the buffer pool and is now dirty, to be written back in place later. That is risky, so the change first goes in the write-ahead log, past the last checkpoint’s mark, synced at commit: the commit is safe though the page is only in memory.

    Logged
    8841 shipped
    Dirty
    8841’s leaf page

    What changed

    • 8841’s leaf page dirty in memory
    • 8841 shipped in the log, synced
    • 1 record past the last checkpoint
  4. Parcel 8850 belongs in 8841’s leaf, which is full, so the leaf splits in two and the parent gains a key for the new page; a full parent would split too. The split and 8850 are logged, and 3 pages are now dirty: two leaves and the parent.

    Leaf
    1 page → 2
    Parent
    gains a key
    Dirty pages
    3

    What changed

    • A new leaf page, in memory
    • Parent gains a key
    • Leaf split, 8850 new in the log
  5. A checkpoint writes the 3 dirty pages to disk, each in its fixed place, and moves its mark to the log’s end. After a crash, recovery replays only the log past the mark: nothing now.

    Written
    3 pages, in place
    Replay from
    the checkpoint

    What changed

    • 3 pages written back in place
    • Checkpoint moved to the log’s end
    • Dirty pages
  6. Parcel 1203 is packed, the first change to its leaf page since the checkpoint. A power cut mid-write can tear a page, half old and half new, so Postgres logs a full copy of the page along with the change; InnoDB writes pages to a doublewrite buffer instead.

    Postgres
    full page copy in the log
    InnoDB
    doublewrite buffer

    What changed

    • Page copy and 1203 packed in the log
    • 1203’s leaf page dirty
  7. The engine starts writing 1203’s page back and the power fails mid-write: the page on disk is torn, half old and half new, and the buffer pool is gone. A crash mid-split is as bad, leaving a parent pointing at a page never written. The log, synced at each commit, is intact.

    On disk
    1203’s page, torn
    Lost
    the buffer pool
    Kept
    the synced log

    What changed

    • Buffer pool, lost with the power
    • 1203’s page torn on disk
  8. On restart the engine replays only the 2 records past the checkpoint: the copy rebuilds 1203’s page in the buffer pool, 1203 packed is redone on it, and the page goes back whole. So a small change costs a log record and, eventually, a whole page write.

    Replayed
    2 records
    Skipped
    3, before the mark

    What changed

    • 1203’s page rebuilt from the copy
    • 1203 packed redone
    • Page written back whole

Space plays · ← → step · Home resets

THE IDEA

How it works

Postgres, MySQL with InnoDB, and SQL Server all index data with B-trees. A B-tree divides storage into fixed-size pages, by default 8 KB in Postgres and 16 KB in InnoDB. Each page holds sorted keys and references to child pages, and leaf pages hold the rows or pointers to them. Wide pages keep the tree shallow. At an example 400 entries per page, three levels reach 64 million entries and four reach 25.6 billion. With upper levels cached, a lookup usually needs one or two disk reads.

To change a key, the engine finds its leaf page and modifies it, and the page is later written back to the same location. If the page is full, it splits in two and the parent gains a key, which may split the parent too. Each key has one home, so reads are predictable and scans follow sorted leaves.

Overwriting in place is risky. A crash mid-split can leave a parent pointing at an unwritten page, and a power cut can tear a page, leaving it half old and half new. So the engine records each change in a write-ahead log, synced at commit, while changed (“dirty”) pages wait in memory. A checkpoint writes dirty pages out, so recovery only replays the log from that point. Torn pages need more: Postgres logs a full copy of a page the first time it changes after a checkpoint, and InnoDB writes pages to a doublewrite buffer first. So a small change costs a log record and, eventually, a whole page write.

DECISION POINT

Your Postgres primary checkpoints about once an hour (an example setting), and after a crash it spent 20 minutes replaying the log. The business wants restarts under 3 minutes. What do you change, and what does it cost?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • A B-tree keeps each key in one page, so reads are predictable and the tree stays shallow.
  • Pages change in place, so the write-ahead log is what makes a crash mid-update recoverable.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
ApplyQUESTION 01

A B-tree page holds about 500 entries. Roughly how many levels are needed to reach 100 million rows?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.