1TRUNCATE(7)             PostgreSQL 9.2.24 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, “Caveats”, in the
73       documentation for more details.
74
75       TRUNCATE is transaction-safe with respect to the data in the tables:
76       the truncation will be safely rolled back if the surrounding
77       transaction does not commit.
78
79       When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART
80       operations are also done transactionally; that is, they will be rolled
81       back if the surrounding transaction does not commit. This is unlike the
82       normal behavior of ALTER SEQUENCE RESTART. Be aware that if any
83       additional sequence operations are done on the restarted sequences
84       before the transaction rolls back, the effects of these operations on
85       the sequences will be rolled back, but not their effects on currval();
86       that is, after the transaction currval() will continue to reflect the
87       last sequence value obtained inside the failed transaction, even though
88       the sequence itself may no longer be consistent with that. This is
89       similar to the usual behavior of currval() after a failed transaction.
90

EXAMPLES

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

COMPATIBILITY

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