skip to content

page splits

what happens when a leaf fills up, and why sequential keys split 90/10 instead of 50/50.

address
everything.index_internals.page_splits
part
part 13, inside indexes
chapter
chapter 3 of 12
reading time
9 min

when a leaf is full, the next insert splits it: part of the entries move to a new page, the new page is linked in beside it, and a separator for it goes into the parent. where the cut falls depends on where the page is. in the middle of the tree postgres splits about 50/50. on the rightmost leaf it assumes keys are arriving in order and keeps the left page 90% full (the fillfactor), so a sequential load leaves dense pages behind instead of half-empty ones.

the picture

you know how a B+tree grows: a full page splits and pushes a key up. this chapter is about a small question with a big effect: where do you cut?

think of filing cards into a box of folders. a folder in the middle overflows, so you split it in half: both halves have room for whatever comes next, because cards can arrive anywhere. but if you're filing invoices numbered 1, 2, 3, … every new card goes in the last folder. split that one in half and the left half never gets another card: it stays half empty forever. so for the last folder, you leave the left one nearly full and start a fresh one.

here's postgres doing exactly that. even keys first, so there are gaps to fill later.

T1INSERT INTO e SELECT g * 2 FROM generate_series(1, 407) AS g
246… 401 more810812814root,a leaf
child and sibling linkshigh key

407 even keys, 2 to 814, in ascending order: the only page is full, with 8 bytes to spare.

step 1/4
real output, postgres 18.6 (PGlite 0.5.12) · leaf-split
step through four inserts into a real index. the first split is on the rightmost leaf: 366 keys stay, 42 move. the second is in the middle of the tree: about 200 each way, and the new page (4) is linked in between pages 1 and 2. the left page's high key changes with each split.

what a split does

an int4 leaf holds 407 entries (8,152 usable bytes, 20 bytes per entry with its line pointer). at the 408th, postgres:

  1. picks a split point, and a separator: the first key that goes right. (with several columns, only as many columns as it needs to tell the halves apart.)
  2. writes the right half to a new page, and the left half back. the left page's high key becomes the separator, and the new page inherits the old high key.
  3. links the new page in: left → new → old right neighbour, both directions.
  4. inserts a downlink for the new page into the parent. if the parent is full, it splits too, one level up. if there was no parent (the page was the root), a new root appears above, and the tree is one level taller.

steps 2 and 3 happen with the pages locked, and step 4 happens after. for a moment the new page exists with no downlink in the parent, and a reader coming down the tree can only find it through the left page's right link. that's on purpose: b-trees under concurrency is about that moment.

90/10 at the right edge

predict the output
you insert ids 1 to 100,000 in order into a table with an index. how full are the index's leaves afterwards?
sql
CREATE TABLE seq (id int);
CREATE INDEX seq_id ON seq (id);
INSERT INTO seq SELECT g FROM generate_series(1, 100000) AS g;

measured on three indexes with the same 100,000 keys: inserted in order, inserted in a scrambled order (sorted by their hash, so the same every run), and built by CREATE INDEX after the scrambled load. pct_full is used bytes per leaf:

sqlempty db · ext=pageinspect
CREATE TABLE seq (id int);
CREATE INDEX seq_id ON seq (id);
INSERT INTO seq SELECT g FROM generate_series(1, 100000) AS g;
CREATE TABLE rnd (id int);
CREATE INDEX rnd_id ON rnd (id);
INSERT INTO rnd SELECT g FROM generate_series(1, 100000) AS g ORDER BY hashint4(g);
CREATE TABLE built (id int);
INSERT INTO built SELECT g FROM generate_series(1, 100000) AS g ORDER BY hashint4(g);
CREATE INDEX built_id ON built (id);
SELECT i AS index,
       count(*) FILTER (WHERE s.type = 'l') AS leaves,
       round(avg(s.live_items) FILTER (WHERE s.type = 'l')) AS avg_items,
       round(100 - 100.0 * avg(s.free_size) FILTER (WHERE s.type = 'l') / 8160) AS pct_full,
       pg_size_pretty(pg_relation_size(i)) AS size
FROM unnest(ARRAY['seq_id', 'rnd_id', 'built_id']) AS i,
     bt_multi_page_stats(i, 1, -1) AS s
GROUP BY i
ORDER BY leaves;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE INDEX
#3 · INSERT 0 100000
#4 · CREATE TABLE
#5 · CREATE INDEX
#6 · INSERT 0 100000
#7 · CREATE TABLE
#8 · INSERT 0 100000
#9 · CREATE INDEX
#10 · SELECT 3
indextextleavesint8avg_itemsnumericpct_fullnumericsizetext
built_id274366902208 kB
seq_id274366902208 kB
rnd_id318315772560 kB
(3 rows)
built by CREATE INDEX274 leaves
inserted in order274 leaves
inserted scrambled318 leaves
leaf pages for the same 100,000 int keys, from the run block above.

