Find unused indexes in PostgreSQL
Every index slows down every write to its table. This finds the ones PostgreSQL has not scanned once, sorted by the space they take.
The task
Every INSERT, and every UPDATE that cannot be done as a HOT update, has to maintain every index on the table, and each index takes disk and shared_buffers. pg_stat_user_indexes counts how many times each index has been scanned. An index with idx_scan = 0 across a long, representative window is overhead you can remove, unless it enforces a constraint or serves a job that runs once a quarter.
The snippet
Written for PostgreSQL.
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 size,
s.idx_tup_read, s.idx_tup_fetch
FROM pg_stat_user_indexes s
JOIN pg_index i ON i.indexrelid = s.indexrelid
WHERE s.idx_scan = 0
AND NOT i.indisunique
AND NOT EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = s.indexrelid)
ORDER BY pg_relation_size(s.indexrelid) DESC;
How it works
pg_stat_user_indexes has one row per index on a user table, and idx_scan counts the scans that used it since the last statistics reset. Joining pg_index drops unique indexes (primary keys are unique too), and the pg_constraint check drops indexes that back an exclusion constraint. Those enforce rules, so a zero scan count does not make them removable. Sorting by pg_relation_size puts the largest at the top. The counters reset after crash recovery or a pg_stat_reset call, so the result means something only with weeks of real traffic behind it.
Variations
Drop statements
SELECT format('DROP INDEX CONCURRENTLY %I.%I;', s.schemaname, s.indexrelname) AS drop_statement
FROM pg_stat_user_indexes s
JOIN pg_index i ON i.indexrelid = s.indexrelid
WHERE s.idx_scan = 0 AND NOT i.indisunique
AND NOT EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = s.indexrelid)
AND pg_relation_size(s.indexrelid) > 100 * 1024 * 1024
ORDER BY pg_relation_size(s.indexrelid) DESC;
Questions that come up
How long should I wait before trusting idx_scan = 0?
At least one full business cycle, which usually means several weeks including a month-end. Check stats_reset in pg_stat_database for your database to see when counting started. The counters are also per server: an index used only by queries on a read replica shows idx_scan = 0 on the primary, so run the query on every node before dropping anything. On PostgreSQL 16 and later, last_idx_scan shows when each index was last used.
Is DROP INDEX CONCURRENTLY safe?
It is the safer form. A plain DROP INDEX takes an ACCESS EXCLUSIVE lock on the table, which blocks reads and writes while it waits for running queries to finish. CONCURRENTLY waits for conflicting transactions without blocking new ones. It cannot run inside a transaction block, so run each generated statement on its own.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.