Skip to content

Instantly share code, notes, and snippets.

@checkaayush
Created May 25, 2022 06:58
Show Gist options
  • Save checkaayush/ddb83b7d209699407e19deaec816d716 to your computer and use it in GitHub Desktop.
Save checkaayush/ddb83b7d209699407e19deaec816d716 to your computer and use it in GitHub Desktop.
Get Index size/usage statistics
-- Source: https://wiki.postgresql.org/wiki/Index_Maintenance
SELECT
t.schemaname,
t.tablename,
c.reltuples::bigint AS num_rows,
pg_size_pretty(pg_relation_size(c.oid)) AS table_size,
psai.indexrelname AS index_name,
pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size,
CASE WHEN i.indisunique THEN 'Y' ELSE 'N' END AS "unique",
psai.idx_scan AS number_of_scans,
psai.idx_tup_read AS tuples_read,
psai.idx_tup_fetch AS tuples_fetched
FROM
pg_tables t
LEFT JOIN pg_class c ON t.tablename = c.relname
LEFT JOIN pg_index i ON c.oid = i.indrelid
LEFT JOIN pg_stat_all_indexes psai ON i.indexrelid = psai.indexrelid
WHERE
t.schemaname NOT IN ('pg_catalog', 'information_schema')
ORDER BY 1, 2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment