How to monitor the size and growth of a PostgreSQL database
Knowing how much each database takes up and which tables grow the most lets you anticipate disk problems and detect bloated tables before they hurt.
- 1
Measure the size of each database in the cluster
pg_database_size returns bytes; pg_size_pretty formats them into a readable size. Sort to see which database weighs the most.
SELECT datname, pg_size_pretty(pg_database_size(datname)) AS tamano FROM pg_database ORDER BY pg_database_size(datname) DESC; - 2
Find the largest tables
pg_total_relation_size includes the table, its indexes, and the TOAST data. It is the real measure of what a table occupies on disk.
SELECT relname, pg_size_pretty(pg_total_relation_size(relid)) AS total FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10; - 3
Separate the weight of the table from its indexes
Sometimes the problem is not the data but oversized indexes. Compare pg_table_size with pg_indexes_size.
SELECT relname, pg_size_pretty(pg_table_size(relid)) AS tabla, pg_size_pretty(pg_indexes_size(relid)) AS indices FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10;
// common mistake
A table growing does not always mean more data: bloat from dead rows inflates it even if the record count does not rise. If the size goes up but the counts do not, check autovacuum before buying more disk.
// when it's worth an expert
When growth starts to threaten disk or performance, it helps to distinguish real data from bloat and useless indexes. At dba.mx we set up growth monitoring with alerts and a capacity plan so it does not catch you by surprise, at a fixed price.
Book an assessment — from USD $550This guide is for reference and uses example commands. In production, adapt to your version and test in a safe environment first.