skip to content

deduplication and bottom-up deletion

posting lists for repeated keys, and clearing out old versions before resorting to a split.

address
everything.index_internals.dedup_and_bottom_up_deletion
part
part 13, inside indexes
chapter
chapter 5 of 12
reading time
11 min

two ways a b-tree avoids splitting. deduplication pg 13+ stores a key that repeats once, followed by a posting list of row addresses, instead of once per row. bottom-up deletion pg 14+ handles the other kind of duplicate: the stale entries an UPDATE leaves behind in indexes whose key it didn't change. before a leaf splits, postgres checks the heap, finds the versions nobody can see anymore, and deletes their entries instead.

the picture

you know how a split is the only way a full leaf gets more room? it's also permanent: pages are never merged back. so before it splits a leaf, postgres tries two cheaper ways to free space on it, and both are about duplicates.

think of a guest list for a hotel. a family of six all named Patel: you could write "Patel, room 12" six times, or "Patel: rooms 12, 12, 14, 14, 15, 15" once (deduplication). and when guests check out and new ones check in to the same room, the old lines are crossed out in pen but still take space. before buying a new page for the list, you go round and erase every line for a guest who's already gone (bottom-up deletion).

d_dedup, leaf 1: 808-byte posting tuples
0 (52,128)10 ×13220 ×13230 ×1324… 
d_plain, leaf 1: one 16-byte tuple per row
0 (16,44)10 → (0,10)20 → (0,20)30 → (0,30)4… 
real output, postgres 18.6 (PGlite 0.5.12) · the run blocks below
the first items of leaf 1 of two indexes on the same column, ten distinct values over 100,000 rows. with deduplication, one tuple holds a key and 132 row addresses; without it, every row gets its own 16-byte tuple. both pages start with their high key.

deduplication

ten values over 100,000 rows, indexed twice, with deduplication (the default) and without:

sqlempty db · ext=pageinspect · session dd
CREATE TABLE d AS SELECT g AS id, g % 10 AS k FROM generate_series(1, 100000) AS g;
CREATE INDEX d_dedup ON d (k);
CREATE INDEX d_plain ON d (k) WITH (deduplicate_items = off);
SELECT i AS index, pg_relation_size(i) / 8192 AS pages
FROM unnest(ARRAY['d_dedup', 'd_plain']) AS i
ORDER BY pages;
output, from postgres 18.6
#1 · SELECT 100000
#2 · CREATE INDEX
#3 · CREATE INDEX
#4 · SELECT 2
indextextpagesint8
d_dedup87
d_plain278
(2 rows)

87 pages against 278: a third of the size. here's what's in them:

sqlempty db · ext=pageinspect · session dd
continues session dd (1 earlier block run first)
SELECT itemoffset, itemlen, cardinality(tids) AS rows, htid, data
FROM bt_page_items('d_dedup', 1)
WHERE itemoffset <= 4
ORDER BY itemoffset;
SELECT itemoffset, itemlen, htid, data
FROM bt_page_items('d_plain', 1)
WHERE itemoffset <= 4
ORDER BY itemoffset;
output, from postgres 18.6
#1 · SELECT 4
itemoffsetint2itemlenint2rowsint4htidtiddatatext
124null(52,128)00 00 00 00 00 00 00 00
2808132(0,10)00 00 00 00 00 00 00 00
3808132(5,200)00 00 00 00 00 00 00 00
4808132(11,164)00 00 00 00 00 00 00 00
(4 rows)
#2 · SELECT 4
itemoffsetint2itemlenint2htidtiddatatext
124(16,44)00 00 00 00 00 00 00 00
216(0,10)00 00 00 00 00 00 00 00
316(0,20)00 00 00 00 00 00 00 00
416(0,30)00 00 00 00 00 00 00 00
(4 rows)

with deduplication, item 2 is one key followed by 132 heap addresses, sorted, in one 808-byte tuple: 16 bytes for the key and header, then 6 bytes per address. the same 132 rows cost 132 × 16 = 2,112 bytes the plain way, plus 132 line pointers. and look at the high keys: both carry a heap address. the key 0 spans several pages, so the boundary between two leaves is a (key, address) pair, and the address is part of the key (a real b-tree, page by page has more on that).

predict the output
the same experiment on a unique column: 100,000 distinct ids, with and without deduplication. how big is the deduplicated index?
sql
CREATE INDEX u_dedup ON u (id);
CREATE INDEX u_plain ON u (id) WITH (deduplicate_items = off);
sqlempty db · ext=pageinspect · session dd
continues session dd (2 earlier blocks run first)
CREATE TABLE u AS SELECT g AS id FROM generate_series(1, 100000) AS g;
CREATE INDEX u_dedup ON u (id);
CREATE INDEX u_plain ON u (id) WITH (deduplicate_items = off);
SELECT i AS index, pg_relation_size(i) / 8192 AS pages
FROM unnest(ARRAY['u_dedup', 'u_plain']) AS i
ORDER BY i;
output, from postgres 18.6
#1 · SELECT 100000
#2 · CREATE INDEX
#3 · CREATE INDEX
#4 · SELECT 2
indextextpagesint8
u_dedup276
u_plain276
(2 rows)

276 and 276. two more things to know:

  • it's lazy. an insert adds a plain tuple. only when the leaf is full does postgres run a deduplication pass over it, merging runs of equal keys into posting lists, and split only if that didn't free enough. CREATE INDEX deduplicates as it builds.
  • some types can't. merging two entries is only safe when "equal" means "the same bytes". numeric isn't (1.0 and 1.00 are equal and stored differently), and neither are float, jsonb, or text under a nondeterministic collation. the metapage says whether an index qualifies, in allequalimage:
sqlempty db · ext=pageinspect · session dd
continues session dd (3 earlier blocks run first)
CREATE TABLE n AS SELECT g AS id, (g % 10)::numeric AS k FROM generate_series(1, 100000) AS g;
CREATE INDEX n_k ON n (k);
SELECT 'd_dedup' AS index, allequalimage, pg_relation_size('d_dedup') / 8192 AS pages FROM bt_metap('d_dedup')
UNION ALL
SELECT 'n_k', allequalimage, pg_relation_size('n_k') / 8192 FROM bt_metap('n_k')
ORDER BY index;
output, from postgres 18.6
#1 · SELECT 100000
#2 · CREATE INDEX
#3 · SELECT 2
indextextallequalimageboolpagesint8
d_dedupt87
n_kf278
(2 rows)

the numeric index is the size of the plain one: same ten values, no posting lists. (INCLUDE indexes never deduplicate either.)

bottom-up deletion

an UPDATE that isn't HOT writes a new row version on another page, and every index on the table gets a new entry pointing at it, including indexes whose column didn't change. that index now has two entries with the same key, one for the old version and one for the new. after the old version is dead, its entry is garbage, but it sits there until VACUUM, and a busy row piles up a lot of them. postgres calls this version churn.

bottom-up deletion is aimed at exactly that. when a leaf of an index that the UPDATE didn't logically change is about to split, postgres looks at the heap pages its duplicate entries point to, checks which versions are dead to everyone, and deletes those entries. if that frees enough room, no split.

here's churn on purpose: ch has an index on a, which never changes, and one on b, which every update changes, so updates can't be HOT. each pass updates every row once, a tenth at a time:

