The autovacuum is triggered when a high volume of write or delete operations is performed on a particular table.
If your application frequently performs update or delete queries on the same table, consider fine-tuning the following Postgres parameters:
autovacuum_vacuum_scale_factor
: This parameter specifies the fraction of the table size to determine whether to trigger a VACUUM or not. The default value is 0.2 (20% of the table). This means if 20% of the records from a particular time period are updated or deleted, the autovacuum will be triggered.
cost_limit
and cost_delay
: These parameters limit the amount of time spent on each vacuuming process. If the "cost_limit" is too low or "cost_delay" is set too high, it can cause Autovacuum to take longer to complete, potentially leading to contention and locks.
If your database has a large number of tables and Autovacuum runs on all of them simultaneously, it can result in increased contention and locking.
Here are a few useful queries that might help you update these parameters for a specific table:
Specify autovacuum parameters at the table level.
ALTER TABLE <TABLE-NAME> SET (<POSTGRES PARAMETER>= <VALUE>);
To check what parameters have been updated on tables using query
SELECT relname, reloptions FROM pg_class WHERE relname in ('<TABLE NAME>');
Check the last time autovacuum was triggered.
SELECT schemaname, relname, last_vacuum, last_autovacuum, vacuum_count, autovacuum_count FROM pg_stat_user_tables;