Skip to content

Instantly share code, notes, and snippets.

@jwg2s
Last active March 5, 2021 20:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jwg2s/f92178825f7df4899c551a9ac8dc2b12 to your computer and use it in GitHub Desktop.
Save jwg2s/f92178825f7df4899c551a9ac8dc2b12 to your computer and use it in GitHub Desktop.
# Find all tables and when they were last vacuumed/analyzed, either manually or automatically
SELECT relname,
last_vacuum,
last_autovacuum,
last_analyze,
last_autoanalyze
FROM pg_stat_all_tables
WHERE schemaname = 'public'
ORDER BY last_vacuum DESC;
# Find any running processes that are doing autovacuum and which tables they're working on
SELECT pid,
Age(query_start, Clock_timestamp()),
usename,
query
FROM pg_stat_activity
WHERE query != '<IDLE>'
AND query ilike '%vacuum%'
ORDER BY query_start ASC;
# Find table/index sizes for all tables in a schema
SELECT *,
Pg_size_pretty(total_bytes) AS total,
Pg_size_pretty(index_bytes) AS INDEX,
Pg_size_pretty(toast_bytes) AS toast,
Pg_size_pretty(table_bytes) AS TABLE
FROM (SELECT *,
total_bytes - index_bytes - Coalesce(toast_bytes, 0) AS
table_bytes
FROM (SELECT c.oid,
nspname AS table_schema,
relname AS TABLE_NAME,
c.reltuples AS row_estimate,
Pg_total_relation_size(c.oid) AS total_bytes,
Pg_indexes_size(c.oid) AS index_bytes,
Pg_total_relation_size(reltoastrelid) AS toast_bytes
FROM pg_class c
LEFT JOIN pg_namespace n
ON n.oid = c.relnamespace
WHERE relkind = 'r') a
WHERE table_schema = 'public'
ORDER BY total_bytes DESC) a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment