Skip to content

Instantly share code, notes, and snippets.

@Sedward
Created October 22, 2013 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sedward/7104399 to your computer and use it in GitHub Desktop.
Save Sedward/7104399 to your computer and use it in GitHub Desktop.
Find unused indexes in Postgresql
select s.schemaname as sch, s.relname as rel, s.indexrelname as idx, s.idx_scan as scans, pg_size_pretty(pg_relation_size(s.relid)) as ts, pg_size_pretty(pg_relation_size(s.indexrelid)) as "is" from pg_stat_user_indexes s join pg_index i on i.indexrelid=s.indexrelid left join pg_constraint c on i.indrelid=c.conrelid and array_to_string(i.indkey, '') = array_to_string(c.conkey, '') where i.indisunique is false and pg_relation_size(s.relid) > 1000000 and s.idx_scan < 100000 and c.confrelid is null order by s.idx_scan asc, pg_relation_size(s.relid) desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment