0Pricing
SQL Academy · Lesson

Index Maintenance and Bloat

Diagnose index bloat, rebuild with REINDEX CONCURRENTLY, and drop unused indexes safely.

Index Maintenance and Bloat is a free SQL Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Indexes Bloat

PostgreSQL uses MVCC: UPDATE writes a new row version, leaving the old visible to existing transactions. Index entries for both versions exist. Over time:

  • Heavy-update tables accumulate dead index entries
  • Indexes grow larger than necessary
  • Lookups slow as the B-tree gets deeper

Diagnosing Bloat

Bloat-checking queries are non-trivial. Common tools:

  • pgstattuple extension
  • pg_repack reports
  • Bloat queries from check_postgres / monitoring tools
CREATE EXTENSION pgstattuple;
SELECT * FROM pgstatindex('orders_user_id_idx');

REINDEX

Rebuilds the index. The classic form takes an ACCESS EXCLUSIVE lock — bad in production:

REINDEX INDEX orders_user_id_idx;       -- blocks writes!

REINDEX CONCURRENTLY (PG 12+)

Non-blocking variant — readers and writers continue during rebuild:

REINDEX INDEX CONCURRENTLY orders_user_id_idx;

Unused Indexes

An unused index slows every write but speeds zero reads. Find them:

SELECT schemaname, relname, indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid))
FROM pg_stat_user_indexes
WHERE idx_scan = 0
  AND indexrelname NOT LIKE '%_pkey'
ORDER BY pg_relation_size(indexrelid) DESC;

Drop Unused Indexes

Drop them — but verify across all environments and time windows first. Indexes used for occasional reports look unused most of the time.

DROP INDEX CONCURRENTLY old_unused_idx;

Duplicate Indexes

Sometimes the same index is created by both a constraint and a manual CREATE INDEX. Check pg_indexes for duplicates and drop the redundant one:

SELECT tablename, indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'public'
ORDER BY tablename, indexname;

Index Write Amplification

Every INSERT/UPDATE/DELETE updates every relevant index. Three indexes on a hot table = 3× write cost. Add only the indexes that pay off.

GIN Pending List

GIN indexes batch updates in a pending list. Flush manually or rely on autovacuum:

SELECT gin_clean_pending_list('events_data_gin');

VACUUM Cleans Index Entries

VACUUM (covered in MVCC lesson) removes dead index entries from heap pages. Without autovacuum, indexes grow indefinitely.

Monitoring Index Size

Track index size over time:

SELECT pg_size_pretty(pg_indexes_size('orders')) AS index_size,
       pg_size_pretty(pg_total_relation_size('orders')) AS total_size;

pg_repack: Rewrite Tables Online

For severe bloat, pg_repack rewrites the table and indexes online — no full table lock. Install as an OS package and a PostgreSQL extension.

Recap

Indexes need care.

  • Bloat from MVCC is normal — manage with VACUUM and REINDEX CONCURRENTLY
  • Drop unused indexes
  • Avoid duplicates
  • Each new index slows writes — be deliberate

Quick Check

Which PostgreSQL command rebuilds an index WITHOUT blocking writes?

Frequently asked questions

Is the “Index Maintenance and Bloat” lesson free?

Yes — the full text of “Index Maintenance and Bloat” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “Index Maintenance and Bloat”?

Diagnose index bloat, rebuild with REINDEX CONCURRENTLY, and drop unused indexes safely. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Index Maintenance and Bloat” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. B-tree vs Hash vs GiST vs GIN Indexes
  2. Composite Indexes and Column Order
  3. Partial and Expression Indexes
  4. Index Maintenance and Bloat
← Back to SQL Academy