2025-01-10postgres
Table Size Query
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;
Schema 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;