Skip to content

Instantly share code, notes, and snippets.

@solidsnack
Last active April 6, 2022 03:22
Show Gist options
  • Save solidsnack/3f8755f59e69f972b08b90dce5012941 to your computer and use it in GitHub Desktop.
Save solidsnack/3f8755f59e69f972b08b90dce5012941 to your computer and use it in GitHub Desktop.
--- Formatted size for total usage, percentages for indexes, toast, data.
--- No system tables.
SELECT pg_class.oid :: regclass AS tab,
pg_size_pretty(total) AS disk_usage,
(100.0 * index / greatest(1, total)) :: numeric(6, 2) AS "%index",
(100.0 * toast / greatest(1, total)) :: numeric(6, 2) AS "%toast",
(100.0 * (total - index - toast) / greatest(1, total)) :: numeric(6, 2) AS "%data",
reltuples AS "~rows"
FROM pg_class LEFT JOIN pg_namespace ON pg_namespace.oid = relnamespace,
pg_total_relation_size(pg_class.oid) AS total,
pg_indexes_size(pg_class.oid) AS index,
COALESCE(pg_total_relation_size(reltoastrelid), 0) AS toast
WHERE relkind = 'r'
AND nspname NOT IN ('information_schema', 'pg_catalog');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment