Splits, Borrows, and Merges

Updating an existing key

  1. Search down to the leaf page containing the key.
  2. Overwrite that page on disk with the new value. This is the "in-place" in "in-place update engine."

Adding a new key

  1. Find the page whose range encompasses the new key.
  2. Add the key to that page — if there is room, you're done.
  3. If there isn't: split the page into two half-full pages, and update the parent with the new boundary. Splits can cascade up; when the root splits, a new root is created above it and the tree grows a level.

Worked example — inserting key 334 into a full page covering 333–345: the page splits into 333–337 (holding the new 334) and 337–345, and the parent gains boundary value 337. If the parent is also full, it splits too, and so on up to the root.

Deleting a key: the underfull problem

Deletion is harder than insertion because removing a key can leave a page underfull — below the minimum occupancy threshold, typically 50% of capacity.

Page has branching factor 4, minimum fill = 2 keys:

  Page A: ["fred","henry","jack"] → delete "jack"  → ["fred","henry"]  ← at minimum, OK
  Page A: ["fred","henry"]        → delete "henry" → ["fred"]          ← UNDERFULL

Step 1 — try to borrow from a sibling

If an adjacent sibling has more than the minimum, rotate one key through the parent. Cheaper than a full merge.

Why rotate through the parent rather than moving a key directly? Because the parent's boundary key is what separates the two children's ranges, and it must stay consistent. Stealing a key straight from the sibling would break the ordering invariant.

✗ Before — A underfull, B has a spare

    Parent: [ptr0] | "jack" | [ptr1]
              /                   \
Page A: ["fred"]     Page B: ["mike","sandy","tony"]
(underfull)          (can lend one)

"jack" is the boundary:
  everything in A < "jack" ≤ everything in B.
Moving "mike" directly into A would
violate that — A would hold a key ≥ "jack".

✓ After — three-way rotation

1. parent's boundary "jack" → end of Page A
2. Page B's leftmost "mike" → parent as new boundary

    Parent: [ptr0] | "mike" | [ptr1]
              /                   \
Page A: ["fred","jack"]  Page B: ["sandy","tony"]
(now healthy)            (still healthy)

Invariant holds: everything in A < "mike" ≤ everything in B.

Step 2 — if the sibling is also at minimum, merge

  1. Combine the two underfull pages into one.
  2. Pull the boundary key down from the parent into the merged page.
  3. Remove the now-redundant pointer from the parent.
Before and after a merge. Before: the parent holds p0, jack, p1; page A on the left holds only fred and is underfull; page B on the right holds only mike, at the minimum, so it cannot lend. After: the boundary key jack is pulled down from the parent into one merged page holding fred, jack and mike, and the parent loses that pointer, keeping only p0. The parent itself may now be underfull, so the merge can cascade upward.
Merge: the boundary key comes down and the parent loses a pointer

Locking during a merge — and why production engines avoid it

The parent must be write-latched during a merge (you are pulling a key down and removing a pointer), and both children need write latches too. You hold all three simultaneously. Worse: after the merge, the parent itself may now be underfull, requiring another merge one level up — cascading potentially all the way to the root, where you would hold write latches on an entire root-to-leaf chain. If the root ends up with a single child, that child becomes the new root and the tree shrinks a level.

Insertions never cause merges — only splits. Merges are purely a deletion concern. If you remember one thing about B-tree mutation, make it this asymmetry.

The Invariants

  • Guarantees: The tree stays balanced and every page stays within its occupancy bounds — if merges are performed. Split behaviour alone guarantees balance for insert-only workloads.
  • Does not guarantee: Minimum page occupancy in real engines. Lazy delete means a delete-heavy index can drift far below 50% fill and stay there until a REINDEX — that is index bloat, and it is a deliberate trade, not a bug.