先看3个参数:
#如果表的事务ID年龄大于该值, 则数据库触发freeze,即使未开启autovacuum也会强制触发FREEZE
autovacuum_freeze_max_age | 500000
#在手动或自动垃圾回收时, 如果记录的事务ID年龄大于该值, 将被FREEZE
vacuum_freeze_min_age | 10
#在手动垃圾回收时, 如果表的事务ID年龄大于该值, 将触发FREEZE. 该参数的上限值为 %95 autovacuum_freeze_max_age
vacuum_freeze_table_age | 100000
首先如果pg_class表age(relfrozenxid)大于autovacuum_freeze_max_age,那么数据库系统会自动做vacuum,回收年龄
没有达到500000之前,不做vacuum
hank=> select oid::regclass,age(relfrozenxid) from pg_class where relname='hank' and relowner=16392; oid | age ------+-------- hank | 495471 (1 row)
达到后,立即做vacuum
hank=> select oid::regclass,age(relfrozenxid) from pg_class where relname='hank' and relowner=16392; oid | age ------+-------- hank | 504559 (1 row)
可见日志:
2017-08-07 17:23:44.193 CST,,,4774,,5988319d.12a6,2,,2017-08-07 17:23:41 CST,3/5308,0,LOG,00000,"automatic vacuum of table ""hank.hank.hank"": index scans: 0 pages: 0 removed, 1596 remain, 0 skipped due to pins, 0 skipped frozen tuples: 0 removed, 307278 remain, 0 are dead but not yet removable buffer usage: 3204 hits, 1 misses, 1278 dirtied avg read rate: 0.003 MB/s, avg write rate: 3.386 MB/s system usage: CPU 0.00s/0.05u sec elapsed 2.94 sec",,,,,,,,,""
vacuum后age回到vacuum_freeze_min_age所设置的值,注意这里做的是vacuum,不是vacuum freeze,如果手工做vacuum freeze,age(relfrozenxid)会降到0
hank=> select oid::regclass,age(relfrozenxid) from pg_class where relname='hank' and relowner=16392; oid | age ------+----- hank | 10 (1 row)
这里手工freeze后,回到0开始计数
hank=> vacuum freeze hank; VACUUM hank=> select oid::regclass,age(relfrozenxid) from pg_class where relname='hank' and relowner=16392; oid | age ------+----- hank | 0 (1 row)
另外对vacuum_freeze_table_age参数说明一下:
普通的vacuum由visibility map决定表的那些page需要被扫描,正常情况下,会跳过没有dead row的page,即使这些page有旧的xid值,所以,正常vacuum并不冻结表中所有行的xid,vacuum_freeze_table_age控制什么时候需要扫描全表,并冻结所有行的xid,如果事物数大于(vacuum_freeze_table_age-vacuum_freeze_min_age),那么就扫描所有可见的,没有冻结的page,如果vacuum_freeze_table_age设置为0,那么每次都会强制扫描所有page。