1TRUNCATE(7) PostgreSQL 14.3 Documentation TRUNCATE(7)
2
3
4
6 TRUNCATE - empty a table or set of tables
7
9 TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
10 [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
11
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.
18
20 name
21 The name (optionally schema-qualified) of a table to truncate. If
22 ONLY is specified before the table name, only that table is
23 truncated. If ONLY is not specified, the table and all its
24 descendant tables (if any) are truncated. Optionally, * can be
25 specified after the table name to explicitly indicate that
26 descendant tables are included.
27
28 RESTART IDENTITY
29 Automatically restart sequences owned by columns of the truncated
30 table(s).
31
32 CONTINUE IDENTITY
33 Do not change the values of sequences. This is the default.
34
35 CASCADE
36 Automatically truncate all tables that have foreign-key references
37 to any of the named tables, or to any tables added to the group due
38 to CASCADE.
39
40 RESTRICT
41 Refuse to truncate if any of the tables have foreign-key references
42 from tables that are not listed in the command. This is the
43 default.
44
46 You must have the TRUNCATE privilege on a table to truncate it.
47
48 TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates
49 on, which blocks all other concurrent operations on the table. When
50 RESTART IDENTITY is specified, any sequences that are to be restarted
51 are likewise locked exclusively. If concurrent access to a table is
52 required, then the DELETE command should be used instead.
53
54 TRUNCATE cannot be used on a table that has foreign-key references from
55 other tables, unless all such tables are also truncated in the same
56 command. Checking validity in such cases would require table scans, and
57 the whole point is not to do one. The CASCADE option can be used to
58 automatically include all dependent tables — but be very careful when
59 using this option, or else you might lose data you did not intend to!
60 Note in particular that when the table to be truncated is a partition,
61 siblings partitions are left untouched, but cascading occurs to all
62 referencing tables and all their partitions with no distinction.
63
64 TRUNCATE will not fire any ON DELETE triggers that might exist for the
65 tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers
66 are defined for any of the tables, then all BEFORE TRUNCATE triggers
67 are fired before any truncation happens, and all AFTER TRUNCATE
68 triggers are fired after the last truncation is performed and any
69 sequences are reset. The triggers will fire in the order that the
70 tables are to be processed (first those listed in the command, and then
71 any that were added due to cascading).
72
73 TRUNCATE is not MVCC-safe. After truncation, the table will appear
74 empty to concurrent transactions, if they are using a snapshot taken
75 before the truncation occurred. See Section 13.5 for more details.
76
77 TRUNCATE is transaction-safe with respect to the data in the tables:
78 the truncation will be safely rolled back if the surrounding
79 transaction does not commit.
80
81 When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART
82 operations are also done transactionally; that is, they will be rolled
83 back if the surrounding transaction does not commit. Be aware that if
84 any additional sequence operations are done on the restarted sequences
85 before the transaction rolls back, the effects of these operations on
86 the sequences will be rolled back, but not their effects on currval();
87 that is, after the transaction currval() will continue to reflect the
88 last sequence value obtained inside the failed transaction, even though
89 the sequence itself may no longer be consistent with that. This is
90 similar to the usual behavior of currval() after a failed transaction.
91
92 TRUNCATE can be used for foreign tables if supported by the foreign
93 data wrapper, for instance, see postgres_fdw.
94
96 Truncate the tables bigtable and fattable:
97
98 TRUNCATE bigtable, fattable;
99
100 The same, and also reset any associated sequence generators:
101
102 TRUNCATE bigtable, fattable RESTART IDENTITY;
103
104 Truncate the table othertable, and cascade to any tables that reference
105 othertable via foreign-key constraints:
106
107 TRUNCATE othertable CASCADE;
108
110 The SQL:2008 standard includes a TRUNCATE command with the syntax
111 TRUNCATE TABLE tablename. The clauses CONTINUE IDENTITY/RESTART
112 IDENTITY also appear in that standard, but have slightly different
113 though related meanings. Some of the concurrency behavior of this
114 command is left implementation-defined by the standard, so the above
115 notes should be considered and compared with other implementations if
116 necessary.
117
119 DELETE(7)
120
121
122
123PostgreSQL 14.3 2022 TRUNCATE(7)