Cookbook recipe

Finding unused indexes consuming space and write overhead

Applies to PostgreSQL 13–17 Last reviewed Nov 2025 Grounded in source
Estimated investigation4 min

Scenario

Every index must be updated on every INSERT, UPDATE, and DELETE — unused indexes impose pure write overhead and waste storage without benefiting any query. Diagnose it -- Indexes with zero scans since last stats reset,…

Investigation Path

Every index must be updated on every INSERT, UPDATE, and DELETE — unused indexes impose pure write overhead and waste storage without benefiting any query.

Diagnose it

-- Indexes with zero scans since last stats reset, excluding constraints:
SELECT s.schemaname,
       s.relname                                          AS table_name,
       s.indexrelname                                     AS index_name,
       s.idx_scan,
       pg_size_pretty(pg_relation_size(s.indexrelid))    AS index_size
FROM pg_stat_all_indexes s
WHERE s.schemaname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
  AND s.idx_scan = 0
  AND NOT EXISTS (
      SELECT 1 FROM pg_index i
      WHERE i.indexrelid = s.indexrelid
        AND (i.indisprimary OR i.indisunique OR i.indisexclusion)
  )
ORDER BY pg_relation_size(s.indexrelid) DESC
LIMIT 20;

Always confirm stats have been accumulating for a sufficient period — check
stats_reset in pg_stat_all_indexes or
pg_stat_database before trusting idx_scan = 0.

Why it happens

Unused indexes are created during development, for queries that were later removed, or as
duplicates that the planner passes over. They take storage (sometimes gigabytes for large
tables), add write latency, slow autovacuum (which must update every index), and increase
checkpoint write volume.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes