Junior — Middle
Why does PostgreSQL use the VACUUM command and what is its main role in database management?
sobes.tech AI
Answer from AI
In PostgreSQL, the VACUUM command is used to clean the database of 'dead' rows that remain after updates and deletes. The main role of VACUUM is to free up space occupied by these outdated records to prevent the growth of table size and improve query performance.
PostgreSQL uses MVCC (Multi-Version Concurrency Control), where updates do not overwrite rows but create new versions. Old versions are considered 'dead' and are no longer needed after all transactions that could have seen them are completed. VACUUM removes these old versions.
Additionally, VACUUM updates statistics and metadata, which helps the query planner choose optimal execution plans.
Example usage:
VACUUM;
-- or for a more thorough cleanup
VACUUM FULL;