Skip to content

Instantly share code, notes, and snippets.

Created Nov 8, 2019
What would you like to do?
PostgreSQL DB Garbage raito
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)
) / 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