18
Full Vacuuming Analogues
CLUSTER
completely rebuilds the table and all its indexes
physically reorders row versions
according to one of the indexes
REINDEX
completely rebuilds indexes
TRUNCATE
empties the table
Features
all these commands completely lock the table
all these commands create new data files
There are several commands that operate using a similar mechanism. All
the commands completely lock the table for operations, remove old data
files and create new ones.
The CLUSTER command is functionally similar to VACUUM FULL, but it
also physically arranges the row versions according to a specified index.
This allows the planner to use index access more efficiently in some cases.
However, this physical order is not maintained, subsequent data
modifications will break it.
REINDEX rebuilds indexes without modifying the table data. In fact,
VACUUM FULL uses this command to rebuild indexes. While reading the
table is not formally prohibited during the operation, the planner will attempt
to get a lock on the index being rebuilt in order to consider possible query
plans. As a result, it is practically impossible to read the table until the
operation completes. The command with CONCURRENTLY option lasts
longer, but it neither locks the index nor interferes with reading and updating
data.
The TRUNCATE command achieves the same result as DELETE by
removing all rows from a table. However, as previously discussed, DELETE
marks row versions as deleted, which requires subsequent vacuuming.
TRUNCATE, by contrast, just creates a new, empty data file. This works
faster, but it is important to note that TRUNCATE locks the table until the
end of the transaction.