Sub topic 15
Statistics, Bloat, and Maintaining Indexes
Assume you know SELECT, WHERE, JOIN, primary keys, and basic EXPLAIN. This chapter uses PostgreSQL terms, but the general ideas apply to other relational databases.
An index can become less useful even when its definition is correct. The query planner may have stale information about the data, or the index may contain space and entries that no longer help searches. Maintenance keeps the planner's decisions and the index's physical structure aligned with the workload.
Statistics guide the query planner
The planner estimates how many rows each operation will produce. It uses those estimates to choose between a sequential scan, an index scan, and other plans.
ANALYZE collects statistics about a table's columns. The statistics include approximate value distributions, common values, and the estimated number of distinct values. PostgreSQL stores these statistics in its system catalogs.
ANALYZE orders;
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;If many rows were inserted, deleted, or updated since the last analysis, the old estimates can be wrong. A planner that expects ten matching rows may choose an index scan. If the query actually returns half the table, a sequential scan might be cheaper.
PostgreSQL normally runs ANALYZE through autovacuum. You can inspect table activity and the last automatic analysis:
SELECT
relname,
n_live_tup,
n_dead_tup,
last_analyze,
last_autoanalyze
FROM pg_stat_user_tables
WHERE relname = 'orders';Bloat changes the physical cost
Bloat is wasted space caused by obsolete row versions, deleted index entries, or pages that no longer hold data efficiently. PostgreSQL uses multi-version concurrency control (MVCC), which keeps old row versions temporarily so concurrent transactions can work safely.
A normal VACUUM marks reusable space inside a table or index. It usually does not return the file to the operating system. That space can still be useful for future writes.
Index bloat can increase the number of pages that must be read. It can also make caching less effective. Heavy updates, deletes, and random inserts are common causes.
flowchart TD
A[INSERT, UPDATE, or DELETE] --> B[Dead row or index entries]
B --> C[VACUUM marks space reusable]
C --> D{Space reused efficiently?}
D -->|Yes| E[Continue normal maintenance]
D -->|No| F[Index or table bloat]
F --> G[Measure and consider REINDEX]caption: How changes create reusable space or bloat alt: A flowchart showing database changes creating dead entries, vacuum reusing space, and reindexing being considered when space is not reused efficiently.
Maintenance choices
Use the least disruptive operation that solves the problem.
| Operation | Main purpose | Typical impact |
|---|---|---|
| `ANALYZE` | Refresh planner statistics | Reads the table; short locking |
| `VACUUM` | Reclaim reusable row and index space | Background-friendly |
| `REINDEX` | Rebuild an index structure | Uses extra work and storage |
| `REINDEX CONCURRENTLY` | Rebuild with less blocking | Takes longer and has extra overhead |
REINDEX can help when an index is bloated or corrupted. In production, REINDEX CONCURRENTLY is often safer because ordinary reads and writes can continue, although it still consumes CPU, I/O, and temporary storage.
Do not rebuild every index on a schedule without evidence. First check query plans, table activity, index usage, and available maintenance windows.
Exercise
A table received a large data load, and EXPLAIN now estimates far fewer matching rows than the query returns. What should you try first?
Show answer
Run ANALYZE on the table, then check the plan again. Rebuilding the index is not the first response to stale planner statistics.