Before we look at how to tune PostgreSQL auto-vacuum, let’s take a brief look at the importance of vacuuming in PostgreSQL.
Why Autovacuum is necessary
In PostgreSQL, rows that are deleted or modified are not completely removed. Rather they are marked as dead tuples. In other words, PostgreSQL does not physically remove these rows but places a marker on them to prevent future queries from returning or accessing dead tuples.
Dead rows consume storage space, which leads to a bloated database. Bloated database is one of the major reasons why database servers perform poorly (excluding other architectural reasons).
To prevent dead rows from taking up excess storage space, we need to run a background process to recover storage space occupied by dead rows or tuples.
PostgreSQL provides the
commands to get rid of dead rows or tuples. However, both commands differ in how they operate.
command focuses only on getting rid of dead rows to recover storage space whilst the
command gets rid of storage space occupied by dead rows and also updates the statistics used by the query planner to select or choose the most efficient plan for executing a query.
There are other parameters such as
that we can attach to the
command to run vacuum tasks against tables in PostgreSQL.
The PostgreSQL documentation contains additional parameters to customize vacuuming tasks.
The Role of Visibility Map in Vacuum Processing
Postgres version earlier than or before version 8.4 performed poorly in vacuuming dead tuples. Tables are vacuumed via the following steps:
- Postgres scans all tables to get all dead tuples and freeze old tuples if necessary.
- Remove index tuples that point to respective dead tuples.
- Remove dead tuples, update live tuples, clean up index and update table statistics.
However the introduction of visibility map improved vacuum processing in Postgresql. The visibility map determines whether each page in the table file has dead tuples. Vacuum processing skips a page that does not have dead tuples. No scanning of tables is needed in this case.
Configure Postgres Vacuum Processing
Let’s discuss how to appropriately tune auto vacuuming or automatic vacuuming in PostgreSQL using the following parameters in PostgreSQL.
: This parameter is a boolean type:
. The default value is
. In this case, the server starts the
launcher daemon. In addition, PostgreSQL recommends that the
parameter must be enabled for the
parameter enables the collection of statistics on database activity.
Even if you decide to set
, PostgreSQL will launch an autovacuum process if there is an urgent need to prevent transaction wraparound ID.
: Logs vacuuming tasks for a specified amount of time. This parameter is helpful if you want to track or log the activity of autovacuum tasks. For instance, you want to check if there is an autovacuum task that did not run or execute due to a non-existing relation. Setting the value to
logs such information.
: autovacuum workers/processes are not the same as the
launcher looks for tables/relations that need to be vacuumed and then starts the autovacuum workers to work on selected tables/relations. Based on the INTEGER value assigned to this parameter, let’s say, four workers. The launcher starts four workers to work on selected tables.
The default value for the
is 3. If you are dealing with many dead tuples we recommend you to increase number of workers to speed up vacuum processing. Otherwise, the default value is okay.
: This parameter defines the naptime for the autovacuum launcher. So assuming we want the launcher to check up on the database to issue
commands after every two minutes. We can set the integer value of the
. Setting appropriate naptime prevents contention of resources with other background Postgres processes since vacuum processing is initialized periodically. Note that this parameter is not specifically related to autovacuum workers but rather the launcher.
: This parameter defines the threshold to trigger the
command on PostgreSQL tables. So assuming you want to trigger the
command tables with 100 dead tuples. You can set the integer value to 100. The default value is 50 deleted/updated tuples/rows. This parameter specifies the minimum threshold for deleted/updated tuples/rows.
: This parameter is similar to the
parameter. The only difference is inserting tuples/rows instead of updated/deleted rows/tuples. The default value is 1000 tuples. However, if -1 is specified, autovacuum will not initiate a
command on any of the selected tables. You can increase the value for the
parameter if there are enough resources to spare for vacuuming. If not, we advise you not to adjust the default value.
: This parameter specifies the minimum or least number of inserted, updated, or deleted tuples to initiate or trigger the
command on selected tables. The default value is 50 tuples/rows.
Assuming your Postgres server runs on a platform with multiple faster cores, It’s advisable to increase the default value to run or execute the
command frequently since the query planner depends on the statistical results produced by the
command to choose the most efficient execution plan for queries. Thus, if the result in the
system catalog is not up to date, the query planner cannot choose the most efficient execution plan for queries.
: This parameter basically specifies a fraction of the table size which decides when to trigger the
command. It is added to the parameter
. The default value of 20% is optional and appropriate for data platforms with minimal processing speed.
: This parameter is similar to the previous parameter we discussed. The only difference is that this parameter is rather added to the
parameter instead of the
: This parameter determines or specifies a fraction of the table size that is in included the
parameter to decide when to execute the
command. The default value is 0.1 or 10%. Again, it’s advisable to increase the default value if Postgres runs on servers with multiple cores since updated statistics improve query performance.
: This parameter specifies the maximum value for a table’s
column to prevent transaction wraparound within the table. The
command is executed or triggered to freeze old rows or convert old rows into frozen rows. The default value for this transaction is 200 million transactions. We suggest the default value is appropriate for most cases.
: This parameter is similar to the previous one we just discussed but it focuses on the minimum value for multixact before the
command is triggered to prevent multixact ID wraparound within the table. The default value for this parameter is 400 million mutlixacts.
in Postgres refers to more than one transaction concurrently locking a row.
: This parameter is used to prevent autovacuum workers from exceeding work points specified by the parameter
. It is advisable not to increase the value for this parameter to prevent workers from excessively consuming operating system resources to avoid resource contention.
: This parameter prevents autovacuum workers from consuming too much available operating system resources. It is advisable to set the value of the parameter to 0 if you are dealing with large tables to avoid bloated tables.
However, you can set it to -1 if you are dealing with small tables to allow other cpu cores to focus on other high priority processing tasks.
We do not advise you to strictly follow the above instructions to tune autovacuum for PostgreSQL. However you can make use of these steps to understand how autovaccum works in PostgreSQL and tune it to your own preference.
Configuring autovacuum correctly is important to avoid performance degradation cause by dead tuples. If you need advice about tuning it properly, consider our PostgreSQL Health Checks.