How to do it…

The first thing to do is make sure that autovacuum is switched on, which is the default. Check that you have the following parameters enabled in your postgresql.conf file:

autovacuum = on 
track_counts = on

PostgreSQL controls autovacuum with more than 40 individually tunable parameters. That provides a wide range of options, though it can be a little daunting.

The following are the relevant parameters that can be set in postgresql.conf to tune the VACUUM command:

vacuum_cost_page_dirty
vacuum_cost_page_hit
vacuum_cost_page_miss
vacuum_cost_delay
vacuum_cost_limit
vacuum_freeze_min_age
vacuum_freeze_table_age
vacuum_multixact_freeze_min_age
vacuum_multixact_freeze_table_age

There are also parameters that apply specifically to the autovacuum process:

autovacuum
autovacuum_work_mem
autovacuum_max_workers
autovacuum_naptime
autovacuum_vacuum_scale_factor
autovacuum_vacuum_threshold
autovacuum_analyze_scale_factor
autovacuum_analyze_threshold
autovacuum_vacuum_cost_delay
autovacuum_vacuum_cost_limit
autovacuum_freeze_max_age
autovacuum_multixact_freeze_max_age
log_autovacuum_min_duration

The preceding parameters apply to all tables at once. Individual tables can be controlled by storage parameters, which are set using the following command:

ALTER TABLE mytable SET (storage_parameter = value);

The storage parameters that relate to maintenance are as follows:

autovacuum_enabled
autovacuum_vacuum_cost_delay
autovacuum_vacuum_cost_limit
autovacuum_vacuum_scale_factor
autovacuum_vacuum_threshold
autovacuum_freeze_min_age
autovacuum_freeze_max_age
autovacuum_freeze_table_age
autovacuum_multixact_freeze_min_age
autovacuum_multixact_freeze_max_age
autovacuum_multixact_freeze_table_age
autovacuum_analyze_scale_factor
autovacuum_analyze_threshold
log_autovacuum_min_duration

The toast tables can be controlled with the following parameters:

toast.autovacuum_enabled 
toast.autovacuum_vacuum_cost_delay
toast.autovacuum_vacuum_cost_limit
toast.autovacuum_vacuum_scale_factor
toast.autovacuum_vacuum_threshold
toast.autovacuum_freeze_min_age
toast.autovacuum_freeze_max_age
toast.autovacuum_freeze_table_age
toast.autovacuum_multixact_freeze_min_age
toast.autovacuum_multixact_freeze_max_age
toast.autovacuum_multixact_freeze_table_age
toast.log_autovacuum_min_duration