官术网_书友最值得收藏!

Configuring VACUUM and autovacuum

Back in the early days of PostgreSQL projects, people had to run VACUUM manually. Fortunately, this is long gone. Nowadays, administrators can rely on a tool called autovacuum, which is part of the PostgreSQL Server infrastructure. It automatically takes care of cleanup and works in the background. It wakes up once per minute (see autovacuum_naptime = 1 in postgresql.conf) and checks if there is work to do. If there is work, autovacuum will fork up to three worker processes (see autovacuum_max_workers in postgresql.conf).

The main question is, when does autovacuum trigger the creation of a worker process?

Actually, the autovacuum process does not fork processes itself. Instead, it tells the main process to do so. This is done to avoid zombie processes in the case of failure and to improve robustness.

The answer to this question can again be found in postgresql.conf:

autovacuum_vacuum_threshold = 50  
autovacuum_analyze_threshold = 50  
autovacuum_vacuum_scale_factor = 0.2  
autovacuum_analyze_scale_factor = 0.1 

autovacuum_vacuum_scale_factor tells PostgreSQL that a table is worth vacuuming if 20% of data has been changed. The trouble is that if a table consists of one row, one change is already 100%. It makes absolutely no sense to fork a complete process to clean up just one row. Therefore, autovacuum_vacuuum_threshold says that we need 20% and this 20% must be at least 50 rows. Otherwise, VACUUM won't kick in. The same mechanism is used when it comes to optimizer stats creation. 10% and at least 50 rows are needed to justify new optimizer stats. Ideally, autovacuum creates new statistics during a normal VACUUM to avoid unnecessary trips to the table.

主站蜘蛛池模板: 钦州市| 卫辉市| 安新县| 公主岭市| 武城县| 聊城市| 南皮县| 枣强县| 林芝县| 五指山市| 开平市| 连南| 临城县| 交城县| 庆云县| 繁昌县| 麻城市| 阿拉善右旗| 永泰县| 东乌珠穆沁旗| 高雄市| 分宜县| 来凤县| 奇台县| 阳城县| 芜湖县| 友谊县| 荣成市| 华容县| 武城县| 诸暨市| 静乐县| 新巴尔虎左旗| 商都县| 河西区| 凯里市| 嘉鱼县| 海盐县| 武川县| 西林县| 准格尔旗|