The idea · Lab 3.2.6An index that nobody queries through is not neutral — it still has to be updated on every insert, update, and delete against its table, paying real write cost for zero read benefit. And an index that IS queried can still degrade over time: as rows are updated and deleted, a B-tree accumulates dead space inside its pages, the same bloat process VACUUM handles for table heaps, except an index needs its own accounting to detect. Both problems are measurable directly rather than guessed at: pg_stat_user_indexes.idx_scan is a genuine, cumulative counter of how many times an index has actually been used, and pgstattuple's pgstatindex gives a genuine bloat percentage for any index, not an estimate from table-level statistics.
Fixing bloat safely matters as much as detecting it. A plain REINDEX takes an exclusive lock for its duration, blocking every read and write against the table — unacceptable on a live production table. REINDEX CONCURRENTLY avoids that lock by building a brand new index alongside the old one and swapping them once ready, at the cost of taking noticeably longer and being interruptible — and being interrupted has a real, specific consequence this lab reproduces directly rather than just describing.
Finding Indexes Nobody Uses
SELECT indexrelname, idx_scan FROM pg_stat_user_indexes WHERE relname = 'idx_maint';
indexrelname | idx_scan
------------------+----------
idx_maint_pkey | 0
idx_maint_unused | 0
idx_maint_used | 2
idx_scan is a real, cumulative counter — it only increments when a query actually uses that specific index. Two of these three indexes show 0: genuinely never used since the counter was last reset, paying full write-maintenance cost for zero read benefit.
Measuring Real Bloat, Not Guessing
CREATE EXTENSION IF NOT EXISTS pgstattuple;
SELECT * FROM pgstatindex('idx_maint_unused');
index_size | leaf_pages | avg_leaf_density | leaf_fragmentation
------------+------------+------------------+---------------------
2342912 | 284 | 64.64 | 38.38
2.2 MB, with leaf_fragmentation at 38.38% — real, page-level bloat measured directly from the index's own internal structure, not inferred from table-level row-count changes.
Rebuilding Online, With No Table Lock
REINDEX INDEX CONCURRENTLY idx_maint_unused;
SELECT * FROM pgstatindex('idx_maint_unused');
index_size | leaf_pages | avg_leaf_density | leaf_fragmentation
------------+------------+------------------+---------------------
917504 | 110 | 89.41 | 0
896 kB, 0% fragmentation — a genuine rebuild, and CONCURRENTLY means it happened without ever taking the exclusive lock a plain REINDEX would have needed, at the cost of a longer runtime.
What a Cancelled REINDEX CONCURRENTLY Actually Leaves Behind
Cancelling a REINDEX INDEX CONCURRENTLY partway through — reproduced here with pg_cancel_backend from a second session — does not clean up after itself:
SELECT relname, indisvalid FROM pg_class JOIN pg_index ON pg_class.oid = pg_index.indexrelid
WHERE relname LIKE 'idx_maint2%';
relname | indisvalid
----------------------+------------
idx_maint2_val | t
idx_maint2_val_ccnew | f
The original index is untouched and still valid. A new, half-built index named with a _ccnew suffix is left behind, permanently marked invalid — it will never be considered by the planner, but it still occupies disk space and needs to be dropped explicitly:
DROP INDEX CONCURRENTLY idx_maint2_val_ccnew;
Key concepts
idx_scan
A column in pg_stat_user_indexes counting how many times PostgreSQL has used a specific index to answer a query, since statistics were last reset. A persistent 0 across a realistic observation window is strong direct evidence an index is not earning back its write-maintenance cost — far more reliable than guessing from the index's definition alone.
REINDEX CONCURRENTLY
Rebuilds an index without taking the exclusive lock a plain REINDEX requires, by building an entirely new index alongside the old one while normal reads and writes continue, then swapping them atomically once the new one is ready and valid. It takes longer than a plain REINDEX and cannot run inside a transaction block; if interrupted before completing, it leaves a permanently invalid, unused index behind that must be dropped manually — the original index is never harmed.
What to remember
- pg_stat_user_indexes.idx_scan is a real, cumulative counter — a persistent 0 is direct evidence an index is not earning back its write-maintenance cost
- pgstatindex measures real, page-level bloat directly from an index's own structure, rather than inferring it from table-level statistics
- REINDEX CONCURRENTLY rebuilds without ever taking the exclusive lock a plain REINDEX needs, at the cost of a longer runtime
- An interrupted REINDEX CONCURRENTLY never harms the original index — it always survives untouched — but leaves a real, permanently invalid _ccnew index behind that needs an explicit DROP INDEX CONCURRENTLY
- Checking for leftover invalid indexes after any interrupted concurrent rebuild is a real operational habit, not a hypothetical edge case