inserting in order ends up exactly like a fresh build: 274 leaves of 366 keys, 90% full. the scrambled inserts split in the middle again and again, and their leaves settle at 77%, with 16% more pages. that's the classic picture for random inserts into a B-tree: pages hover between half and completely full, and the textbook average (A. Yao's 1978 analysis of random insertions) is ln 2, about 69%. postgres's midpoint choice does a bit better: it picks the cut that balances the free space on both sides, not the item count.

the model below plays the same game with 10 keys a page, under postgres's rules. pick a preset and watch the "full leaves" number.

rules =
keys =
key =
emptyroot,a leaf
height 1 · leaves 1 · keys 0 · full leaves 0%

emptyan empty index is one leaf page with room for 10 keys.

step 1/85
model of a B+tree, postgres split rules, 10 keys a page · checked against postgres 18.6
the BTree model with postgres's split rules and 10 keys a page. in order, every split keeps 9 on the left; shuffled, the splits fall in the middle and the leaves end up emptier. high keys are meta.

the rule (nbtsplitloc.c in the postgres source) is: on the rightmost leaf, split at the fillfactor, wherever the new key goes. the first capture showed it: even after the split, 42 keys went right, not 1. upper levels use 70% instead of 90. and there's a smarter case for pages in the middle: when the new key lands right after the one inserted just before it on that page (an ascending run inside, say, a (customer_id, created_at) index), postgres splits just after the new key, so each run packs densely too.

fillfactor

the 90 isn't magic. it's the index's fillfactor, which CREATE INDEX uses for every leaf it builds and the rightmost split uses afterwards:

sqlempty db · ext=pageinspect
CREATE TABLE t AS SELECT g AS id FROM generate_series(1, 100000) AS g;
CREATE INDEX ff100 ON t (id) WITH (fillfactor = 100);
CREATE INDEX ff90 ON t (id);
CREATE INDEX ff50 ON t (id) WITH (fillfactor = 50);
CREATE TABLE app (id int);
CREATE INDEX app_ff100 ON app (id) WITH (fillfactor = 100);
INSERT INTO app SELECT generate_series(1, 100000);
SELECT i AS index, pg_relation_size(i) / 8192 AS pages,
       round(avg(s.live_items) FILTER (WHERE s.type = 'l')) AS avg_items
FROM unnest(ARRAY['ff100', 'ff90', 'ff50', 'app_ff100']) AS i,
     bt_multi_page_stats(i, 1, -1) AS s
GROUP BY i
ORDER BY pages, i;
output, from postgres 18.6
#1 · SELECT 100000
#2 · CREATE INDEX
#3 · CREATE INDEX
#4 · CREATE INDEX
#5 · CREATE TABLE
#6 · CREATE INDEX
#7 · INSERT 0 100000
#8 · SELECT 4
indextextpagesint8avg_itemsnumeric
app_ff100249406
ff100249406
ff90276366
ff50500203
(4 rows)

249 pages packed full, 276 at the default, 500 at half. app_ff100 was never built, only inserted into in order, and it came out identical to ff100: the rightmost split follows the setting too.

so why not always 100? because then the first insert into the middle of any leaf splits it. the default 10% is room for late arrivals. watch it absorb 10,000 of them:

sqlempty db · ext=pageinspect
CREATE TABLE late (id int);
CREATE INDEX late_id ON late (id);
INSERT INTO late SELECT g * 10 FROM generate_series(1, 100000) AS g;
INSERT INTO late SELECT g * 100 + 5 FROM generate_series(1, 10000) AS g;
SELECT count(*) FILTER (WHERE type = 'l') AS leaves,
       round(avg(live_items) FILTER (WHERE type = 'l')) AS avg_items
FROM bt_multi_page_stats('late_id', 1, -1);
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE INDEX
#3 · INSERT 0 100000
#4 · INSERT 0 10000
#5 · SELECT 1
leavesint8avg_itemsnumeric
274402
(1 row)

still 274 leaves, now with 402 keys each. every leaf took its share of the newcomers into the 808 bytes it had spare, and nothing split.

lower the fillfactor for an index whose column gets lots of inserts in the middle of its range. set it to 100 for one that is only ever appended to, or never changes. (fillfactor is the same idea for heap pages, where the spare room is for updates.)

one level deeperwhat splits don't do

nbtree never merges two half-empty pages back together, and never shifts entries to a neighbour to avoid a split (some engines do both). a page leaves the tree only when it's completely empty: VACUUM unlinks it from its siblings and parent, and recycles it once no running scan can still be looking at it. so an index that had a lot of deletes stays as big as it was until new keys fill the gaps, or a REINDEX rebuilds it. before splitting, though, postgres does try to make room: it removes entries already marked dead, then tries deduplication and bottom-up deletion. a split is the last resort.

keylines

  • a full leaf splits: part of it moves to a new page, linked in beside it, with a downlink added to the parent.
  • in the middle of the tree the cut is about 50/50; on the rightmost leaf it's at the fillfactor, 90/10.
  • sequential inserts leave leaves 90% full, like a fresh build. scrambled inserts settle around 77%.
  • fillfactor sets both: the build and the rightmost split. 10% spare absorbs late inserts without splits.
  • pages are never merged. an emptied index stays big until it's reused or rebuilt.