4 TRUNCATE — empty a table or set of tables
8 TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
9 [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
13 TRUNCATE quickly removes all rows from a set of tables. It has the same
14 effect as an unqualified DELETE on each table, but since it does not
15 actually scan the tables it is faster. Furthermore, it reclaims disk
16 space immediately, rather than requiring a subsequent VACUUM operation.
17 This is most useful on large tables.
22 The name (optionally schema-qualified) of a table to truncate.
23 If ONLY is specified before the table name, only that table is
24 truncated. If ONLY is not specified, the table and all its
25 descendant tables (if any) are truncated. Optionally, * can be
26 specified after the table name to explicitly indicate that
27 descendant tables are included.
30 Automatically restart sequences owned by columns of the
34 Do not change the values of sequences. This is the default.
37 Automatically truncate all tables that have foreign-key
38 references to any of the named tables, or to any tables added to
39 the group due to CASCADE.
42 Refuse to truncate if any of the tables have foreign-key
43 references from tables that are not listed in the command. This
48 You must have the TRUNCATE privilege on a table to truncate it.
50 TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates
51 on, which blocks all other concurrent operations on the table. When
52 RESTART IDENTITY is specified, any sequences that are to be restarted
53 are likewise locked exclusively. If concurrent access to a table is
54 required, then the DELETE command should be used instead.
56 TRUNCATE cannot be used on a table that has foreign-key references from
57 other tables, unless all such tables are also truncated in the same
58 command. Checking validity in such cases would require table scans, and
59 the whole point is not to do one. The CASCADE option can be used to
60 automatically include all dependent tables — but be very careful when
61 using this option, or else you might lose data you did not intend to!
62 Note in particular that when the table to be truncated is a partition,
63 siblings partitions are left untouched, but cascading occurs to all
64 referencing tables and all their partitions with no distinction.
66 TRUNCATE will not fire any ON DELETE triggers that might exist for the
67 tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers
68 are defined for any of the tables, then all BEFORE TRUNCATE triggers
69 are fired before any truncation happens, and all AFTER TRUNCATE
70 triggers are fired after the last truncation is performed and any
71 sequences are reset. The triggers will fire in the order that the
72 tables are to be processed (first those listed in the command, and then
73 any that were added due to cascading).
75 TRUNCATE is not MVCC-safe. After truncation, the table will appear
76 empty to concurrent transactions, if they are using a snapshot taken
77 before the truncation occurred. See Section 13.6 for more details.
79 TRUNCATE is transaction-safe with respect to the data in the tables:
80 the truncation will be safely rolled back if the surrounding
81 transaction does not commit.
83 When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART
84 operations are also done transactionally; that is, they will be rolled
85 back if the surrounding transaction does not commit. Be aware that if
86 any additional sequence operations are done on the restarted sequences
87 before the transaction rolls back, the effects of these operations on
88 the sequences will be rolled back, but not their effects on currval();
89 that is, after the transaction currval() will continue to reflect the
90 last sequence value obtained inside the failed transaction, even though
91 the sequence itself may no longer be consistent with that. This is
92 similar to the usual behavior of currval() after a failed transaction.
94 TRUNCATE can be used for foreign tables if supported by the foreign
95 data wrapper, for instance, see postgres_fdw.
99 Truncate the tables bigtable and fattable:
100 TRUNCATE bigtable, fattable;
102 The same, and also reset any associated sequence generators:
103 TRUNCATE bigtable, fattable RESTART IDENTITY;
105 Truncate the table othertable, and cascade to any tables that reference
106 othertable via foreign-key constraints:
107 TRUNCATE othertable CASCADE;
111 The SQL:2008 standard includes a TRUNCATE command with the syntax
112 TRUNCATE TABLE tablename. The clauses CONTINUE IDENTITY/RESTART
113 IDENTITY also appear in that standard, but have slightly different
114 though related meanings. Some of the concurrency behavior of this
115 command is left implementation-defined by the standard, so the above
116 notes should be considered and compared with other implementations if