Estimate index bloat in PostgreSQL
A bloated index wastes cache and disk without any error to tell you. pgstatindex reads the actual leaf-page fill so you can decide which indexes need a rebuild.
The task
B-tree indexes bloat after heavy update and delete activity: page splits and removed entries leave leaf pages partly empty, and a PostgreSQL B-tree gives a page back only once it is completely empty; half-full pages stay half full. The index ends up larger than its live entries need. pgstatindex, from the pgstattuple extension, reads every page of one B-tree index and reports avg_leaf_density. A freshly built index sits near 90 percent (the default fillfactor); 50 percent means half of every leaf page is empty.
The snippet
Written for PostgreSQL.
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT pg_size_pretty(index_size) AS index_size,
leaf_pages, empty_pages, deleted_pages,
avg_leaf_density, leaf_fragmentation
FROM pgstatindex('public.orders_created_at_idx');
Fonctionnement
pgstatindex reads the whole index, so on a 50 GB index it does 50 GB of I/O; run it on candidates, not on everything. avg_leaf_density is the average fill of leaf pages, and a value well below 90 percent means the index would shrink if rebuilt, roughly in proportion (an index at 45 percent comes back at about half the size). leaf_fragmentation is the share of leaf pages out of physical order, which matters for range scans on spinning disks and much less on SSDs. The workflow: list the largest indexes that are actually used (the variant), check the suspicious ones, then rebuild the genuinely bloated ones with REINDEX INDEX CONCURRENTLY. CREATE EXTENSION needs a role allowed to create extensions in the database; calling pgstatindex needs superuser or membership in pg_stat_scan_tables.
Variations
List the biggest busy indexes to check first
SELECT s.schemaname || '.' || s.indexrelname AS index_name,
pg_size_pretty(pg_relation_size(s.indexrelid)) AS size, s.idx_scan
FROM pg_stat_user_indexes s
WHERE s.idx_scan > 0
AND pg_relation_size(s.indexrelid) > 1024 * 1024 * 1024
ORDER BY pg_relation_size(s.indexrelid) DESC
LIMIT 20;
Rebuild a bloated index without blocking writes
REINDEX INDEX CONCURRENTLY public.orders_created_at_idx;
Questions that come up
What causes bloat besides deletes?
Page splits from random-order inserts (random UUID keys), updates that change indexed columns (each one adds a new index entry and leaves a dead one), and long-running transactions, which stop vacuum from removing dead index entries until they finish. All three show up as a falling avg_leaf_density.
Is REINDEX CONCURRENTLY safe to run in production?
Yes, with three costs to plan for. It builds a new copy without blocking reads or writes and then swaps it in, so it needs free disk for a second copy of the index. It takes longer than a plain REINDEX because it scans the table twice and waits for running transactions. If it fails, it leaves an invalid index with a _ccnew suffix that you should drop.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.