Created
November 8, 2019 04:57
-
-
Save osapon/8abd82ac942a0ccdce7e02b0540b15db to your computer and use it in GitHub Desktop.
PostgreSQL DB Garbage raito
This file contains 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
SELECT pg_database_size('notestock') AS db_size -- Any DB Name | |
, ROUND(100 * SUM( | |
CASE (a.n_live_tup + a.n_dead_tup) WHEN 0 THEN 0 | |
ELSE c.relpages * (a.n_dead_tup / (a.n_live_tup + a.n_dead_tup)::numeric) | |
END | |
) / SUM(c.relpages), 2) AS garbage_raito | |
FROM pg_class AS c | |
JOIN pg_stat_all_tables AS a ON(c.oid = a.relid) | |
WHERE relpages > 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment