Created
January 28, 2023 00:11
-
-
Save mihow/02ee1eca01ee2cfb524c928fde7caed0 to your computer and use it in GitHub Desktop.
PostgreSQL - Get size of all tables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ORDER BY table_bytes; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment