Skip to content

Instantly share code, notes, and snippets.

@clarkbw
Last active December 8, 2021 17:46
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 clarkbw/dfa0c177d6b635a8fe3cfb411252f1f3 to your computer and use it in GitHub Desktop.
Save clarkbw/dfa0c177d6b635a8fe3cfb411252f1f3 to your computer and use it in GitHub Desktop.
SELECT s.schemaname,
s.relname AS tablename,
s.indexrelname AS indexname,
pg_size_pretty(pg_relation_size(s.indexrelid)) AS index_size
FROM pg_catalog.pg_stat_user_indexes s
JOIN pg_catalog.pg_index i ON s.indexrelid = i.indexrelid
WHERE s.idx_scan = 0 -- has never been scanned
AND 0 <>ALL (i.indkey) -- no index column is an expression
AND NOT i.indisunique -- is not a UNIQUE index
AND NOT EXISTS -- does not enforce a constraint
(SELECT 1 FROM pg_catalog.pg_constraint c
WHERE c.conindid = s.indexrelid)
AND s.schemaname NOT LIKE '_timescaledb_%' -- ignores hypertables and caggs
ORDER BY pg_relation_size(s.indexrelid) DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment