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).
deduplication
ten values over 100,000 rows, indexed twice, with deduplication (the default) and without:
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;| indextext | pagesint8 |
|---|---|
| d_dedup | 87 |
| d_plain | 278 |
87 pages against 278: a third of the size. here's what's in them:
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;| itemoffsetint2 | itemlenint2 | rowsint4 | htidtid | datatext |
|---|---|---|---|---|
| 1 | 24 | null | (52,128) | 00 00 00 00 00 00 00 00 |
| 2 | 808 | 132 | (0,10) | 00 00 00 00 00 00 00 00 |
| 3 | 808 | 132 | (5,200) | 00 00 00 00 00 00 00 00 |
| 4 | 808 | 132 | (11,164) | 00 00 00 00 00 00 00 00 |
| itemoffsetint2 | itemlenint2 | htidtid | datatext |
|---|---|---|---|
| 1 | 24 | (16,44) | 00 00 00 00 00 00 00 00 |
| 2 | 16 | (0,10) | 00 00 00 00 00 00 00 00 |
| 3 | 16 | (0,20) | 00 00 00 00 00 00 00 00 |
| 4 | 16 | (0,30) | 00 00 00 00 00 00 00 00 |
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).
CREATE INDEX u_dedup ON u (id);
CREATE INDEX u_plain ON u (id) WITH (deduplicate_items = off);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;| indextext | pagesint8 |
|---|---|
| u_dedup | 276 |
| u_plain | 276 |
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".
numericisn't (1.0 and 1.00 are equal and stored differently), and neither arefloat,jsonb, or text under a nondeterministic collation. the metapage says whether an index qualifies, inallequalimage:
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;| indextext | allequalimagebool | pagesint8 |
|---|---|---|
| d_dedup | t | 87 |
| n_k | f | 278 |
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:
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;eight passes later:
SELECT * FROM sizes ORDER BY pass;| passint4 | pkeyint8 | aint8 | bint8 |
|---|---|---|---|
| 0 | 30 | 30 | 10 |
| 1 | 37 | 37 | 20 |
| 2 | 57 | 57 | 29 |
| 3 | 57 | 57 | 38 |
| 4 | 57 | 57 | 47 |
| 5 | 57 | 57 | 56 |
| 6 | 57 | 57 | 66 |
| 7 | 57 | 63 | 76 |
| 8 | 57 | 76 | 85 |
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:
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.