Skip to content

Indexes never used

Non-unique indexes with zero scans since the last statistics reset, with the space they take. Each one is write cost with no read benefit.

PostgreSQL · SQL
SELECT s.schemaname || '.' || s.relname AS table_name,       s.indexrelname AS index_name,       pg_size_pretty(pg_relation_size(s.indexrelid)) AS size,       pg_relation_size(s.indexrelid) AS size_bytes,       s.idx_scan AS scansFROM pg_stat_user_indexes sJOIN pg_index i ON i.indexrelid = s.indexrelidWHERE s.idx_scan = 0  AND NOT i.indisunique  AND NOT i.indisprimaryORDER BY pg_relation_size(s.indexrelid) DESCLIMIT 25

This query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.

What this query shows

Every index speeds up some reads and slows down every write, because each INSERT, UPDATE and DELETE has to maintain it. An index that no query uses is pure cost: disk, memory in the cache and slower writes.

The query reads pg_stat_user_indexes and lists non-unique, non-primary-key indexes whose idx_scan is 0, biggest first, 25 rows. Unique and primary key indexes are left out on purpose, because they enforce a constraint even when no query reads them.

How to read each column

  • table_name and index_name: where the index lives.
  • size: the index size in readable form. size_bytes is the same number for sorting and charts.
  • scans: idx_scan, the number of times the planner used this index since statistics were last reset. This query only returns rows where it is 0.

Start from the top. The largest index with no scans is the biggest saving and the biggest write you stop paying for.

Healthy and worrying numbers

An empty result is the ideal. A list of small indexes is not urgent. A few indexes of several gigabytes with zero scans are worth dealing with this week.

Always check how old the counter is before you trust a zero. A server that had its statistics reset last week has not had time to show its monthly or quarterly reports. As a rule of thumb, only act on indexes that have been at zero for at least one full business cycle.

SELECT stats_reset FROM pg_stat_database WHERE datname = current_database();

What to do about it

Before dropping, check three things: the index is not backing a foreign key you rely on for fast deletes on the parent table, it is not used only by a rare job, and no other server (a replica) reads it.

Save the definition first so you can recreate the index.

SELECT indexdef FROM pg_indexes WHERE indexname = 'idx_orders_created_at';

Then drop it without blocking writes.

DROP INDEX CONCURRENTLY public.idx_orders_created_at;

DROP INDEX CONCURRENTLY waits for running transactions to finish and cannot run inside a transaction block.

Limits of this query

  • idx_scan is counted per server. On a streaming replica the counters are separate, so an index unused on the primary can be busy on a replica that serves reports. Run the query on every server that takes reads.
  • Unique indexes and constraints are excluded, but an unused unique index can still be redundant. Look at those by hand.
  • Indexes that exist only to support a foreign key may show no scans and still protect deletes on the referenced table.
  • Only the connected database is covered.

Run it on a schedule in DataRunner

Connect the database to DataRunner, paste this query or start from the template, approve the SQL, then pick a schedule and a channel. Monthly is a good rhythm, since the point is to catch indexes that stay unused over a long period. Each run executes the query on your database and delivers the result as CSV, Excel, PDF or inline HTML by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.

Every run is kept in the run history, so you can set today's result next to last week's. A schedule carries its own time zone, so a Monday at 8:00 is your Monday.

If the database sits in a private network, the bridge agent opens the connection from the inside out, so you do not need to open an inbound port.

Frequently asked questions

How do I find unused indexes in PostgreSQL?
Query pg_stat_user_indexes for rows where idx_scan is 0, and exclude unique and primary key indexes, as this template does. Sort by pg_relation_size to start with the largest. Check the statistics reset date first so a zero means something.
Is it safe to drop an unused index?
Usually, if the counter covers a full business cycle on every server that reads the table, and the index is not unique or backing a constraint. Use DROP INDEX CONCURRENTLY so writes are not blocked, and keep the CREATE INDEX statement so you can undo it.
Why does an unused index hurt performance?
Every write to the table also updates each of its indexes, so unused ones add work to INSERT, UPDATE and DELETE. They also take disk, backup size and room in shared buffers that hot data could use.
When do the index statistics reset?
When someone calls pg_stat_reset, after a crash and after some upgrades. A clean restart keeps them in current versions. Check stats_reset in pg_stat_database before acting on a zero.