Use built-in size functions (pg_total_relation_size, pg_relation_size) to see how much disk space a table and its indexes consume.
Execute SELECT pg_size_pretty(pg_total_relation_size('schema.table_name'));
. The result is the total on-disk size, including data, indexes, and TOAST.
Call both functions:SELECT pg_size_pretty(pg_relation_size('schema.table_name')) AS data_size, pg_size_pretty(pg_indexes_size('schema.table_name')) AS index_size;
Yes—run:SELECT relname AS table, pg_size_pretty(pg_total_relation_size(oid)) AS total, pg_size_pretty(pg_relation_size(oid)) AS data, pg_size_pretty(pg_indexes_size(oid)) AS indexesFROM pg_classWHERE relkind = 'r'ORDER BY pg_total_relation_size(oid) DESC;
pg_relation_size()
= table heap only. pg_indexes_size()
= all indexes. pg_total_relation_size()
= heap + indexes + TOAST. All return bigint
bytes; wrap with pg_size_pretty()
for readability.
• Schedule the above query in cron or a monitoring tool.
• Alert when size jumps unexpectedly.
• Vacuum & analyze large tables regularly.
• Partition or archive when growth exceeds retention policies.
VACUUM FULL
rewrites the table, removing dead tuples and shrinking on-disk size. Measure again afterward to confirm reclaimed space.
SELECT table_schema || '.' || table_name AS table, pg_size_pretty(pg_total_relation_size(table_schema || '.' || table_name)) AS totalFROM information_schema.tablesWHERE table_type = 'BASE TABLE'ORDER BY pg_total_relation_size(table_schema || '.' || table_name) DESCLIMIT 10;
No. These size functions are read-only and acquire only lightweight locks, so they do not block writes.
Yes. Run SELECT pg_size_pretty(pg_database_size(current_database()));
to view the total size of the current database.
Usually, but high-churn tables may still accumulate bloat. Monitor size trends and schedule manual VACUUM FULL
or partitioning if growth is excessive.