Skip to content

Instantly share code, notes, and snippets.

@zacharyarnaise
Created November 8, 2023 15:00
Show Gist options
  • Save zacharyarnaise/c7c4dc4891952bf35108e0be24b3b23e to your computer and use it in GitHub Desktop.
Save zacharyarnaise/c7c4dc4891952bf35108e0be24b3b23e to your computer and use it in GitHub Desktop.
PostgreSQL: index stats
SELECT
psui.relname AS table_name,
pg_size_pretty(pg_relation_size(psui.relid)) AS table_size,
psui.indexrelname AS index_name,
psui.idx_scan AS index_scans_count,
pg_size_pretty(pg_relation_size(psui.indexrelid)) AS index_size,
psut.idx_scan AS table_idx_scans_count,
psut.seq_scan AS table_seq_scans_count,
psut.seq_scan + psut.idx_scan AS table_scans_total
FROM pg_stat_user_indexes AS psui
JOIN pg_stat_user_tables AS psut
ON psui.relid = psut.relid
ORDER BY psui.idx_scan DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment