Skip to content

Instantly share code, notes, and snippets.

@doctaphred
Created April 3, 2021 17:02
Show Gist options
  • Save doctaphred/b6d3d2b80d8caad66049a10ade084d8f to your computer and use it in GitHub Desktop.
Save doctaphred/b6d3d2b80d8caad66049a10ade084d8f to your computer and use it in GitHub Desktop.
Postgres table sizes
SELECT table_schema,
TABLE_NAME,
pg_size_pretty(SIZE) AS SIZE,
pg_size_pretty(total_size) AS total_size
FROM
(SELECT table_schema,
TABLE_NAME,
pg_relation_size(quote_ident(table_schema) || '.' || quote_ident(TABLE_NAME)) AS SIZE,
pg_total_relation_size(quote_ident(table_schema) || '.' || quote_ident(TABLE_NAME)) AS total_size
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN ('information_schema',
'pg_catalog')
ORDER BY pg_relation_size(quote_ident(table_schema) || '.' || quote_ident(TABLE_NAME)) DESC, table_schema,
TABLE_NAME) x
ORDER BY x.SIZE DESC, x.total_size DESC,
table_schema,
TABLE_NAME;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment