Unit 6 of 12 · about 4 hours · explain, implement

In-memory and sparse indexes, and B-tree page and split invariants

Every range query in this track walks an ordered structure. The B-tree invariants are what keep that walk correct after splits and merges.

You'll be able to

  • Explain when an in-memory index suffices and when a sparse on-disk index pays off.
  • State the B-tree page and split invariants and check them after every mutation.
  • Derive tree depth from page size and entry size for a stated row count.

Covers

In-memory and sparse indexesB-tree page and split invariants
1

Read

In this order, only the parts listed

0 of 3 done

  1. B-treesDDIA 2e - Chapter 4 Storage and Retrieval

    Chapter 4, B-Trees, printed pp. 125-128: fixed-size pages, page numbers as disk pointers, and branch versus leaf layout

  2. CMU 15-445 Fall 2025 index lectures and B+ tree projectCMU 15-445 - Storage, Index and Recovery Sequence

    Lecture #08: Indexes and Filters I with Lecture #07: Hash Tables for the hash-index contrast and Lecture #10: Index Concurrency Control for latch crabbing; project page for the B+ tree with the Fall 2025 leaf tombstone buffer

    Use the lectures for page layout and split behavior. Leave optimistic latch crabbing internals for later; the invariant work here is single-threaded correctness.

  3. HLD Handbook B-tree mechanicsHLD Handbook - Storage Engines and Data Structures

    B-tree mechanics: fixed pages with InnoDB 16 KiB and PostgreSQL 8 KiB sizes, fanout and depth, page splits, clustered versus heap layout

2

Focus on

  • An in-memory hash map from key to log offset is fast but must fit in RAM and rebuilds by scanning the log. A sparse index keeps one entry per page or segment and bounds memory at the cost of one extra page read.
  • Every leaf sits at the same depth. Every node except the root holds between the minimum and maximum key count. A split moves half the keys and posts the middle key upward, possibly cascading.
  • A 16 KiB page with 200-byte entries holds about 80 children, so a billion-row table needs 5 levels since . Depth grows logarithmically, not with data size.
  • The Systems unit on hash indexes, B-trees, selectivity and range scans taught the structures. Here the new work is checking split and merge invariants in code. InnoDB stores row data in the primary-key leaf while PostgreSQL keeps rows in a heap, so their secondary pointers and vacuum behavior differ.
Common mistakes · 3
  • Counting depth as linear in data size. Each level multiplies capacity by the fanout, so one more level absorbs roughly 80x more keys.
  • Assuming every secondary index points at the heap. InnoDB secondary entries store the primary key and need a second traversal.
  • Splitting only the leaf. An overflowing internal node splits too, and a root split grows the tree by one level.
3

Try it yourself

Closed-book, before checking anything

Closed-book, derive depth for keys with 16 KiB pages and 200-byte entries: fanout about , since is too small and exceeds , lookups touch at most 5 pages.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.When does a sparse index beat a dense in-memory hash map?Answer

    When the key count exceeds RAM or restart time matters. The sparse index keeps one entry per page or segment, trading one bounded page read for a small memory footprint and fast rebuild.

  2. 2.A leaf with capacity 4 holds keys 1, 2, 3 and 4, and key 5 arrives. What changes?Answer

    The leaf splits into two leaves with 2 or 3 keys each, the middle key posts to the parent, and all leaves stay at the same depth. A full parent splits recursively.

  3. 3.Why do B-tree range scans stay cheap while hash-index range scans do not?Answer

    B-tree leaves hold keys in sorted order with sibling links, so a range walks forward. A hash index scatters neighboring keys across buckets with no order to walk.

  4. 4.PostgreSQL updates a row three times without vacuum. How many heap tuples exist?Answer

    Four versions until cleanup: the original plus three new ones, the older three dead once the updates commit. Vacuum, or pruning when the page is next read, reclaims them.

5

Practice

  • CMU 15-445 Fall 2025 Project 2: B+ Tree with leaf tombstone buffer

    B+ tree insert, delete and iterator tests including the leaf tombstone buffer behavior across merge, split and redistribute

    Configure with -DCMAKE_CXX_FLAGS="-include cstdint" under GCC 15 and strip the DISABLED_ test prefix to run them. Keep scope to single-threaded invariants.

You're done when

