Junior — Middle
Can you explain what the VACUUM command does in PostgreSQL and how it affects the database?
sobes.tech AI
Answer from AI
The VACUUM command in PostgreSQL is used to clean the database of 'dead' rows that remain after updates and deletes. This helps free up disk space and prevent the growth of tables and indexes.
PostgreSQL uses MVCC (Multi-Version Concurrency Control), so when data is modified, old versions of rows are not immediately deleted but are marked as obsolete. VACUUM removes these obsolete records, improving query performance and reducing disk space usage.
Example of usage:
VACUUM;
There is also the VACUUM FULL command, which more aggressively compresses tables but requires locking.
Regularly running VACUUM is important for maintaining database health and preventing its bloat.