Sub topic 12
Index Bloat and `REINDEX CONCURRENTLY`
Prerequisites
This chapter assumes that you understand PostgreSQL indexes, basic transactions, and the difference between a table and its indexes. You should also know how to connect with psql and run SQL statements.
How index bloat forms
An index stores search entries that point to table rows. PostgreSQL uses multiversion concurrency control (MVCC), so an UPDATE usually creates a new row version instead of changing the old version in place. The index may also receive a new entry.
When the old row version becomes invisible to every transaction, VACUUM can mark the related index entry as reusable. The index page remains allocated, however. PostgreSQL usually reuses that space for later entries instead of returning it to the operating system.
Index pages can also become inefficient after inserts split pages, or when updates produce entries in different parts of the index. This unused or poorly arranged space is called index bloat. A bloated index takes more disk space and requires more page reads during scans.
Measure before rebuilding
Start with estimated statistics from pg_stat_user_indexes and relation sizes. A large index is not automatically bloated. A large table, a high-cardinality column, or a necessary covering index can explain its size.
SELECT indexrelname,
idx_scan,
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_stat_user_indexes
WHERE schemaname = 'public'
ORDER BY pg_relation_size(indexrelid) DESC;For a closer look at a B-tree index, install pgstattuple if you have permission. Its pgstatindex function reads the index and reports internal details such as leaf-page density and fragmentation.
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT version,
tree_level,
index_size,
avg_leaf_density,
leaf_fragmentation
FROM pgstatindex('public.orders_customer_id_idx');Use these figures with workload history. A low scan count may mean that an index is unnecessary, not that it needs rebuilding. Confirm usage with query plans and application requirements before dropping anything.
What `REINDEX CONCURRENTLY` does
REINDEX CONCURRENTLY builds a replacement index while ordinary reads and writes continue. After the replacement is valid, PostgreSQL swaps it with the old index and removes the old one. This can reduce index bloat without the long write lock caused by ordinary REINDEX.
REINDEX INDEX CONCURRENTLY public.orders_customer_id_idx;The operation needs extra disk space because both the old and new indexes exist during the rebuild. It also performs more work than a normal index build and can take longer. Run it outside an explicit transaction block, and make sure your role has the required permissions.
A concurrent rebuild still takes short locks during setup and the final swap. Long-running transactions can delay those phases. Monitor progress and disk space rather than assuming that “concurrent” means “lock-free.”
| Operation | Table writes during most of the operation | Extra disk space | Main use |
|---|---|---|---|
| `REINDEX` | Blocked | Required | Fast rebuild during maintenance |
| `REINDEX CONCURRENTLY` | Usually allowed | Required, usually more | Rebuild with low application disruption |
> callout warning > Do not rebuild every large index on a schedule. Measure first, check whether the index is used, and account for the temporary disk space and I/O load.
Choosing the maintenance action
VACUUM removes dead index entries when it can, but it normally does not shrink the index file. Use REINDEX CONCURRENTLY when the index structure itself has accumulated enough wasted space to justify a rebuild. If the table is also bloated, rebuilding only its indexes will not solve the table-space problem. Consider a coordinated maintenance operation such as pg_repack when you need to address both.
Exercise
An index is large, has low leaf density, and the application cannot tolerate a long write lock. Which command is the best first rebuild choice, and what limitation must you check?
Show answer
Use REINDEX INDEX CONCURRENTLY. Check available disk space because PostgreSQL builds the replacement while retaining the old index, and check for long-running transactions that could delay the final swap.