You derive depth for a new page and entry size without notes and your invariant checker passes splits, merges and redistributes.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 10 more in the section
  • CMU 15-445 - Storage, Index and Recovery SequenceCarnegie Mellon Database Group · primary

    Follow Fall 2025 lectures 3 to 10 and 21 to 22. Do the BusTub projects from tag v20251119-2025fall (https://github.com/cmu-db/bustub/tree/v20251119-2025fall): P1 (ARC replacer, disk scheduler and buffer pool manager) and P2 (B+ tree with a leaf tombstone buffer), with P0 (Count-Min Sketch) as the C++ warm-up. Tests ship with a DISABLED_ prefix to remove, and GCC 15 needs -DCMAKE_CXX_FLAGS="-include cstdint" at configure time. Use the storage and recovery homework for WAL reasoning, and keep C++ scope bounded to storage and recovery.

  • DDIA 2e - Chapter 4 Storage and RetrievalMartin Kleppmann and Chris Riccomini · required

    Read "Storage and Indexing for OLTP" (log-structured storage, B-trees, their comparison and secondary indexes) and "Data Storage for Analytics" (column-oriented storage). Its Bloom filter, tombstone and write-ahead log coverage is brief, so take those from OSTEP and The Internals of PostgreSQL. Build a workload decision table, then connect each claim to a measurement from your small store.

  • HLD Handbook - Storage Engines and Data StructuresHLD Handbook · required

    Read Part 0 data structures plus Part 4 storage engines and OLTP versus OLAP. Use its exercises for comparison, while the CMU project and local fault tests remain the implementation evidence.

Also in this section

  • Hypothesis - Stateful TestingHypothesis · support

    Use RuleBasedStateMachine with rules, bundles and invariants to drive your store and a dictionary model with the same random operation sequences, and check the machine catches a planted bug before trusting a pass.

  • Building Distributed Systems - The Logger and IndexesBuild Distributed Systems · optional

    Only the first three tasks are free (a write-ahead log, its recovery and log segments). Tasks are graded in the browser after login, so treat a pass as a drill and keep your local harness as the evidence.

  • The Internals of PostgreSQL - Buffer Manager, WAL and RecoveryHironobu Suzuki · support

    Read Chapter 1 Sections 1.2 to 1.4 (heap pages and tuples), Chapter 8 (buffer manager and dirty-page flushing) and Chapter 9 Sections 9.1 to 9.8 (WAL segments, XLOG records, checkpoints and recovery). Compare each mechanism with your simplified store. The site is revised often, so note the date you read it.

  • PostgreSQL 18 Documentation - WAL Configuration and InternalsPostgreSQL Global Development Group · support

    Read "WAL Configuration" (checkpoints and the redo point crash recovery starts from), "WAL Internals" and "Reliability" for the official, versioned statement of the write-ahead and durability contract.

  • Operating Systems: Three Easy Pieces - Crash Consistency and Data IntegrityRemzi and Andrea Arpaci-Dusseau · required

    Read Chapter 42 (FSCK and journaling: the crash scenarios, data and metadata journaling and their write order) and Chapter 45 (data integrity: checksums, misdirected and lost writes, scrubbing; https://pages.cs.wisc.edu/~remzi/OSTEP/file-integrity.pdf). Turn each crash scenario into an injection point in your store.

  • ARIES: A Transaction Recovery Method (Mohan et al., 1992)ACM TODS (Stanford course copy) · support

    Read the introduction, the overview and restart processing (analysis, redo by repeating history, undo) and the page LSN rule. The rest is 1992 storage detail beyond this section.

  • Build Your Own RedisBuild Your Own · optional

    Use the persistence and data-structure portions only if a second implementation is needed to cement parsing, indexing or append-log behavior. Do not replace the crash-tested stage artifact with tutorial completion.

  • Replicated Log and Stream Processor - Storage SliceRoadmap-Final · support

    Implement checksummed segments, indexes, acknowledgement states, recovery, retention and atomic metadata changes. Preserve every crash schedule and corrupted artifact needed to reproduce a failure.

  • Apache Parquet File FormatApache Software Foundation · support

    Read the File Format layout (row groups, column chunks, footer metadata) and the Page Index page (ColumnIndex and OffsetIndex with per-page min and max values for page skipping). Use it only for file layout and skipping; it does not cover table metadata or catalogs.

  • Linux man-pages: fsyncLinux man-pages project · support

    Read the DESCRIPTION for what fsync and fdatasync guarantee and the directory-sync requirement for new file names, and the ERRORS section for what an EIO from write-back can refer to.