SQL 201 (8 Part Series)
If you work in a Postgres database there are tasks your DBA need to perform to keep things running smoothly. VACUUMing is one of these tasks. This is a high-level overview of why this is important.
The first thing to get to grips with is the concept of Multiversion Concurrency Control (MVCC). The Postgres docs say that this is a way to protect "reading from blocking writing and writing from blocking reading", by providing each transaction with a snapshot of the data.
While this is great when you're querying your data, no one gets locked. The trade-off is that all these snapshots float around only marked for deletion. They are not automatically deleted. These are called 'dead tuples', 'dead rows' or 'bloat' because they clog things up and slow queries down.
Using VACUUM is a way to deal with the garbage collection that needs to be done to keep things running smoothly. This option only clears out the unused data but doesn't rewrite to disk.
VACUUM <tablename> -- to specify a table VACUUM <tablename.columnname> -- to specify a table and column
This is the most no-nonsense way to get the job done. The tradeoff is that VACUUM FULL puts a full lock on the table. Not a great option if you have anyone trying to SELECT as you carry this task out.
VACUUM(FULL) <tablename> -- to specify a table VACUUM(FULL) <tablename.columnname> -- to specify a table and column
This clears out unused data and updates query plan
VACUUM(FULL, ANALYZE) <tablename> -- to specify a table VACUUM(FULL, ANALYZE) <tablename.columnname> -- to specify a table and column
This option keeps things under control automatically by using a trigger to kick off VACUUM when it reaches a certain level. When it is exceeded the VACUUM begins.
vacuum base threshold + vacuum scale factor * number of tuples
These are automatically set but can be adjusted in the postgresql.conf file to fit your data requirements
autovacuum_vacuum_threshold = 50 -- the threshold of 50 rows is set to prevent small tables being overcleaned autovacuum_vacuum_scale_factor = 0.2 -- 20% of a table may have dead tuples
Not necessarily. Performing these tasks uses resources, and depending on which strategy you have in mind, could lock users out while it happens. The advantage of AUTOVACUUM is that is throttled, so it doesn't use all your resources
The aim here is to keep disk space usage at a steady state, not to knock it down to a minimum.
Using the query below you can check on when the process ran by using the pg_stat_user_tables table:
select relname, last_vacuum, last_autovacuum from pg_stat_user_tables;
To tell if AUTOVACUUM is running you can use this query using the pg_stat_activity table:
select datname, usename, pid, state, wait_event, current_timestamp - xact_start AS xact_runtime, query from pg_stat_activity where upper(query) LIKE '%VACUUM%' order by xact_start;
There is more to this process and other maintenance tasks that your friendly DBA will be across. Hopefully, this gives you a good introduction to the concepts and demystifies why you may need to log out so the VACCUUMing can be done.
This post first appeared on helenanderson.co.nz
Photo by JD Hancock