Database Administration — Fri Apr 17

← Home | ← database-admin

PostgreSQL - DBA Toolkit - Database Size and Growth

Fri Apr 17 — See database and table sizes at a glance, identify the biggest objects, and spot growth before it fills the disk.
#postgresql #dba #monitoring #storage #growth #tool-kit

Disk Space Does Not Give You a Warning

By the time PostgreSQL reports a disk full error, writes have stopped.

Monitoring size and growth is one of the most straightforward DBA habits — and one of the most commonly skipped. Tables grow. Indexes grow. Dead tuple bloat accumulates. WAL archives fill volumes that nobody is watching.

This script gives you a full picture: total database size, the largest tables and indexes, and the ratio of live data to dead bloat so you can see where vacuum is falling behind.

DatabaseSizeandGrowth.sql
    Loading…
  

Reading the Results

Database Summary

ColumnWhat It Tells You
datnameDatabase name
pg_size_prettyTotal database size in human-readable format
pg_database_sizeRaw size in bytes — useful for trend tracking
numbackendsCurrent active connections to this database

Top Tables by Size

ColumnWhat It Tells You
schemaname / tablenameSchema and table
total_sizeTable + all associated indexes and toast
table_sizeTable data only (excluding indexes)
index_sizeTotal index size for the table
live_tuplesEstimated live (visible) rows
dead_tuplesEstimated dead rows not yet vacuumed
bloat_ratioDead tuples as a percentage of total — above 20% warrants a look

Top Indexes by Size

ColumnWhat It Tells You
indexnameIndex name
index_sizeDisk space used by this index
idx_scanNumber of times this index has been used

What to Watch For

SignalWhat It Means
Total size growing faster than expectedCheck for missing vacuum, log tables with no retention, or batch job side effects
High dead_tuples on a busy tableAutovacuum is not keeping up — tune autovacuum_vacuum_scale_factor for this table
Large indexes on rarely scanned columnsUnused indexes still cost writes and storage — consider dropping
idx_scan = 0 on a large indexIndex was never used since the last stats reset — candidate for removal
index_size > table_sizeOver-indexed table — review which indexes are actually needed
Many small databases each with significant sizeConsolidation or archival opportunity

Checking Size Over Time

To track growth trends, log pg_database_size() results into a monitoring table on a schedule:

CheckingSizeOverTime.sql
    Loading…
  

Compare snapshots to project when you will hit capacity.

Gareth Winterman