Skip to content

Instantly share code, notes, and snippets.

@Jeetah
Created September 15, 2023 09:18
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 Jeetah/58c78442902a6b36641f9505874ad67c to your computer and use it in GitHub Desktop.
Save Jeetah/58c78442902a6b36641f9505874ad67c to your computer and use it in GitHub Desktop.
Analyze Postgres index usage
SELECT
relname, 100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used,
n_live_tup rows_in_table
FROM
pg_stat_user_tables
WHERE
seq_scan + idx_scan \> 0
ORDER BY
n_live_tup DESC;
SELECT
sum(idx_blks_read) as idx_read,
sum(idx_blks_hit) as idx_hit,
(sum(idx_blks_hit) - sum(idx_blks_read)) / sum(idx_blks_hit) as ratio
FROM
pg_statio_user_indexes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment