Created
June 9, 2017 08:26
-
-
Save hfs/db65ff72062e388e93f64aa3e76e422a to your computer and use it in GitHub Desktop.
PostgreSQL: List databases with no current activity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Show databases with no current connections | |
-- | |
-- BIG FAT WARNING: Don't trust this blindly! Just because there's no *current* | |
-- activity does not mean a database is no longer needed. | |
-- | |
SELECT | |
(pg_stat_file ('base/' || d.oid || '/PG_VERSION')).modification, | |
d.datname, | |
pg_size_pretty(pg_database_size(d.datname)) AS size | |
FROM pg_database d | |
LEFT JOIN pg_stat_activity a ON d.oid = a.datid | |
WHERE | |
a.query IS NULL AND | |
d.datname NOT LIKE 'template%' | |
ORDER BY datname; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment