1TRUNCATE(7)              PostgreSQL 12.2 Documentation             TRUNCATE(7)
2
3
4

NAME

6       TRUNCATE - empty a table or set of tables
7

SYNOPSIS

9       TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
10           [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
11

DESCRIPTION

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

PARAMETERS

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

NOTES

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. This is unlike the
84       normal behavior of ALTER SEQUENCE RESTART. Be aware that if any
85       additional sequence operations are done on the restarted sequences
86       before the transaction rolls back, the effects of these operations on
87       the sequences will be rolled back, but not their effects on currval();
88       that is, after the transaction currval() will continue to reflect the
89       last sequence value obtained inside the failed transaction, even though
90       the sequence itself may no longer be consistent with that. This is
91       similar to the usual behavior of currval() after a failed transaction.
92
93       TRUNCATE is not currently supported for foreign tables. This implies
94       that if a specified table has any descendant tables that are foreign,
95       the command will fail.
96

EXAMPLES

98       Truncate the tables bigtable and fattable:
99
100           TRUNCATE bigtable, fattable;
101
102       The same, and also reset any associated sequence generators:
103
104           TRUNCATE bigtable, fattable RESTART IDENTITY;
105
106       Truncate the table othertable, and cascade to any tables that reference
107       othertable via foreign-key constraints:
108
109           TRUNCATE othertable CASCADE;
110

COMPATIBILITY

112       The SQL:2008 standard includes a TRUNCATE command with the syntax
113       TRUNCATE TABLE tablename. The clauses CONTINUE IDENTITY/RESTART
114       IDENTITY also appear in that standard, but have slightly different
115       though related meanings. Some of the concurrency behavior of this
116       command is left implementation-defined by the standard, so the above
117       notes should be considered and compared with other implementations if
118       necessary.
119

SEE ALSO

121       DELETE(7)
122
123
124
125PostgreSQL 12.2                      2020                          TRUNCATE(7)
Impressum