summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/man7/TRUNCATE.7
blob: 6e547c7d7e2f7fea1ed71d9d9d2f3aa724809420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
'\" t
.\"     Title: TRUNCATE
.\"    Author: The PostgreSQL Global Development Group
.\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
.\"      Date: 2023
.\"    Manual: PostgreSQL 15.5 Documentation
.\"    Source: PostgreSQL 15.5
.\"  Language: English
.\"
.TH "TRUNCATE" "7" "2023" "PostgreSQL 15.5" "PostgreSQL 15.5 Documentation"
.\" -----------------------------------------------------------------
.\" * Define some portability stuff
.\" -----------------------------------------------------------------
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.\" http://bugs.debian.org/507673
.\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
.\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.ie \n(.g .ds Aq \(aq
.el       .ds Aq '
.\" -----------------------------------------------------------------
.\" * set default formatting
.\" -----------------------------------------------------------------
.\" disable hyphenation
.nh
.\" disable justification (adjust text to left margin only)
.ad l
.\" -----------------------------------------------------------------
.\" * MAIN CONTENT STARTS HERE *
.\" -----------------------------------------------------------------
.SH "NAME"
TRUNCATE \- empty a table or set of tables
.SH "SYNOPSIS"
.sp
.nf
TRUNCATE [ TABLE ] [ ONLY ] \fIname\fR [ * ] [, \&.\&.\&. ]
    [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
.fi
.SH "DESCRIPTION"
.PP
\fBTRUNCATE\fR
quickly removes all rows from a set of tables\&. It has the same effect as an unqualified
\fBDELETE\fR
on each table, but since it does not actually scan the tables it is faster\&. Furthermore, it reclaims disk space immediately, rather than requiring a subsequent
\fBVACUUM\fR
operation\&. This is most useful on large tables\&.
.SH "PARAMETERS"
.PP
\fIname\fR
.RS 4
The name (optionally schema\-qualified) of a table to truncate\&. If
ONLY
is specified before the table name, only that table is truncated\&. If
ONLY
is not specified, the table and all its descendant tables (if any) are truncated\&. Optionally,
*
can be specified after the table name to explicitly indicate that descendant tables are included\&.
.RE
.PP
RESTART IDENTITY
.RS 4
Automatically restart sequences owned by columns of the truncated table(s)\&.
.RE
.PP
CONTINUE IDENTITY
.RS 4
Do not change the values of sequences\&. This is the default\&.
.RE
.PP
CASCADE
.RS 4
Automatically truncate all tables that have foreign\-key references to any of the named tables, or to any tables added to the group due to
CASCADE\&.
.RE
.PP
RESTRICT
.RS 4
Refuse to truncate if any of the tables have foreign\-key references from tables that are not listed in the command\&. This is the default\&.
.RE
.SH "NOTES"
.PP
You must have the
TRUNCATE
privilege on a table to truncate it\&.
.PP
\fBTRUNCATE\fR
acquires an
ACCESS EXCLUSIVE
lock on each table it operates on, which blocks all other concurrent operations on the table\&. When
RESTART IDENTITY
is specified, any sequences that are to be restarted are likewise locked exclusively\&. If concurrent access to a table is required, then the
\fBDELETE\fR
command should be used instead\&.
.PP
\fBTRUNCATE\fR
cannot be used on a table that has foreign\-key references from other tables, unless all such tables are also truncated in the same command\&. Checking validity in such cases would require table scans, and the whole point is not to do one\&. The
CASCADE
option can be used to automatically include all dependent tables \(em but be very careful when using this option, or else you might lose data you did not intend to! Note in particular that when the table to be truncated is a partition, siblings partitions are left untouched, but cascading occurs to all referencing tables and all their partitions with no distinction\&.
.PP
\fBTRUNCATE\fR
will not fire any
ON DELETE
triggers that might exist for the tables\&. But it will fire
ON TRUNCATE
triggers\&. If
ON TRUNCATE
triggers are defined for any of the tables, then all
BEFORE TRUNCATE
triggers are fired before any truncation happens, and all
AFTER TRUNCATE
triggers are fired after the last truncation is performed and any sequences are reset\&. The triggers will fire in the order that the tables are to be processed (first those listed in the command, and then any that were added due to cascading)\&.
.PP
\fBTRUNCATE\fR
is not MVCC\-safe\&. After truncation, the table will appear empty to concurrent transactions, if they are using a snapshot taken before the truncation occurred\&. See
Section\ \&13.6
for more details\&.
.PP
\fBTRUNCATE\fR
is transaction\-safe with respect to the data in the tables: the truncation will be safely rolled back if the surrounding transaction does not commit\&.
.PP
When
RESTART IDENTITY
is specified, the implied
\fBALTER SEQUENCE RESTART\fR
operations are also done transactionally; that is, they will be rolled back if the surrounding transaction does not commit\&. Be aware that if any additional sequence operations are done on the restarted sequences before the transaction rolls back, the effects of these operations on the sequences will be rolled back, but not their effects on
\fBcurrval()\fR; that is, after the transaction
\fBcurrval()\fR
will continue to reflect the last sequence value obtained inside the failed transaction, even though the sequence itself may no longer be consistent with that\&. This is similar to the usual behavior of
\fBcurrval()\fR
after a failed transaction\&.
.PP
\fBTRUNCATE\fR
can be used for foreign tables if supported by the foreign data wrapper, for instance, see
postgres_fdw\&.
.SH "EXAMPLES"
.PP
Truncate the tables
bigtable
and
fattable:
.sp
.if n \{\
.RS 4
.\}
.nf
TRUNCATE bigtable, fattable;
.fi
.if n \{\
.RE
.\}
.PP
The same, and also reset any associated sequence generators:
.sp
.if n \{\
.RS 4
.\}
.nf
TRUNCATE bigtable, fattable RESTART IDENTITY;
.fi
.if n \{\
.RE
.\}
.PP
Truncate the table
othertable, and cascade to any tables that reference
othertable
via foreign\-key constraints:
.sp
.if n \{\
.RS 4
.\}
.nf
TRUNCATE othertable CASCADE;
.fi
.if n \{\
.RE
.\}
.SH "COMPATIBILITY"
.PP
The SQL:2008 standard includes a
\fBTRUNCATE\fR
command with the syntax
TRUNCATE TABLE \fItablename\fR\&. The clauses
CONTINUE IDENTITY/RESTART IDENTITY
also appear in that standard, but have slightly different though related meanings\&. Some of the concurrency behavior of this command is left implementation\-defined by the standard, so the above notes should be considered and compared with other implementations if necessary\&.
.SH "SEE ALSO"
\fBDELETE\fR(7)