What is TRUNCATE and how does it differ from DELETE?

Answer

TRUNCATE removes all rows from a table instantly by deallocating the data pages, making it much faster than DELETE for large tables. Key differences: TRUNCATE does not scan individual rows — it's an O(1) operation regardless of table size. TRUNCATE resets sequences if RESTART IDENTITY is specified. TRUNCATE fires BEFORE/AFTER STATEMENT triggers (not row-level triggers). TRUNCATE is transactional in PostgreSQL (unlike MySQL) — it can be rolled back. DELETE is fully logged row-by-row, supports WHERE filtering, fires row-level triggers, and does not reset sequences. For removing all data from large tables in a script, prefer TRUNCATE. For removing specific rows or when you need RETURNING, use DELETE.