Senior
What happens to data during a DELETE in PostgreSQL?
sobes.tech AI
Answer from AI
When executing the DELETE command in PostgreSQL, rows are logically deleted from the table. Physical data deletion does not happen immediately — rows are marked as deleted (dead tuples), but remain in the table until the VACUUM operation is performed.
This is related to MVCC (Multi-Version Concurrency Control), which allows other transactions to see a consistent state of data without locks.
Thus:
- DELETE marks rows as deleted for the current transaction.
- Other transactions that started before the deletion can see these rows.
- VACUUM physically removes the deleted rows, freeing space.
Example:
DELETE FROM users WHERE id = 123;
-- The row with id=123 is marked as deleted but remains in the table until VACUUM.