下面的提交内容已经被整合进Postgres12
版本里了,添加了一个新的选项来使用VACUUM
和ANALYZE
commit: 803b1301e8c9aac478abeec62824a5d09664ffffauthor: Michael Paquier <michael@paquier.xyz>date: Thu, 4 Oct 2018 09:00:33 +0900Add option SKIP_LOCKED to VACUUM and ANALYZEWhen specified, this option allows VACUUM to skip the work on a relationif there is a conflicting lock on it when trying to open it at thebeginning of its processing.Similarly to autovacuum, this comes with a couple of limitations whilethe relation is processed which can cause the process to still block:- when opening the relation indexes.- when acquiring row samples for table inheritance trees, partition treesor certain types of foreign tables, and that a lock is taken on someleaves of such trees.Author: Nathan BossartReviewed-by: Michael Paquier, Andres Freund, Masahiko SawadaDiscussion: https://postgr.es/m/9EF7EBE4-720D-4CF1-9D0E-4403D7E92990@amazon.comDiscussion: https://postgr.es/m/20171201160907.27110.74730@wrigleys.postgresql.org
Postgres 11
已经扩展了VACUUM
,因此可以在单次查询中指定多张表,每次处理一张表。但是当VACUUM
卡在一张表上很长时间,由于表被锁或者别的原因时,应由触发VACUUM
的应用层仔细查看并解除阻塞情况。SKIP_LOCKED
通过立即跳过任何在VACUUM
或者ANALYZE
处理开始时无法锁定的表,实现了对这种场景的更多控制,这意味着这种处理方式将可能以不做任何事情为代价及时完成,如果表不断累积膨胀而不被清理,这也可能是危险的。正如commit
信息中所提到的,有一些类似于autovacuum
的限制:
可能需要锁定表索引,这将导致处理过程在处理它们时仍然会阻塞。
要处理的分区或继承树关系部分的列表是在开始
VACUUM
或者ANALYZE
时生成的,如果父表已锁定,则不会处理其子表。如果其中一个子表被锁定了,并且父表在VACUUM
的列表,那么继承树的所有成员都会被处理,除了被锁定的子表。但是,在获取树的样本行的过程中存在一个限制,因为在获取父表统计信息的行样本时,如果对子表加锁,则ANALYZE
将阻塞.
此选项仅支持这些命令的括号语法,例如:
=# VACUUM (SKIP_LOCKED) tab1, tab2;WARNING: 55P03: skipping vacuum of "tab1" --- lock not availableLOCATION: expand_vacuum_rel, vacuum.c:654VACUUM
在这个例子中,列出的第二个表被锁定了。
在此过程中,请注意,由于以下commit,已将更多选项添加到vacuumdb中.
commit: 354e95d1f2122d20c1c5895eb3973cfb0e8d0cc2author: Michael Paquier <michael@paquier.xyz>date: Tue, 8 Jan 2019 10:52:29 +0900Add --disable-page-skipping and --skip-locked to vacuumdbDISABLE_PAGE_SKIPPING is available since v9.6, and SKIP_LOCKED sincev12. They lacked equivalents for vacuumdb, so this closes the gap.Author: Nathan BossartReviewed-by: Michael Paquier, Masahiko SawadaDiscussion: https://postgr.es/m/FFE5373C-E26A-495B-B5C8-911EC4A41C5E@amazon.com
因此,尽管DISABLE_PAGE_SKPING
从9.6开始就出现了,但结合-table
,可以获得与VACUUM
和ANALYZE
提供的相同的映射。这个特性也添加到Postgres 12
了。
文章翻译自:Postgres 12 highlight - SKIP_LOCKED for VACUUM and ANALYZE
https://paquier.xyz/postgresql-2/postgres-12-vacuum-skip-locked/




