VACUUM
reclaims storage occupied by dead tuples. In normal PostgreSQL operation, tuples that are deleted or obsoleted by an update are not physically removed from their table; they remain present until a VACUUM
is done. Therefore it’s necessary to do VACUUM
periodically, especially on frequently-updated tables.
Plain VACUUM
(without FULL
) simply reclaims space and makes it available for re-use. This form of the command can operate in parallel with normal reading and writing of the table, as an exclusive lock is not obtained. However, extra space is not returned to the operating system (in most cases); it’s just kept available for re-use within the same table. It also allows us to leverage multiple CPUs in order to process indexes. This feature is known as parallel vacuum. To disable this feature, one can use PARALLEL
option and specify parallel workers as zero.
VACUUM FULL
rewrites the entire contents of the table into a new disk file with no extra space, allowing unused space to be returned to the operating system. This form is much slower and requires an ACCESS EXCLUSIVE
lock on each table while it is being processed.
To vacuum a table, one must ordinarily be the table’s owner or a superuser. However, database owners are allowed to vacuum all tables in their databases, except shared catalogs. (The restriction for shared catalogs means that a true database-wide VACUUM
can only be performed by a superuser.) VACUUM
will skip over any tables that the calling user does not have permission to vacuum.
VACUUM
cannot be executed inside a transaction block.
The FULL
option is not recommended for routine use, but might be useful in special cases. An example is when you have deleted or updated most of the rows in a table and would like the table to physically shrink to occupy less disk space and allow faster table scans. VACUUM FULL
will usually shrink the table more than a plain VACUUM
would.
an example of table which have dead tuples :
doing VACUUM command will remove dead tuples from it’s table, but it will not reclaim some space
to retain some space in table vacuum_test2 we need to execute VACUUM FULL command. Notice that VACUMM FULL will get exclusive lock which mean all insert or update on it’s table will be delayed until vacumm full process is finish. Of course bigger size of a table will take a lot of time.
As we can see after doing vacuum full the size of vacuum_test2 table has shrink to 17 MB, which become half size from the beginning.
Leave a Reply