Sobes.tech
Senior

What is VACUUM in PostgreSQL and why is it needed?

sobes.tech AI

Answer from AI

VACUUM in PostgreSQL is a database maintenance command that cleans tables of 'dead' rows, which are created after updates and deletes. PostgreSQL uses MVCC (Multi-Version Concurrency Control), so old versions of rows are not immediately removed but remain to ensure transaction consistency.

Why VACUUM is needed:

  • To free space occupied by outdated rows to prevent the growth of table size.
  • To update statistics for the query optimizer.
  • To prevent transaction counter overflow (in the case of VACUUM FULL or autovacuum).

Example usage:

VACUUM ANALYZE my_table;

This command will clean the table and update statistics for the query planner.