0%
PostgreSQL Deep Dive: MVCC, Vacuum, Plans, and Replication
Architecture and Storage
MVCC and Vacuum
Concurrency and Schema
B-tree Indexes in Practice
The B-tree is the default index type in PostgreSQL and the right choice for most indexes: equality and range lookups, sorting, uniqueness and primary keys. Its internal design, based on the Lehman and Yao concurrent B-tree with several Postgres specific additions, determines how fast it is, how it behaves under concurrency and how it bloats. This section explains the structure of a Postgres B-tree on disk and how inserts and page splits work, which is the foundation for choosing and maintaining indexes well.
Pages and levels
A B-tree index is a file of 8 kilobyte pages, like a table. Page zero is the metapage, which records where the root is and the height of the tree. Above the leaves are internal pages, whose entries hold a separator key and a pointer to a child page. Leaf pages hold the index keys and, for each, the tuple identifier of a heap row. A lookup starts at the root, follows separators down through internal levels, and reaches the leaf that would contain the key. Because each page holds hundreds of entries, even billions of rows need only four or five levels.
Leaf pages are a sorted linked list
Leaf pages are linked to their left and right siblings, so the leaf level forms a doubly linked list in key order. A range scan descends once to the first matching key and then walks right along the leaf level until keys pass the upper bound, without returning to the root. Backward scans walk left. This is what makes B-trees good at ranges, ORDER BY and LIMIT queries: the index delivers rows in order, and the scan can stop as soon as it has enough.
High keys and right links
Each page except the rightmost on its level stores a high key, an upper bound on keys in that page. This is the Lehman and Yao design: if a concurrent split moved the key a reader is looking for to a new right sibling, the reader sees that its target is beyond the high key and follows the right link instead of restarting from the root. This lets readers descend without holding locks on parent pages, only a brief share lock on each page as they read it, which is a large part of why Postgres B-trees scale well under concurrency.
Inserts and page splits
An insert descends to the leaf where the new key belongs and adds an entry, keeping the page sorted. If the leaf is full, it splits: about half the entries move to a new page, the left page gets a new high key, the new page is linked in, and a separator for the new page is inserted into the parent, which may itself split, all the way up to the root, which splitting makes the tree one level taller. Splits are WAL logged and are among the more expensive index operations, so their frequency matters for write heavy indexes.
Rightmost splits for increasing keys
When keys are inserted in increasing order, such as an identity column or a timestamp, every insert goes to the rightmost leaf. Splitting that page in half would leave the left half permanently half empty, since no smaller keys will arrive. Postgres detects inserts at the rightmost position and splits so the left page stays nearly full, which is close to how the index fillfactor of 90 is applied during builds. Indexes on increasing keys therefore stay compact, while indexes on random keys such as UUIDs version 4 split in the middle and settle around 70 percent full.
Random keys and the cost of UUIDs
Random keys spread inserts across all leaf pages, so a high insert rate touches many pages, each likely to be a cache miss once the index is larger than memory, each producing a full page image after every checkpoint. An index on a random UUID can generate far more WAL and I/O per insert than one on a sequential key. Time ordered identifiers, such as UUID version 7, which Postgres 18 can generate natively with uuidv7, keep the uniqueness benefits while inserting near the right edge of the index.
Duplicates and deduplication
Before version 13, each duplicate key had its own leaf entry with the full key repeated, so an index on a low cardinality column stored the same value many times. Version 13 added deduplication: when a leaf page would split, Postgres first tries to merge entries with equal keys into a posting list, one key followed by many tuple identifiers. This can shrink indexes with many duplicates by a factor of several. It is enabled by default and can be disabled per index with the deduplicate_items storage parameter, which is rarely needed.
Heap tuple id as a tiebreaker
Since version 12, Postgres treats the heap tuple identifier as a final key column in B-tree leaf entries, so every entry is unique and entries with equal user keys are ordered by heap location. This makes page splits choose better split points, lets vacuum find specific entries quickly, and improves locality when scanning duplicates. It also enables suffix truncation of separator keys in internal pages, which stores only as much of a key as needed to separate two pages, making internal pages hold more separators and trees shorter.
What the metapage and fast root do
After deletions shrink the upper levels, the true root may sit above a chain of single child pages. The metapage records a fast root, the lowest page that still has more than one child, so lookups can start there and skip useless levels. The pageinspect function bt_metap shows the root, fast root and level. These details are rarely important in practice, but they explain why index height in bt_metap can differ from the number of levels a lookup actually visits.
Index size versus table size
A B-tree leaf entry holds the key, a tuple identifier of six bytes and an eight byte index tuple header, aligned to eight bytes. An index on a single bigint column therefore uses about 16 to 20 bytes per row in leaves plus page overhead, often a quarter or more of a narrow table size. Several indexes on a narrow table can together exceed the table itself. This is normal, and it is why index count and width matter as much as table design for storage and cache planning.
Locks during index operations
Inserts and lookups in a B-tree take short lived buffer locks on individual pages, not table level locks, so many sessions can insert concurrently into different parts of the index. Contention appears when many sessions insert into the same leaf page, typically the rightmost page of an index on an increasing key, where all inserts converge. At very high insert rates this hot page becomes a bottleneck visible as buffer content lock waits. Hash partitioning or slightly randomized keys spread the inserts, at the cost of the benefits of sequential keys.
Deletes and empty pages
When vacuum removes every entry from a leaf page, it can delete the page from the tree, unlinking it from its siblings and removing its separator from the parent, and later recycle it for new splits once no scan can still be on it. Pages that are not completely empty stay in the tree however sparse they are. This is why mass deletes of old rows in an index on an increasing key leave the left side of the index full of near empty pages until enough of them become fully empty.
Other index types build on the same ideas
Hash, GiST, SP-GiST, GIN and BRIN indexes share the page format and access method interface but organize entries differently. B-tree is the only built in type that supports ordering, uniqueness and range queries on scalar types together, which is why it is the default and covers the large majority of indexes. The next lesson covers when the other types are a better fit.
Postgres B-trees are Lehman and Yao trees with right links and high keys, so readers do not lock parents. Leaves are a linked list for ranges and ordering. Increasing keys split at the right edge and stay compact, while random keys split in the middle and cause more I/O per insert.