Skip to content

Instantly share code, notes, and snippets.

@sumanthkumarc
Created July 12, 2024 09:57
Show Gist options
  • Save sumanthkumarc/8a45baba8534ed6825b1d93b557fa5d6 to your computer and use it in GitHub Desktop.
Save sumanthkumarc/8a45baba8534ed6825b1d93b557fa5d6 to your computer and use it in GitHub Desktop.
Query to get auto vacuum/auto analyze stats from postgres database
SELECT pg_stat_all_tables.relname AS table_name,
COALESCE(NULLIF(pg_class.reltuples, -1), 0)::bigint AS live_tuples,
pg_stat_all_tables.n_dead_tup AS dead_tuples,
pg_stat_all_tables.n_ins_since_vacuum AS rows_inserted_since_vacuum,
pg_stat_all_tables.n_mod_since_analyze AS rows_modified_since_analyze,
(
COALESCE(NULLIF(pg_class.reltuples, -1), 0) * (
SELECT setting
FROM pg_settings
WHERE name = 'autovacuum_vacuum_scale_factor'
LIMIT 1
)::real + (
SELECT setting
FROM pg_settings
WHERE name = 'autovacuum_vacuum_threshold'
LIMIT 1
)::int
) as table_autovacuum_vacuum_threshold,
(
COALESCE(NULLIF(pg_class.reltuples, -1), 0) * (
SELECT setting
FROM pg_settings
WHERE name = 'autovacuum_vacuum_insert_scale_factor'
LIMIT 1
)::real + (
SELECT setting
FROM pg_settings
WHERE name = 'autovacuum_vacuum_insert_threshold'
LIMIT 1
)::int
) as table_autovacuum_vacuum_insert_threshold,
(
COALESCE(NULLIF(pg_class.reltuples, -1), 0) * (
SELECT setting
FROM pg_settings
WHERE name = 'autovacuum_analyze_scale_factor'
LIMIT 1
)::real + (
SELECT setting
FROM pg_settings
WHERE name = 'autovacuum_analyze_threshold'
LIMIT 1
)::int
) as table_autovacuum_analyze_threshold,
pg_stat_all_tables.last_vacuum,
pg_stat_all_tables.last_autovacuum,
pg_stat_all_tables.last_analyze,
pg_stat_all_tables.last_autoanalyze
FROM pg_stat_all_tables
JOIN pg_class ON pg_class.relname = pg_stat_all_tables.relname
WHERE pg_stat_all_tables.schemaname NOT IN (
'information_schema',
'pg_catalog'
)
ORDER BY live_tuples DESC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment