2025-01-10
SELECT
table_schema,
table_name,
pg_size_pretty(table_size) AS table_size,
pg_size_pretty(indexes_size) AS indexes_size,
pg_size_pretty(total_size) AS total_size
FROM (
SELECT
table_schema,
table_name,
pg_table_size(fmt_table_name) AS table_size,
pg_indexes_size(fmt_table_name) AS indexes_size,
pg_total_relation_size(fmt_table_name) AS total_size
FROM (
SELECT
('"' || table_schema || '"."' || table_name || '"') AS fmt_table_name,
table_schema,
table_name
FROM information_schema.tables
) AS all_tables
) AS pretty_sizes;
SELECT
schema_name,
pg_size_pretty(SUM(total_size)) AS total_size
FROM (
/* Get sizes for each table in the database */
SELECT
table_schema AS schema_name,
pg_table_size(fmt_table_name) AS table_size,
pg_indexes_size(fmt_table_name) AS indexes_size,
pg_total_relation_size(fmt_table_name) AS total_size
FROM (
/* Build the fully-qualified table name */
SELECT
('"' || table_schema || '"."' || table_name || '"') AS fmt_table_name,
table_schema,
table_name
FROM information_schema.tables
/* Optionally exclude system schemas here (like 'pg_catalog', 'information_schema') */
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
) AS all_tables
) AS table_sizes
GROUP BY schema_name
ORDER BY SUM(total_size) DESC;
mitchell@gritts.me
Built by me. Writing and images are my original work unless otherwise noted. Please attribute this site when sharing.
This content served from this URL (mitchell.gritts.net) is licensed with the Unlicense. Content served from other URLs may have other licenses. The repo is currently private while I actively build out the site.