Senior
What happens in PostgreSQL when a DELETE FROM table query is executed without conditions?
sobes.tech AI
Answer from AI
When executing the query DELETE FROM table without conditions in PostgreSQL, all rows in the table are deleted.
What happens under the hood:
- Each row is marked as deleted (dead tuple) in the current transaction context.
- Physical data removal does not happen immediately — PostgreSQL uses MVCC, so old versions of rows remain until the transactions that see them are finished.
- To free space, a
VACUUMmust be run, which cleans up dead rows.
Difference from TRUNCATE:
DELETEgenerates WAL records for each deleted row, which can be slower.DELETEtriggers triggers and checks constraints.TRUNCATEquickly clears the table without triggering triggers and without logging each row deletion.