Sobes.tech
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 VACUUM must be run, which cleans up dead rows.

Difference from TRUNCATE:

  • DELETE generates WAL records for each deleted row, which can be slower.
  • DELETE triggers triggers and checks constraints.
  • TRUNCATE quickly clears the table without triggering triggers and without logging each row deletion.
What happens in PostgreSQL when a DELETE FROM table… - sobes.tech