Last active
February 5, 2024 12:26
-
-
Save eusonlito/87790d4b1d6a8e2a5340ec73ca5e3c9c to your computer and use it in GitHub Desktop.
List PostgreSQL database tables by size and rows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
"s"."relname" AS "table_name", | |
pg_total_relation_size("s"."schemaname" || '.' || "s"."relname") / 1024 / 1024 AS "total_size", | |
pg_relation_size("s"."schemaname" || '.' || "s"."relname") / 1024 / 1024 AS "table_size", | |
(pg_total_relation_size("s"."schemaname" || '.' || "s"."relname") - pg_relation_size("s"."schemaname" || '.' || "s"."relname")) / 1024 / 1024 AS "index_size", | |
"s"."n_live_tup" AS "table_rows" | |
FROM | |
"pg_stat_user_tables" "s" | |
JOIN | |
"pg_class" "c" ON "s"."relid" = "c"."oid" | |
ORDER BY | |
pg_total_relation_size("s"."schemaname" || '.' || "s"."relname") DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment