而不是查找是否正在清理表,而是关闭相关表的自动清理:
alter table table_name_pattern
set (
autovacuum_enabled = false,
toast.autovacuum_enabled = false
);
table 模式是一个 glob 模式,例如 tbl*。在查询结束时重新打开自动清理
alter table table_name_pattern
set (
autovacuum_enabled = true,
toast.autovacuum_enabled = true
);
根据评论进行编辑:
查询相关表是否正在被清理是不必要且无用的。如果已知一个或多个相关表正在被清理,应该怎么做?等待并继续重复查询,直到没有被清空?并且当没有然后开始长查询只是为了在一段时间后发现自动真空刚刚再次启动?无关紧要。为什么不直接关闭自动真空吸尘器并避免所有麻烦?
以艰难的方式来做这件事并没有道德上的优势,尤其是在艰难的方式会比简单的方式产生更糟糕的结果时。更简单的代码更易于使用和理解,但不一定更容易构建。很多时候情况正好相反,需要更多的智力努力或准备,而不是复杂的。
如果 autovacuum 设置在事务中被更改并且该事务被回滚,则设置将恢复到事务开始之前的状态
drop table if exists t;
create table t (id int);
begin;
alter table t
set (
autovacuum_enabled = false,
toast.autovacuum_enabled = false
);
\d+ t
Table "public.t"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+-----------+---------+--------------+-------------
id | integer | | plain | |
Has OIDs: no
Options: autovacuum_enabled=false
rollback;
\d+ t
Table "public.t"
Column | Type | Modifiers | Storage | Stats target | Description
--------+---------+-----------+---------+--------------+-------------
id | integer | | plain | |
Has OIDs: no
但是事务内部的设置不会在事务外部看到,所以我猜 autovacuum 仍然会运行。如果这是真的,那么设置必须在事务之外完成并由一个作业控制,无论长时间运行的查询会发生什么,都会将其返回。