Skip to content

Instantly share code, notes, and snippets.

@izmailoff
Created February 24, 2020 06:41
Show Gist options
  • Save izmailoff/ee12d619131c79360389e896aed0e791 to your computer and use it in GitHub Desktop.
Save izmailoff/ee12d619131c79360389e896aed0e791 to your computer and use it in GitHub Desktop.
Postgres DB stats
-- analyze DB for more precise stats
VACUUM (FULL, ANALYZE);
-- get row counts for all tables (option 1)
SELECT
nspname AS schemaname,relname,reltuples
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE
nspname NOT IN ('pg_catalog', 'information_schema') AND
relkind='r'
ORDER BY reltuples DESC;
-- get row counts for all tables (option 2)
SELECT schemaname,relname,n_live_tup
FROM pg_stat_user_tables
ORDER BY n_live_tup DESC;
-- get table sizes
SELECT nspname || '.' || relname AS "relation",
pg_size_pretty(pg_relation_size(C.oid)) AS "size"
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_relation_size(C.oid) DESC
LIMIT 20;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment