Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active December 11, 2020 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eusonlito/cc510af9c671fe883a8b9f8f40433809 to your computer and use it in GitHub Desktop.
Save eusonlito/cc510af9c671fe883a8b9f8f40433809 to your computer and use it in GitHub Desktop.
List all postgresql tables with rows, contents size, index size and total size
SELECT N.nspname || '.' || C.relname AS "relation",
S.n_live_tup AS "rows",
pg_size_pretty(pg_relation_size(C.oid)) AS "data_size",
pg_size_pretty(pg_indexes_size(C.oid)) AS "index_size",
pg_size_pretty(pg_total_relation_size(C.oid)) AS "total_size"
FROM pg_class C
LEFT JOIN pg_namespace AS N ON (N.oid = C.relnamespace)
LEFT JOIN pg_stat_user_tables S ON (S.relname = C.relname)
WHERE (
N.nspname NOT IN ('pg_catalog', 'information_schema')
AND N.nspname !~ '^pg_toast'
AND C.relkind <> 'i'
)
ORDER BY pg_total_relation_size(C.oid) DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment