← Back to PostgreSQL, page 2

postgresql-0021 · Easy fresher

How do TRUNCATE and DELETE differ in effect, and what do RESTART IDENTITY and CASCADE do?

DELETE removes rows one by one, fires row triggers, and leaves sequences alone. TRUNCATE empties the whole table at once, much faster and without leaving dead rows, and RESTART IDENTITY resets the table's identity sequence. TRUNCATE refuses tables referenced by foreign keys unless CASCADE is given, which also empties the referencing tables. Both are transactional in PostgreSQL.

Full answer

Setup for the example:

sql
CREATE TABLE carts (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, owner text);
CREATE TABLE cart_items (cart_id int REFERENCES carts(id), sku text);
INSERT INTO carts (owner) VALUES ('Asha'), ('Ben');
INSERT INTO cart_items VALUES (1, 'PEN'), (2, 'INK');
sql
TRUNCATE carts;

Output:

text
ERROR:  cannot truncate a table referenced in a foreign key constraint
sql
BEGIN; TRUNCATE carts RESTART IDENTITY CASCADE; SELECT (SELECT count(*) FROM carts) AS carts, (SELECT count(*) FROM cart_items) AS items;

PostgreSQL returns:

text
NOTICE:  truncate cascades to table "cart_items"
 carts | items
-------+-------
 0     | 0
(1 row)
sql
ROLLBACK; SELECT count(*) AS carts_after_rollback FROM carts;

Result on PostgreSQL 17:

text
 carts_after_rollback
----------------------
 2
(1 row)
sql
TRUNCATE carts RESTART IDENTITY CASCADE; INSERT INTO carts (owner) VALUES ('Chen') RETURNING id;

Result:

text
NOTICE:  truncate cascades to table "cart_items"
 id
----
 1
(1 row)

A plain TRUNCATE fails because cart_items refers to carts. Inside a transaction, CASCADE empties both tables, and ROLLBACK brings every row back, showing that TRUNCATE is transactional in PostgreSQL, unlike in some other databases. After truncating for real with RESTART IDENTITY, the next cart gets id 1 again. TRUNCATE takes an ACCESS EXCLUSIVE lock, so it blocks all readers while it runs, and it does not fire ON DELETE triggers; use DELETE when per-row triggers or concurrent reads matter.

sql
CREATE TABLE carts (id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY, owner text);
CREATE TABLE cart_items (cart_id int REFERENCES carts(id), sku text);
INSERT INTO carts (owner) VALUES ('Asha'), ('Ben');
INSERT INTO cart_items VALUES (1, 'PEN'), (2, 'INK');

TRUNCATE carts;

BEGIN; TRUNCATE carts RESTART IDENTITY CASCADE; SELECT (SELECT count(*) FROM carts) AS carts, (SELECT count(*) FROM cart_items) AS items;

ROLLBACK; SELECT count(*) AS carts_after_rollback FROM carts;

TRUNCATE carts RESTART IDENTITY CASCADE; INSERT INTO carts (owner) VALUES ('Chen') RETURNING id;

Run on PostgreSQL 17.12 (Debian 17.12-0+deb13u1) in a fresh schema, twice, with identical output.

Key points

  • TRUNCATE empties tables at once
  • RESTART IDENTITY resets sequences
  • CASCADE empties referencing tables
  • TRUNCATE is transactional in PostgreSQL
  • It takes an exclusive lock and skips row triggers

Common mistakes

  • Using CASCADE without checking what else empties
  • Expecting DELETE to reset ids

Follow-ups

  • Which triggers fire on TRUNCATE?
  • Why does DELETE leave dead rows behind?

Tips

  • Show the rollback