Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if PostgreSQL's Autovacuum is running on UNIX?

How can one tell if the autovacuum daemon in Postgres 9.x is running and maintaining the database cluster?

like image 974
Clint Pachl Avatar asked Nov 07 '13 01:11

Clint Pachl


People also ask

Is Autovacuum enabled by default?

The AUTOVACUUM daemon is enabled in the default configuration. The AUTOVACUUM daemon is made up of multiple processes that reclaim storage by removing obsolete data or tuples from the database.

How often does Postgres Autovacuum run?

For every database in a cluster autovacuum attempts to start a new worker once every autovacuum_naptime (default 1 minute). It will run at most autovacuum_max_workers (default 3) at a time.

Is Autovacuum blocked?

Autovacuum does take a lock on the table, but it is a weak lock which does not interfere with normal operations (SELECT, UPDATE, DELETE) but will interfere with things like adding indexes, or truncating the table.

What does Postgres Autovacuum do?

PostgreSQL has an optional but highly recommended feature called autovacuum, whose purpose is to automate the execution of VACUUM and ANALYZE commands. When enabled, autovacuum checks for tables that have had a large number of inserted, updated or deleted tuples.


2 Answers

PostgreSQL 9.3

Determine if Autovacuum is Running

This is specific to Postgres 9.3 on UNIX. For Windows, see this question.

Query Postgres System Table

SELECT   schemaname, relname,   last_vacuum, last_autovacuum,   vacuum_count, autovacuum_count  -- not available on 9.0 and earlier FROM pg_stat_user_tables; 

Grep System Process Status

$ ps -axww | grep autovacuum 24352 ??  Ss      1:05.33 postgres: autovacuum launcher process  (postgres)     

Grep Postgres Log

# grep autovacuum /var/log/postgresql LOG:  autovacuum launcher started LOG:  autovacuum launcher shutting down 

If you want to know more about the autovacuum activity, set log_min_messages to DEBUG1..DEBUG5. The SQL command VACUUM VERBOSE will output information at log level INFO.


Regarding the Autovacuum Daemon, the Posgres docs state:

In the default configuration, autovacuuming is enabled and the related configuration parameters are appropriately set.

See Also:

  • http://www.postgresql.org/docs/current/static/routine-vacuuming.html
  • http://www.postgresql.org/docs/current/static/runtime-config-autovacuum.html
like image 136
Clint Pachl Avatar answered Sep 21 '22 20:09

Clint Pachl


I'm using:

select count(*) from pg_stat_activity where query like 'autovacuum:%'; 

in collectd to know how many autovacuum are running concurrently.

You may need to create a security function like this:

CREATE OR REPLACE FUNCTION public.pg_autovacuum_count() RETURNS bigint AS 'select count(*) from pg_stat_activity where query like ''autovacuum:%'';' LANGUAGE SQL STABLE SECURITY DEFINER; 

and call that from collectd.

In earlier Postgres, "query" was "current_query" so change it according to what works.

like image 36
Charly Koza Avatar answered Sep 20 '22 20:09

Charly Koza