SELECT
name,
tuples,
pg_size_pretty(table_size) AS table_size,
pg_size_pretty(index_size) AS index_size,
pg_size_pretty(toast_size) AS toast_size,
pg_size_pretty(total_size) AS total_size
FROM (
SELECT
c.relname AS name,
(c.reltuples)::BIGINT AS tuples,
pg_relation_size((c.oid)::regclass) AS table_size,
((pg_total_relation_size((c.oid)::regclass) - pg_relation_size((c.oid)::regclass)) -
CASE
WHEN (c.reltoastrelid <> (0)::oid) THEN pg_relation_size((c.reltoastrelid)::regclass)
ELSE (0)::BIGINT
END) AS index_size,
CASE
WHEN (c.reltoastrelid <> (0)::oid) THEN pg_relation_size((c.reltoastrelid)::regclass)
ELSE (0)::BIGINT
END AS toast_size,
pg_total_relation_size((c.oid)::regclass) AS total_size
FROM
((pg_class c JOIN pg_roles r ON ((r.oid = c.relowner)))
LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace)))
WHERE
(((c.relkind = 'r'::"char") AND (n.nspname <> ALL (ARRAY['pg_catalog'::name, 'pg_toast'::name]))) AND pg_table_is_visible(c.oid))
ORDER BY pg_total_relation_size((c.oid)::regclass) DESC
) AS sub;