Skip to content

Commit db8fab1

Browse files
authored
Merge pull request #44 from percona/improved-autovacuum-candiate
Improved query for getting candidate tables for autovacuum
2 parents a59c464 + f2c7e90 commit db8fab1

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
WITH vbt AS (SELECT setting AS autovacuum_vacuum_threshold FROM
2+
pg_settings WHERE name = 'autovacuum_vacuum_threshold')
3+
, vsf AS (SELECT setting AS autovacuum_vacuum_scale_factor FROM
4+
pg_settings WHERE name = 'autovacuum_vacuum_scale_factor')
5+
, fma AS (SELECT setting AS autovacuum_freeze_max_age FROM
6+
pg_settings WHERE name = 'autovacuum_freeze_max_age')
7+
, sto AS (select opt_oid, split_part(setting, '=', 1) as param,
8+
split_part(setting, '=', 2) as value from (select oid opt_oid,
9+
unnest(reloptions) setting from pg_class) opt)
10+
SELECT
11+
'"'||ns.nspname||'"."'||c.relname||'"' as relation
12+
, pg_size_pretty(pg_table_size(c.oid)) as table_size
13+
, age(relfrozenxid) as xid_age
14+
, coalesce(cfma.value::float, autovacuum_freeze_max_age::float)
15+
autovacuum_freeze_max_age
16+
, (coalesce(cvbt.value::float, autovacuum_vacuum_threshold::float)
17+
+ coalesce(cvsf.value::float,autovacuum_vacuum_scale_factor::float) *
18+
pg_table_size(c.oid)) as autovacuum_vacuum_tuples
19+
, n_dead_tup as dead_tuples
20+
FROM pg_class c join pg_namespace ns on ns.oid = c.relnamespace
21+
join pg_stat_all_tables stat on stat.relid = c.oid
22+
join vbt on (1=1) join vsf on (1=1) join fma on (1=1)
23+
left join sto cvbt on cvbt.param = 'autovacuum_vacuum_threshold' and
24+
c.oid = cvbt.opt_oid
25+
left join sto cvsf on cvsf.param = 'autovacuum_vacuum_scale_factor' and
26+
c.oid = cvsf.opt_oid
27+
left join sto cfma on cfma.param = 'autovacuum_freeze_max_age' and
28+
c.oid = cfma.opt_oid
29+
WHERE c.relkind = 'r' and nspname <> 'pg_catalog'
30+
and (
31+
age(relfrozenxid) >= coalesce(cfma.value::float,
32+
autovacuum_freeze_max_age::float)
33+
or
34+
coalesce(cvbt.value::float, autovacuum_vacuum_threshold::float) +
35+
coalesce(cvsf.value::float,autovacuum_vacuum_scale_factor::float) *
36+
pg_table_size(c.oid) <= n_dead_tup
37+
-- or 1 = 1
38+
)
39+
ORDER BY age(relfrozenxid) DESC LIMIT 50;
40+

0 commit comments

Comments
 (0)