Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Created October 15, 2023 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theY4Kman/52b3abd7cc44ae2d55f9db19f5f9e9ec to your computer and use it in GitHub Desktop.
Save theY4Kman/52b3abd7cc44ae2d55f9db19f5f9e9ec to your computer and use it in GitHub Desktop.
Show sizes of all tables in the public schema, itemized for tablespace alone, indexes size, and TOAST size
-- Sizes of all tables in the public schema
SELECT
table_name AS "Table",
pg_size_pretty(table_size) AS "Table Size",
pg_size_pretty(indexes_size) AS "Indexes Size",
pg_size_pretty(toast_size) AS "Toast Size",
pg_size_pretty(total_size) AS "Total Size"
FROM (
SELECT
table_name,
pg_relation_size(table_identifier) AS table_size,
pg_indexes_size(table_identifier) AS indexes_size,
pg_table_size(table_identifier) - pg_relation_size(table_identifier) AS toast_size,
pg_total_relation_size(table_identifier) AS total_size
FROM (
SELECT
table_name::text AS table_name,
('"' || table_schema || '"."' || table_name || '"') AS table_identifier
FROM information_schema.tables
WHERE table_schema = 'public'
) AS all_tables
ORDER BY total_size DESC
) AS pretty_sizes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment