Skip to content

Instantly share code, notes, and snippets.

@mdimai666
Last active August 1, 2018 03:46
Show Gist options
  • Save mdimai666/bef22628cf23cf4d1a15a95761ea27b7 to your computer and use it in GitHub Desktop.
Save mdimai666/bef22628cf23cf4d1a15a95761ea27b7 to your computer and use it in GitHub Desktop.
Postgres Return statistic(size, rows count) from all tables in current DataBase
/*
Return statistic from all tables in current DataBase
+------------------+----------+-------+--------------+
| tableName | rowCount | size | externalSize |
+------------------+----------+-------+--------------+
| mails | 101 | 64kb | 48kb |
+------------------+----------+-------+--------------+
| "logs" | 25 | 104kb | 56kb |
+------------------+----------+-------+--------------+
| "mail_templates" | 2 | 112kb | 96kb |
+------------------+----------+-------+--------------+
| "campaigns" | 2 | 64kb | 56kb |
+------------------+----------+-------+--------------+
*/
SELECT
pgClass.relname AS tableName,
CAST(pgClass.reltuples AS INT) AS rowsCount,
pg_size_pretty(pg_total_relation_size(relid)) As "size",
pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) as "externalSize"
FROM
pg_class pgClass
LEFT JOIN
pg_namespace pgNamespace ON (pgNamespace.oid = pgClass.relnamespace)
LEFT JOIN
pg_catalog.pg_statio_user_tables ON pg_catalog.pg_statio_user_tables.relname = pgClass.relname
WHERE
pgNamespace.nspname NOT IN ('pg_catalog', 'information_schema') AND
pgClass.relkind='r' AND
pgClass.reltuples > 1
ORDER BY pg_total_relation_size(relid) DESC, pgClass.reltuples DESC
LIMIT 35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment