Sub topic 12
Fill factor, fragmentation, and index bloat
This chapter assumes you know how B+tree pages split on insert and merge on delete, and that you can read a basic EXPLAIN plan. It covers how an index's physical state drifts from its ideal shape and what you can do about it.
Fill factor
Fill factor is the percentage of each leaf page that the database fills when it builds or rebuilds an index. The rest is left empty on purpose. A page that is 100% full must split on the next insert into it. A page with spare room absorbs several inserts first.
In PostgreSQL, B-tree indexes default to a fill factor of 90. You can set it per index:
CREATE INDEX orders_customer_created_idx
ON orders (customer_id, created_at)
WITH (fillfactor = 70);The trade-off is direct:
- A lower fill factor means fewer splits during random inserts, but more pages to read and cache for the same number of keys.
- A higher fill factor packs the index tightly, which suits read-only or append-only data.
For an index on an ever-increasing key such as a timestamp, inserts hit only the rightmost leaf. Extra free space on older pages is never used, so a high fill factor is the better choice there. For a random key such as a UUID, inserts land everywhere, and some headroom pays off.
Fragmentation
Fragmentation means the physical state of the index no longer matches its logical order. There are two kinds.
- Internal fragmentation: pages hold less data than they could. Splits leave two half-full pages, and deletes leave pages sparsely filled.
- External fragmentation: leaf pages that are next to each other in key order are far apart on disk. A range scan that follows the leaf links then jumps around instead of reading sequentially.
External fragmentation matters most on spinning disks. On SSDs the penalty is smaller, but internal fragmentation still costs you because more pages must be read for the same range.
Index bloat
Bloat is the extra space an index occupies beyond what its live entries need. It comes from the same mechanisms as fragmentation, plus one more. In PostgreSQL, an UPDATE or DELETE leaves the old index entries in place until VACUUM removes them. Vacuum frees entries inside pages for reuse, but it rarely merges sparse pages or shrinks the file. A table with heavy churn can therefore end up with an index several times larger than needed.
Measuring and fixing
To see actual size, use built-in functions. For page density, the pgstattuple extension reports it.
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT pg_size_pretty(pg_relation_size('orders_customer_created_idx')) AS size;
SELECT avg_leaf_density, leaf_fragmentation
FROM pgstatindex('orders_customer_created_idx');A low avg_leaf_density (well under 70) suggests wasted space. To fix it, rebuild the index:
REINDEX INDEX CONCURRENTLY orders_customer_created_idx;CONCURRENTLY (PostgreSQL 12 and later) builds a new copy without blocking writes, at the cost of more time and temporary disk space. Plain REINDEX takes a lock that blocks writes to the table.
Common errors
- Setting a very low fill factor on an append-only index, which wastes space with no benefit.
- Assuming
VACUUMshrinks indexes. It reuses space but usually does not return it to the operating system. - Running a blocking
REINDEXon a busy production table.
Exercise
An index is on a column holding random UUIDs, and the table receives constant inserts. A second index is on an auto-incrementing id, with inserts only at the end. Which index benefits more from a lower fill factor, and why?
Show answer
The UUID index. Its inserts land on random leaf pages, so free space in each page absorbs inserts and delays splits. The auto-incrementing index only writes to the rightmost page, so free space on older pages is never used and a high fill factor is better.
Exercise
Your index is three times larger than the live data needs, and VACUUM has already run. Which command rebuilds it without blocking writes in PostgreSQL 12 or later?
Show answer
REINDEX INDEX CONCURRENTLY index_name;