1.查看vacuum相关的参数设置
PGDB2=# select name,setting,unit from pg_settings where name like '%vacuu%';
name | setting | unit
-------------------------------------+-----------+------
autovacuum | on |
autovacuum_analyze_scale_factor | 0.1 |
autovacuum_analyze_threshold | 50 |
autovacuum_freeze_max_age | 200000000 |
autovacuum_max_workers | 10 |
autovacuum_multixact_freeze_max_age | 400000000 |
2.查看当前事务ID
postgres=# select txid_current();
txid_current
--------------
78339
(1 row)
3.检查被冻结的事务ID
postgres=# SELECT oid,datname,datfrozenxid,age(datfrozenxid),datminmxid,age(datminmxid) FROM pg_database;
oid | datname | datfrozenxid | age | datminmxid | age
-------+-----------+--------------+-------+------------+------------
13451 | postgres | 548 | 66889 | 1 | 2154852315
16551 | test | 548 | 66889 | 1 | 2154852315
1 | template1 | 548 | 66889 | 1 | 2154852315
13450 | template0 | 548 | 66889 | 1 | 2154852315
16680 | mxl_db | 548 | 66889 | 1 | 2154852315
16889 | view_test | 548 | 66889 | 1 | 2154852315
20816 | pgbench | 548 | 66889 | 1 | 2154852315
(7 rows)
列参数说明:
datfrozenxid 数据库中最小的冻结的事务ID,此ID之前的所有事务均已被冻结
age(datfrozenxid) 当前事务ID(TXID)与datfrozenxid之间的年龄(距离),这个age大于参数 autovacuum_freeze_max_age,说明vacuum存在延迟
datminmxid 数据库中最小的multixact ID,此ID之前的所有事务均已被冻结
mxid_age(datminmxid) 当前multixact ID与datminmxid之间的年龄(距离),这个age大于参数 autovacuum_multixact_freeze_max_age.说明vacuum存在延迟
4.数据库对象年龄计算
数据库对象年龄的算法是当前的事务id-已经冻结的事务id
postgres=# select 78339 - 548;
?column?
----------
77791
(1 row)
postgres=# select txid_current();
txid_current
--------------
78340
(1 row)
5.检查数据库年龄超过 400000000 (4亿)的数据库
PGDB2=# select datname,age(datfrozenxid) from pg_database where age(datfrozenxid)>400000000 order by 2 desc;
datname | age
---------+-----
(0 行记录)
6.检查数据库对象年龄超过 200000000 ,并且大小大于1G的表
PGDB2=# select relname,age(relfrozenxid),pg_relation_size(oid)/1024/1024/1024.0 as "size(GB)" from pg_class
where relkind='r' and age(relfrozenxid)>200000000 and pg_relation_size(oid)/1024/1024/1024.0 > 1 order by 3 desc;
relname | age | size(GB)
---------+-----+----------
(0 行记录)
7.检查 事务 id 及 vacuum 冻结运行是否正常
PGDB2=# SELECT oid,datname,datfrozenxid,age(datfrozenxid),datminmxid,mxid_age(datminmxid) FROM pg_database;
oid | datname | datfrozenxid | age | datminmxid | age
-------+-----------+--------------+-----------+------------+------------
1 | template1 | 110362903 | 10127220 | 3668424 | 116821
13291 | template0 | 110362903 | 10127220 | 3668426 | 116821
13296 | postgres | 4220243347 | 195214072 | 1 | 2147423
16384 | v5 | 4220243347 | 195214072 | 1 | 2147423
21306 | PGDB2 | 4215660404 | 199797015 | 1 | 2147423
(5 行记录)
8.总结
由此来看,数据库暂时没有需要做Vacuum操作的数据库或对象。