In this PostgreSQL post explains how to use the PostgreSQL VACUUM command with syntax and examples.
The VACUUM statement is used to reclaim storage by removing obsolete data or tuples from the PostgreSQL database.
The syntax for the VACUUM statement in PostgreSQL is:
OR
Optional. If specified, the database writes the full contents of the table into a new file. This reclaims all unused space and requires an exclusive lock on each table that is vacuumed.
Optional. If specified, the tuples are aggressively frozen when the table is vacuumed. This is the default behavior when FULL is specified, so it is redundant to specify both FULL and FREEZE.
Optional. If specified, an activity report will be printed detailing the vacuum activity for each table.
Optional. If specified, the statistics used by the planner will be updated. These statistics are used to determine the most efficient plan for executing a particular query.
Optional. If specified, only the table listed will be vacuumed. If not specified, all tables in the database will be vacuumed.
Optional. If specified, these are the columns that will be analyzed.
In PostgreSQL, the process of vacuuming is a regular activity that must be performed to remove old, obsolete tuples and minimize the size of your database file.
Let's look at an example of how to use the VACUUM statement in PostgreSQL.
This first example shows how to reclaim space so that the unused space can be used by the same table. It does not reduce the size of the PostgreSQL database file as the space is not reclaimed by the operating system, only by the table from which the space was allocated.
For example:
This example would vacuum all tables within the database. It would free up the space within each table and leave the space available to be reused by the same table. It does not return the space to the operating system, therefore, the size of the database file would not be reduced.
If you wanted to vacuum all tables and minimize the database file by returning the unused space to the operating system, you would run the following vacuum statement:
This example would rewrite all tables into a new file, thus requiring an exclusive lock on each table. The database file would be minimized as all of the unused space is reclaimed back to the operating system.
Next, let's look at how to vacuum a specific table, instead of the entire database.
For example:
This example would vacuum only the products table. It would free up the space within the products table and leave the space available to be used by only the products table. The size of the database file would not be reduced.
If you wanted to allocate the unused space back to the operating system, you would have to add the FULL option to the VACUUM statement as follows:
This would not only free up the unused space in the products table, but it would also allow the operating system to reclaim the space and reduce the database size.
Finally, you can add the VERBOSE option to the VACUUM command to display an activity report of the vacuum process.
For example:
This would perform a full vacuum of the products table. Let's show you what you can expect to see as output for a vacuum activity report:
This activity report will display the tables that are vacuumed as well as the details and time taken to perform the vacuum operation.
Next, learn more about the AUTOVACUUM feature starting in PostgreSQL 8.1