sqlempty db · session churn
CREATE TABLE ch (id int PRIMARY KEY, a int, b int);
CREATE INDEX ch_a ON ch (a);
CREATE INDEX ch_b ON ch (b);
INSERT INTO ch SELECT g, g, 0 FROM generate_series(1, 10000) AS g;
CREATE TABLE sizes (pass int, pkey bigint, a bigint, b bigint);
INSERT INTO sizes SELECT 0, pg_relation_size('ch_pkey') / 8192, pg_relation_size('ch_a') / 8192, pg_relation_size('ch_b') / 8192;
-- one pass: every row updated once, a tenth at a time
UPDATE ch SET b = b + 1 WHERE id % 10 = 0;
UPDATE ch SET b = b + 1 WHERE id % 10 = 1;
UPDATE ch SET b = b + 1 WHERE id % 10 = 2;
UPDATE ch SET b = b + 1 WHERE id % 10 = 3;
UPDATE ch SET b = b + 1 WHERE id % 10 = 4;
UPDATE ch SET b = b + 1 WHERE id % 10 = 5;
UPDATE ch SET b = b + 1 WHERE id % 10 = 6;
UPDATE ch SET b = b + 1 WHERE id % 10 = 7;
UPDATE ch SET b = b + 1 WHERE id % 10 = 8;
UPDATE ch SET b = b + 1 WHERE id % 10 = 9;
INSERT INTO sizes SELECT 1, pg_relation_size('ch_pkey') / 8192, pg_relation_size('ch_a') / 8192, pg_relation_size('ch_b') / 8192;
output, from postgres 18.6
#1 · CREATE TABLE
#2 · CREATE INDEX
#3 · CREATE INDEX
#4 · INSERT 0 10000
#5 · CREATE TABLE
#6 · INSERT 0 1
#7 · UPDATE 1000
#8 · UPDATE 1000
#9 · UPDATE 1000
#10 · UPDATE 1000
#11 · UPDATE 1000
#12 · UPDATE 1000
#13 · UPDATE 1000
#14 · UPDATE 1000
#15 · UPDATE 1000
#16 · UPDATE 1000
#17 · INSERT 0 1

eight passes later:

sqlempty db · session churn
continues session churn (8 earlier blocks run first)
SELECT * FROM sizes ORDER BY pass;
output, from postgres 18.6
SELECT 9
passint4pkeyint8aint8bint8
0303010
1373720
2575729
3575738
4575747
5575756
6575766
7576376
8577685
(9 rows)
ch_pkey (unchanged key)57 pages
ch_a (unchanged key)76 pages
ch_b (changed key)85 pages
index size in pages after 8 passes of churn over 10,000 rows, from the run blocks above. ch_b's key changes on every update, so its new entries are real new keys. ch_pkey and ch_a only gained stale duplicates, which got deleted instead of split.

ch_pkey and ch_a grow in the first two passes, from 30 to 57 pages, and then stop. ch_a holds at 57 pages for five passes while ch_b grows by 9 or 10 pages every pass. at pass 7 ch_a starts to split again: bottom-up deletion is a best effort that looks at a limited number of heap pages per attempt, and the manual says as much ("a page split will usually be avoided"). the primary key holds on longest because a unique index gets a second kind of help: every insert's uniqueness check already visits the old versions of that key in the heap, and marks the dead ones' entries for removal on the way.

it needs the old versions to be dead

"dead to everyone" is the catch. bottom-up deletion can only remove an entry whose heap version no snapshot can still see, the same rule as VACUUM. here is the same churn on a real server, with a second session holding a repeatable read snapshot open the whole time:

ch_a, nobody else looking57 pages
ch_a, snapshot held111 pages
ch_pkey, snapshot held111 pages
real output, a postgres 18.6 server · churn-held
the same churn, four passes, with and without another session holding an old snapshot. with it, no old version is removable, so nothing can be deleted and the indexes split as if bottom-up deletion didn't exist.

111 pages instead of 57. the extra pages stay after the snapshot is gone, because pages are never merged. a long-running transaction doesn't just hold back VACUUM (the xmin horizon): it switches off the index's own cleanup too, and leaves the splits behind as permanent bloat.

keylines

  • deduplication (13+) stores a repeated key once with a posting list of heap addresses: a third of the size for ten values over 100,000 rows.
  • it happens lazily, just before a leaf would split, and only for types where equal means byte-equal.
  • a non-HOT update adds an entry to every index, even ones whose key didn't change: version churn.
  • bottom-up deletion (14+) deletes those stale entries instead of splitting, when the old versions are dead to everyone.
  • an old snapshot switches it off, and the splits that follow are permanent.