Skip to content

Instantly share code, notes, and snippets.

@meniam
Created September 24, 2016 12:39
Show Gist options
  • Save meniam/99ec16cfbd0b19a20e541050d9264400 to your computer and use it in GitHub Desktop.
Save meniam/99ec16cfbd0b19a20e541050d9264400 to your computer and use it in GitHub Desktop.
CREATE OR REPLACE VIEW public.unused_indexes AS
SELECT
(pg_stat_all_indexes.schemaname :: TEXT || '.' :: TEXT) || pg_stat_all_indexes.relname :: TEXT,
pg_stat_all_indexes.indexrelname,
pg_stat_all_indexes.idx_scan,
pg_size_pretty(
pg_relation_size(((pg_stat_all_indexes.schemaname :: TEXT || '.' :: TEXT) || pg_stat_all_indexes.indexrelname :: TEXT) :: REGCLASS)) AS pg_size_pretty
FROM pg_stat_all_indexes
WHERE (pg_stat_all_indexes.schemaname <> ALL (ARRAY ['pg_toast' :: NAME, 'pg_catalog' :: NAME])) AND pg_stat_all_indexes.idx_scan = 0 AND
pg_relation_size(((pg_stat_all_indexes.schemaname :: TEXT || '.' :: TEXT) || pg_stat_all_indexes.indexrelname :: TEXT) :: REGCLASS) >
(1024 * 1024 * 1024)
ORDER BY (pg_relation_size(((pg_stat_all_indexes.schemaname :: TEXT || '.' :: TEXT) || pg_stat_all_indexes.indexrelname :: TEXT) :: REGCLASS)) DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment