Senior
What is bloating in PostgreSQL and why does the physical size of the table grow even after data deletion?
sobes.tech AI
Answer from AI
In PostgreSQL, "bloating" is a situation where the physical size of a table or index grows due to the accumulation of "dead" tuples, which remain after updates or deletions of data. PostgreSQL uses MVCC (multi-version concurrency control), so when rows are deleted or updated, old versions are not immediately removed but are marked as obsolete.
Because of this, the physical size of the table does not automatically decrease after data is deleted. To free up space, you need to run the VACUUM command, which cleans up dead tuples, or VACUUM FULL, which additionally compresses the table and returns space to the OS.
Example:
-- Delete data
DELETE FROM my_table WHERE condition;
-- Free up space
VACUUM my_table;
-- Or for more aggressive compression
VACUUM FULL my_table;
Without regular vacuuming, the table will grow, negatively affecting performance and disk usage.