Table and index sizes

Top tables by total size including indexes and TOAST

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
tags
postgresdiskperformancemonitoring

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); candidates to drop and reclaim 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 dead tuples and stale autovacuum runs

  • REINDEX INDEX CONCURRENTLY <name>; -- note: cannot run inside a transaction block

    Rebuild a bloated index online; note: must run outside a transaction

  • CREATE INDEX CONCURRENTLY idx_<name> ON <table>(col); -- monitor: SELECT * FROM pg_stat_progress_create_index;

    Build index without blocking writes; track progress via pg_stat_progress_create_index

  • pg_dump -Fc -j 4 -d <name> -f db.dump && pg_restore -j 4 -d <name> db.dump

    Custom-format parallel dump/restore; note: -j needs directory or custom format, not plain SQL

all 29 postgres commands