Skip to content

Instantly share code, notes, and snippets.

@shadyrudy
Created February 27, 2024 21:03
Show Gist options
  • Save shadyrudy/1b9e7620bb06393639245229ab8b9a29 to your computer and use it in GitHub Desktop.
Save shadyrudy/1b9e7620bb06393639245229ab8b9a29 to your computer and use it in GitHub Desktop.
PostgreSQL - Tables modified since last vacuum.
SELECT n.nspname AS schema_name
,c.relname AS table_or_index_name
,c.relkind AS table_or_index
,c.reltuples AS row_count
,s.last_vacuum
,s.last_autovacuum
,s.last_analyze
,s.last_autoanalyze
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_stat_user_tables s ON s.relid = c.oid
WHERE n.nspname not in ('pg_catalog', 'information_schema')
AND
(
c.relkind = 'r'
OR c.relkind = 'i'
)
AND (
s.last_vacuum < s.last_autovacuum
OR s.last_vacuum < s.last_analyze
)
order by n.nspname, c.relname;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment