Skip to content

Instantly share code, notes, and snippets.

@dkam
Last active November 27, 2023 02:07
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 dkam/37d7a41d3c4ab00278580531832130bd to your computer and use it in GitHub Desktop.
Save dkam/37d7a41d3c4ab00278580531832130bd to your computer and use it in GitHub Desktop.
Postgres table, index & toast size.
# This ( https://gist.github.com/hatarist/675dc3debf6cf5f825b5c15aed4cbac0 ) with TOAST size formatting
SELECT
table_name,
pg_size_pretty(table_bytes) AS table,
pg_size_pretty(index_bytes) AS index,
pg_size_pretty(toast_bytes) AS toast,
pg_size_pretty(total_bytes) AS total
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
WHERE table_schema = 'public'
ORDER BY total_bytes DESC;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment