Show index usage

Which indexes are actually being used and how often

psql -U myuser -d mydb -c "SELECT relname, indexrelname, idx_scan, idx_tup_read FROM pg_stat_user_indexes ORDER BY idx_scan DESC LIMIT 20;"

tool
postgres29 commands
tags
postgresmonitoringperformance

More postgres commands

  • psql -U myuser -d mydb -c "SELECT schemaname, tablename, indexname, idx_scan FROM pg_stat_user_indexes WHERE idx_scan = 0 AND NOT indisprimary ORDER BY pg_relation_size(indexrelid) DESC;"

    Indexes that have never been scanned — safe to drop candidates

  • pg_dump -U myuser -d mydb -F c -f mydb.dump

    Custom-format dump (fastest, supports parallel restore)

  • pg_restore -U myuser -d mydb -j 4 --clean mydb.dump

    Parallel restore with 4 workers, drop existing objects first

  • psql -U myuser -d mydb -c 'VACUUM (VERBOSE, ANALYZE) orders;'

    Reclaim dead tuples and update statistics for query planner

  • psql -U postgres -c "SELECT client_addr, state, sent_lsn, replay_lsn, (sent_lsn - replay_lsn) AS lag_bytes FROM pg_stat_replication;"

    Replication state and byte lag per replica

all 29 postgres commands