Junior — Middle
Explain what the VACUUM command means in PostgreSQL and its role in database management.
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. PostgreSQL uses MVCC (multi-version concurrency control), so old versions of rows are not immediately removed but are kept to ensure transaction consistency.
Role of VACUUM:
- Freeing space occupied by outdated rows to prevent database size growth.
- Updating statistics for the query planner.
- Preventing transaction ID (XID) wraparound, which can lead to errors.
There are two main types of VACUUM:
- VACUUM — frees space but does not return it to the operating system.
- VACUUM FULL — a more intensive operation that reorganizes the table completely and returns space to the OS.
Example usage:
VACUUM;
VACUUM FULL;
Regularly running VACUUM is important for maintaining database performance and stability.