Show table and index sizes
Top 20 tables by total size with indexes and TOAST, next to the size of the table alone
SELECT relname, pg_size_pretty(pg_total_relation_size(oid)) AS total, pg_size_pretty(pg_relation_size(oid)) AS table FROM pg_class WHERE relkind='r' ORDER BY pg_total_relation_size(oid) DESC LIMIT 20;
- tool
- postgres29 commands
More postgres commands
-
SELECT relname, indexrelname, idx_scan, pg_size_pretty(pg_relation_size(indexrelid)) AS size FROM pg_stat_user_indexes WHERE idx_scan=0 ORDER BY pg_relation_size(indexrelid) DESC;Indexes never scanned (idx_scan=0), biggest first; candidates to drop to free space
-
SELECT relname, n_dead_tup, last_autovacuum, last_autoanalyze FROM pg_stat_user_tables ORDER BY n_dead_tup DESC LIMIT 20;Find tables with many dead tuples and old autovacuum runs
-
REINDEX INDEX CONCURRENTLY <name>; -- note: cannot run inside a transaction blockRebuild a bloated index without blocking writes; it cannot run inside a transaction
-
CREATE INDEX CONCURRENTLY idx_<name> ON <table>(col); -- monitor: SELECT * FROM pg_stat_progress_create_index;Build an index without blocking writes and track progress in pg_stat_progress_create_index
-
pg_dump -Fc -j 4 -d <name> -f db.dump && pg_restore -j 4 -d <name> db.dumpDump and restore in 4 parallel jobs; note: pg_restore -j works with custom format, but pg_dump -j needs directory format (-Fd)