What is the difference between DELETE and TRUNCATE in PostgreSQL?

1 Answers
Answered by suresh

Difference between DELETE and TRUNCATE in PostgreSQL

What is the difference between DELETE and TRUNCATE in PostgreSQL?

DELETE in PostgreSQL is a DML (data manipulation language) statement that is used to remove specific rows from a table based on a condition. It is slower because it writes an entry in the WAL (Write Ahead Log) for each deleted row, and also triggers any associated triggers or constraints.

On the other hand, TRUNCATE in PostgreSQL is a DDL (data definition language) statement that is used to remove all rows from a table efficiently. It deallocates the storage space used by the table and resets the sequence values if the table contains any sequences. TRUNCATE is faster than DELETE because it does not generate individual WAL entries for each row.

It is important to note that DELETE can be rolled back using a ROLLBACK statement, while TRUNCATE cannot be rolled back.

Answer for Question: What is the difference between DELETE and TRUNCATE in PostgreSQL?