Skip to content

Instantly share code, notes, and snippets.

@mkaranasou
Created March 10, 2020 10:32
Show Gist options
  • Save mkaranasou/74178f141e55fdf0fdcf77df2bee9437 to your computer and use it in GitHub Desktop.
Save mkaranasou/74178f141e55fdf0fdcf77df2bee9437 to your computer and use it in GitHub Desktop.
Get the tables by size - Postgres
-- source: https://makandracards.com/makandra/52141-postgresql-how-to-show-table-sizes
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
AND C.relkind <> 'i'
AND nspname !~ '^pg_toast'
ORDER BY pg_total_relation_size(C.oid) DESC
LIMIT 5;
-- Example output:
-- relation | total_size
-- -----------------------------------
-- public.biggest_table | 13 GB
-- public.other_table | 6651 MB
-- public.next_table | 3021 MB
-- public.and_so_on | 1691 MB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment