Skip to content

Instantly share code, notes, and snippets.

@beratdogan
Created February 4, 2019 22: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 beratdogan/a138d5cafa4644bdfaea2d1e6e30cc12 to your computer and use it in GitHub Desktop.
Save beratdogan/a138d5cafa4644bdfaea2d1e6e30cc12 to your computer and use it in GitHub Desktop.
PostgreSQL DBA Queries
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
INNER 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