You must have the
privilege on a table to truncate it.
lock on each table it operates on, which blocks all other concurrent operations on the table. When
is specified, any sequences that are to be restarted are likewise locked exclusively. If concurrent access to a table is required, then the
command should be used instead.
cannot be used on a table that has foreign-key references from other tables, unless all such tables are also truncated in the same command. Checking validity in such cases would require table scans, and the whole point is not to do one. The
option can be used to automatically include all dependent tables but be very careful when using this option, or else you might lose data you did not intend to!
will not fire any
triggers that might exist for the tables. But it will fire
triggers are defined for any of the tables, then all
triggers are fired before any truncation happens, and all
triggers are fired after the last truncation is performed and any sequences are reset. The triggers will fire in the order that the tables are to be processed (first those listed in the command, and then any that were added due to cascading).
is not MVCC-safe. After truncation, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the truncation occurred. See
Section 13.5, Caveats, in the documentation
for more details.
is transaction-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit.
is specified, the implied
ALTER SEQUENCE RESTART
operations are also done transactionally; that is, they will be rolled back if the surrounding transaction does not commit. This is unlike the normal behavior of
ALTER SEQUENCE RESTART. Be aware that if any additional sequence operations are done on the restarted sequences before the transaction rolls back, the effects of these operations on the sequences will be rolled back, but not their effects on
currval(); that is, after the transaction
will continue to reflect the last sequence value obtained inside the failed transaction, even though the sequence itself may no longer be consistent with that. This is similar to the usual behavior of
after a failed transaction.
is not currently supported for foreign tables. This implies that if a specified table has any descendant tables that are foreign, the command will fail.
Truncate the tables
TRUNCATE bigtable, fattable;
The same, and also reset any associated sequence generators:
TRUNCATE bigtable, fattable RESTART IDENTITY;
Truncate the table
othertable, and cascade to any tables that reference
via foreign-key constraints:
TRUNCATE othertable CASCADE;