TRUNCATE
TRUNCATE TABLE removes every row from one or more tables in one operation:
TRUNCATE TABLE sessions;
TRUNCATE TABLE sessions, events RESTART IDENTITY;
The command is transactional. Inside a transaction, ROLLBACK restores the
removed rows. The table and its indexes remain available for new INSERTs, and
the command returns the TRUNCATE TABLE tag.
Foreign keys
Without CASCADE, the command is rejected when another table has a foreign key
referencing the target, even if the child table is empty:
TRUNCATE TABLE parent CASCADE;
With CASCADE, dependent tables are truncated in the same savepoint, in
child-to-parent order. A ROLLBACK undoes the whole operation. Self-referencing
foreign keys are allowed.
Multiple tables can be listed with commas. Without CASCADE, a referenced table
must be included in the same list or the command is rejected. RESTART IDENTITY
resets sequences owned by serial/bigserial columns to start at 1, including
dependents expanded by CASCADE.
Concurrency and current limitations
TRUNCATE takes an exclusive relation lock. Concurrent writes wait or fail with
55P03, according to lock_timeout.
For heap tables, the command swaps in an empty physical generation. The old
generation is removed only after commit and when no active snapshot depends on
it. B-tree and GIN indexes are swapped together with the heap. Identity resets are transactional
and are undone by ROLLBACK/ROLLBACK TO SAVEPOINT.