Full answer
Evidence to gather:
- Usage counts: PostgreSQL's
pg_stat_user_indexes.idx_scanshows indexes that are never used since the last stats reset (check replicas too, since reports may run there). - Duplicates: an index on
(a)is redundant when(a, b)exists, for most queries. - Size: large indexes compete for cache with the table itself.
Decisions:
- Drop unused and redundant indexes, ideally with
DROP INDEX CONCURRENTLY. - Replace several single-column indexes with one composite index matching the main filter and sort.
- Use partial indexes for queries on a small subset (
WHERE status = 'pending'). - Keep indexes that enforce uniqueness, even if rarely scanned.
Measure insert time before and after on a copy of production data. Bulk loads can drop and recreate secondary indexes around the load.
Key points
- Each index costs writes and memory
- Usage statistics show unused indexes
- Composite indexes can replace several singles
- Partial indexes target subsets
- Keep uniqueness indexes
Common mistakes
- Indexing every column 'just in case'
- Dropping a unique index because it shows few scans
Follow-ups
- Why might an index be used only on a replica?
- How do partial indexes save space?
Tips
- Ground the choice in usage data