1TRUNCATE(7)              PostgreSQL 10.7 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
61       TRUNCATE will not fire any ON DELETE triggers that might exist for the
62       tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers
63       are defined for any of the tables, then all BEFORE TRUNCATE triggers
64       are fired before any truncation happens, and all AFTER TRUNCATE
65       triggers are fired after the last truncation is performed and any
66       sequences are reset. The triggers will fire in the order that the
67       tables are to be processed (first those listed in the command, and then
68       any that were added due to cascading).
69
70       TRUNCATE is not MVCC-safe. After truncation, the table will appear
71       empty to concurrent transactions, if they are using a snapshot taken
72       before the truncation occurred. See Section 13.5 for more details.
73
74       TRUNCATE is transaction-safe with respect to the data in the tables:
75       the truncation will be safely rolled back if the surrounding
76       transaction does not commit.
77
78       When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART
79       operations are also done transactionally; that is, they will be rolled
80       back if the surrounding transaction does not commit. This is unlike the
81       normal behavior of ALTER SEQUENCE RESTART. Be aware that if any
82       additional sequence operations are done on the restarted sequences
83       before the transaction rolls back, the effects of these operations on
84       the sequences will be rolled back, but not their effects on currval();
85       that is, after the transaction currval() will continue to reflect the
86       last sequence value obtained inside the failed transaction, even though
87       the sequence itself may no longer be consistent with that. This is
88       similar to the usual behavior of currval() after a failed transaction.
89
90       TRUNCATE is not currently supported for foreign tables. This implies
91       that if a specified table has any descendant tables that are foreign,
92       the command will fail.
93

EXAMPLES

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

COMPATIBILITY

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

SEE ALSO

118       DELETE(7)
119
120
121
122PostgreSQL 10.7                      2019                          TRUNCATE(7)
Impressum