Skip to content

How to Check PostgreSQL Index Usage and Find Unused Indexes

DodaTech 2 min read

In this tutorial, you'll learn about How to Check PostgreSQL Index Usage and Find Unused Indexes. We cover key concepts, practical examples, and best practices.

The Problem

Your PostgreSQL database has many indexes but you do not know which ones are actually being used by queries. Unused indexes waste disk space and slow down writes.

Quick Fix

Find Unused Indexes with pg_stat_user_indexes

psql -U postgres -d mydb -c "
SELECT schemaname, tablename, indexname, idx_scan
FROM pg_stat_user_indexes
ORDER BY idx_scan ASC, tablename;
"
# schemaname | tablename | indexname          | idx_scan
# public     | orders    | orders_unused_idx  |        0
# public     | orders    | orders_pkey        |    12345

Indexes with idx_scan = 0 have never been used for an index scan. These are candidates for removal, but investigate first — some indexes support unique constraints or are used for ordering.

Check Index Size and Bloat

psql -U postgres -d mydb -c "
SELECT
    indexrelid::regclass AS index_name,
    pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_stat_user_indexes
ORDER BY pg_relation_size(indexrelid) DESC LIMIT 10;
"
# index_name        | index_size
# orders_pkey       | 256 MB
# orders_unused_idx | 128 MB

Combine idx_scan = 0 with index size to prioritize which unused indexes to drop. A large unused index on a write-heavy table is the most impactful to remove.

Check Last Index Usage Time

psql -U postgres -d mydb -c "
SELECT
    schemaname,
    tablename,
    indexname,
    idx_scan,
    last_idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0;
"
# schemaname | tablename | indexname           | idx_scan | last_idx_scan
# public     | logs      | logs_old_idx        |        0 | 2025-01-01

The last_idx_scan column (available in PostgreSQL 14+) shows when the index was last scanned. A scan date far in the past confirms the index is unused.

Drop an Unused Index Safely

psql -U postgres -d mydb -c "
DROP INDEX CONCURRENTLY orders_unused_idx;
"
# DROP INDEX

Use DROP INDEX CONCURRENTLY to avoid locking the table for writes. The command waits for all conflicting transactions to finish before dropping the index.

Reset Index Usage Statistics for Accurate Measurement

psql -U postgres -d mydb -c "SELECT pg_stat_reset();"
# Run your application queries for a representative period
# Then check pg_stat_user_indexes again

Reset the statistics counters after deploying new indexes to measure their true usage from that point forward. This gives you accurate data about whether the new indexes are actually being used by queries.

Additional Troubleshooting

# Check the error message and stack trace for more context
echo "Review the full error output to identify the root cause"

If the above steps do not resolve the issue, examine the complete error message and stack trace. Often the key detail is in the middle of the traceback rather than the final line. Search for the error message in the project documentation or issue tracker for additional solutions.

Prevention

  • Review pg_stat_user_indexes monthly to identify unused indexes
  • Drop indexes with idx_scan = 0 on tables that have been through a full query cycle
  • Use pg_stat_all_indexes alongside query logging (pg_stat_statements) to correlate index usage with actual queries
  • Add indexes only after analyzing query patterns, not preemptively

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro