Skip to content

Instantly share code, notes, and snippets.

@agarman
Created August 1, 2019 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agarman/b1d401eaca3931d28a1359274ef93f5d to your computer and use it in GitHub Desktop.
Save agarman/b1d401eaca3931d28a1359274ef93f5d to your computer and use it in GitHub Desktop.
PostgreSQL df
SELECT
table_name,
pg_size_pretty(table_size) AS table_size,
pg_size_pretty(indexes_size) AS indexes_size,
pg_size_pretty(total_size) AS total_size,
live_tuples,
dead_tuples,
(case
when ((live_tuples + dead_tuples) > 0)
then 100.0 * ((1.0 * dead_tuples) / (live_tuples + dead_tuples))
else 0.0
end) as percentage_dead
FROM (
SELECT
pg_table_size(table_name) AS table_size,
pg_indexes_size(table_name) AS indexes_size,
pg_total_relation_size(table_name) AS total_size,
table_stats.*
FROM (
SELECT
('"' || schemaname || '"."' || relname || '"') AS table_name
,n_live_tup AS live_tuples
,n_dead_tup AS dead_tuples
FROM pg_catalog.pg_stat_all_tables s
) AS table_stats
) AS pretty_sizes
ORDER BY percentage_dead desc, total_size desc
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment