Senior
What does VACUUM do in PostgreSQL and why do dead tuples appear?
sobes.tech AI
Answer from AI
In PostgreSQL, when updating or deleting rows, the old versions of the rows are not immediately removed but become "dead tuples". This is related to MVCC (Multi-Version Concurrency Control), which ensures transaction isolation. Dead rows occupy space and can slow down the database.
The VACUUM command cleans up these dead rows, freeing space and preventing the growth of tables and indexes. There is a regular VACUUM, which simply marks space as free, and VACUUM FULL, which additionally compresses the table, reducing its physical size.
Example:
VACUUM my_table;
Thus, VACUUM maintains performance and prevents excessive disk space usage.