Part 17 / Topic 7 of 8
The cost of indexes
Every index you add is a promise the engine must keep on every write: an INSERT updates the heap plus every index; our big_users now carries six. The bill has three lines:
- Write amplification — each index is extra page writes (and extra WAL) per change.
- Storage — indexes routinely rival their table:
SELECT pg_size_pretty(pg_relation_size('big_users')) AS table_size,
pg_size_pretty(pg_indexes_size('big_users')) AS all_indexes;
table_size | all_indexes
------------+-------------
6672 kB | 13 MB
(1 row)
- Dead weight — indexes nobody queries still pay both costs. Postgres counts every use; zero-scan indexes are candidates for deletion:
SELECT indexrelname, idx_scan
FROM pg_stat_user_indexes
WHERE relname = 'big_users'
ORDER BY idx_scan, indexrelname;
indexrelname | idx_scan
-------------------------+----------
idx_users_email | 0
idx_users_email_lower | 0
idx_users_joined_brin | 0
idx_users_pending | 0
idx_users_status_cover | 0
idx_users_status_joined | 0
(6 rows)
(All zeros — fittingly: every query in this lesson was EXPLAIN-only, planned but never executed. In production you read this view after weeks of real traffic, then investigate the zeros — checking first that they aren't enforcing uniqueness or backing a foreign key.) The discipline: indexes are designed from query shapes, Part 18's method — never sprinkled on columns "just in case".
🧪 Quick quiz — answer from memory, then check.
- What does every additional index cost on INSERT?
- How do you find unused indexes?
Show the answers
- An extra index-structure update (pages + WAL) — writes slow as indexes accumulate.
pg_stat_user_indexes.idx_scanover a representative period; investigate the zeros.
Done here? The practice set for Part 17 — 5 exercises with solutions — is on the part page.