dataiotech Labs
Scripts Library/postgresql/#14
Script #14
postgresql
Storageread-onlyMin Version: 9.x

PostgreSQL Database Sizes and Temp File Usage

Shows each database's size with rollback, deadlock and temp file stats. Used for growth tracking and a quick health summary.

#database-size#capacity#pg_database
postgresql · sql
SELECT d.datname,
       pg_size_pretty(pg_database_size(d.datname)) AS size,
       s.xact_commit, s.xact_rollback, s.deadlocks,
       s.temp_files, pg_size_pretty(s.temp_bytes) AS temp_size
FROM pg_database d
JOIN pg_stat_database s ON s.datname = d.datname
WHERE NOT d.datistemplate
ORDER BY pg_database_size(d.datname) DESC;
Operational Notes & Remediation Guidance

Verify in a staging environment prior to running in production. This is a read-only diagnostic query; it does not acquire exclusive locks.

Production Safety & Execution Standards
  • This script is designed for read-only diagnostics; it does not alter schema or modify data directly.
  • Prior to executing on production systems under high load, verify the target database version (Min. Version: 9.x).
  • Exercise caution with mutating actions (e.g., terminating sessions or dropping indexes) referenced in the notes section.

Engineering Triad: Related Runbooks & Incident Response

Which production incidents and emergency drills execute this script as a diagnosis step?

Runbook Library (63)
Related Alerts & Metrics
M12PostgreSQL Data and WAL Disk Free SpacePostgreSQL