arthrop0d

Brief articles on varied topics.

B-Tree Page Splits and Parent Updates

databasessql

A B-tree is a balanced, page-oriented search tree that stores sorted keys in database pages and uses parent pages to direct each lookup toward the right data. When a page fills, the database splits it into two pages, redistributes its entries, and records a separator in the parent so both pages remain reachable.

The problem a B-tree solves

An index lets a database find rows without scanning every row in a table. A simple sorted array could support binary search, but inserting into the middle would require shifting a large portion of the array. A linked list makes insertion easier, but finding a key requires walking many entries.

Database indexes have another constraint: storage is read and written in fixed-size blocks, usually called pages. Reading one page may involve a storage operation, so an index should make each page useful and keep the number of pages visited small.

A B-tree groups many sorted keys into each page. Its height stays small because one internal page can point to many child pages. “Balanced” means that all searches reach the leaves at roughly the same depth; the tree does not grow into a long, inefficient chain as rows are inserted.

What an index page contains

A typical B-tree has three kinds of information:

Many database indexes are technically B+ trees, a common B-tree variant in which row references live in leaves and internal pages contain routing information only. The split process is the same in principle, although the exact treatment of the separator key differs between implementations.

For example, an internal page might divide its key ranges like this:

Child Keys covered
A less than 40
B 40 through 79
C 80 and above

The parent stores 40 and 80 as separators. A lookup for 63 follows the pointer to B; a lookup for 91 follows the pointer to C.

Inserting into a full leaf

Suppose an insert reaches a leaf page that has room. The database places the new key in sorted order, updates the page's row references, and writes the changed page. No structural change is needed.

If the leaf is full, the database cannot simply append another entry. It allocates a new page, usually called a sibling, and divides the old page's sorted entries between the two pages. The new key is included in that ordering before the division, so the result remains sorted.

Imagine a leaf containing these keys:

10  20  30  40  50  60

An insertion of 35 could split the entries into:

left leaf:  10  20  30
right leaf: 35  40  50  60

The database then changes the parent page so it has a pointer to the new right leaf and a separator describing the right leaf's lower bound. In a common B+ tree arrangement, the parent receives 35, the first key in the right leaf; the key remains in the leaf as well. Other B-tree variants promote a middle key differently. The important invariant is that the parent separator sends every possible search to the correct child.

Some engines can first try to redistribute entries with an adjacent sibling that has spare capacity. This means moving some keys between existing pages instead of allocating a new one. If the neighboring page is also full, a two-page split is the usual operation. The policy is implementation-specific, but sorted order and correct parent routing must be preserved either way.

Updating the parent

The parent itself must have room for one more child pointer and separator. If it does, the update is local: insert the new separator in sorted position and place the new child pointer beside it.

If the parent is full, it must split too. Its child pointers and separator keys are divided across two internal pages. One separator is promoted to the parent's parent, because that higher page now needs to distinguish the two new internal pages.

This can repeat upward. A leaf split may therefore cause several internal-page splits in one insertion:

  1. Split the full leaf.
  2. Add the new leaf pointer and separator to its parent.
  3. If that parent is full, split it and promote a separator.
  4. Continue until reaching a page with room or the root.

An internal-page split does not always copy the promoted separator in the same way a leaf split copies a boundary key. In the traditional B-tree form, the promoted key leaves the lower level. In a B+ tree, the parent commonly stores a copy of a boundary key while the actual indexed entries remain in leaves. Storage engines choose details such as these to fit their page format and search rules.

When the root splits

The root has no parent to update. If it splits, the database creates a new root containing pointers to the two resulting pages and a separator between them. The tree gains one level, but every leaf remains at the same depth, so it is still balanced.

Before the split, the root might be a single page containing all entries. Afterward, the new root directs searches to two children. Future inserts may split those children, but the same propagation process keeps extending the tree in a controlled way.

Why lookups stay efficient

A split increases the number of pages, but it does not require rebuilding the whole index. Only the overflowing page, its parent path, and possibly newly allocated pages are changed. Existing pages keep their search ranges and remain connected through parent pointers.

Because pages hold many keys, each internal page has a high fan-out, meaning it can point to many children. A tree with a large number of rows can therefore remain only a few pages tall. A lookup follows one pointer per level, giving B-tree indexes logarithmic growth in search work while matching the database's page-oriented storage model.

If an incident log says an index experienced “page splits,” it usually means inserts reached full pages and the engine performed this process. Splits can add writes and cause temporary contention, but they are the normal mechanism that lets a growing index preserve sorted structure and fast indexed lookups.

← All articles