Skip to content

Instantly share code, notes, and snippets.

@matt-schwartz
Created July 27, 2016 20:34
Show Gist options
  • Save matt-schwartz/9ac77288b1f7458405cee8016ee5f8b5 to your computer and use it in GitHub Desktop.
Save matt-schwartz/9ac77288b1f7458405cee8016ee5f8b5 to your computer and use it in GitHub Desktop.
PostgreSQL table sizes
SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS INDEX
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS TABLE
FROM (
SELECT *, 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
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY total_bytes desc;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment