Skip to content

Instantly share code, notes, and snippets.

@timotewb
Last active November 22, 2020 18:38
Show Gist options
  • Save timotewb/7b2298675942ce36051ba20081b3d124 to your computer and use it in GitHub Desktop.
Save timotewb/7b2298675942ce36051ba20081b3d124 to your computer and use it in GitHub Desktop.
SELECT
table_schema
,table_name
,pg_size_pretty(total_bytes) AS total_size
,pg_size_pretty(index_bytes) AS index_size
,pg_size_pretty(toast_bytes) AS toast_size
,pg_size_pretty(table_bytes) AS table_size
FROM (
SELECT
table_schema
,table_name
,total_bytes-index_bytes-COALESCE(toast_bytes,0) AS table_bytes
FROM (
SELECT
c.oid
,nspname AS table_schema
,relname AS table_name
,c.reltuples AS row_estimate
,pg_total_relation_size(c.oid) AS total_bytes
,pg_indexes_size(c.oid) AS index_bytes
,pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM
pg_class c
LEFT JOIN pg_namespace n
ON n.oid = c.relnamespace
WHERE relkind = 'r'
) a
) a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment