]> begriffs open source - ai-pg/blob - full-docs/man7/TRUNCATE.7
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / man7 / TRUNCATE.7
1 '\" t
2 .\"     Title: TRUNCATE
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "TRUNCATE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 TRUNCATE \- empty a table or set of tables
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 TRUNCATE [ TABLE ] [ ONLY ] \fIname\fR [ * ] [, \&.\&.\&. ]
36     [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
37 .fi
38 .SH "DESCRIPTION"
39 .PP
40 \fBTRUNCATE\fR
41 quickly removes all rows from a set of tables\&. It has the same effect as an unqualified
42 \fBDELETE\fR
43 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
44 \fBVACUUM\fR
45 operation\&. This is most useful on large tables\&.
46 .SH "PARAMETERS"
47 .PP
48 \fIname\fR
49 .RS 4
50 The name (optionally schema\-qualified) of a table to truncate\&. If
51 ONLY
52 is specified before the table name, only that table is truncated\&. If
53 ONLY
54 is not specified, the table and all its descendant tables (if any) are truncated\&. Optionally,
55 *
56 can be specified after the table name to explicitly indicate that descendant tables are included\&.
57 .RE
58 .PP
59 RESTART IDENTITY
60 .RS 4
61 Automatically restart sequences owned by columns of the truncated table(s)\&.
62 .RE
63 .PP
64 CONTINUE IDENTITY
65 .RS 4
66 Do not change the values of sequences\&. This is the default\&.
67 .RE
68 .PP
69 CASCADE
70 .RS 4
71 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
72 CASCADE\&.
73 .RE
74 .PP
75 RESTRICT
76 .RS 4
77 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\&.
78 .RE
79 .SH "NOTES"
80 .PP
81 You must have the
82 TRUNCATE
83 privilege on a table to truncate it\&.
84 .PP
85 \fBTRUNCATE\fR
86 acquires an
87 ACCESS EXCLUSIVE
88 lock on each table it operates on, which blocks all other concurrent operations on the table\&. When
89 RESTART IDENTITY
90 is specified, any sequences that are to be restarted are likewise locked exclusively\&. If concurrent access to a table is required, then the
91 \fBDELETE\fR
92 command should be used instead\&.
93 .PP
94 \fBTRUNCATE\fR
95 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
96 CASCADE
97 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\&.
98 .PP
99 \fBTRUNCATE\fR
100 will not fire any
101 ON DELETE
102 triggers that might exist for the tables\&. But it will fire
103 ON TRUNCATE
104 triggers\&. If
105 ON TRUNCATE
106 triggers are defined for any of the tables, then all
107 BEFORE TRUNCATE
108 triggers are fired before any truncation happens, and all
109 AFTER TRUNCATE
110 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)\&.
111 .PP
112 \fBTRUNCATE\fR
113 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
114 Section\ \&13.6
115 for more details\&.
116 .PP
117 \fBTRUNCATE\fR
118 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\&.
119 .PP
120 When
121 RESTART IDENTITY
122 is specified, the implied
123 \fBALTER SEQUENCE RESTART\fR
124 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
125 \fBcurrval()\fR; that is, after the transaction
126 \fBcurrval()\fR
127 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
128 \fBcurrval()\fR
129 after a failed transaction\&.
130 .PP
131 \fBTRUNCATE\fR
132 can be used for foreign tables if supported by the foreign data wrapper, for instance, see
133 postgres_fdw\&.
134 .SH "EXAMPLES"
135 .PP
136 Truncate the tables
137 bigtable
138 and
139 fattable:
140 .sp
141 .if n \{\
142 .RS 4
143 .\}
144 .nf
145 TRUNCATE bigtable, fattable;
146 .fi
147 .if n \{\
148 .RE
149 .\}
150 .PP
151 The same, and also reset any associated sequence generators:
152 .sp
153 .if n \{\
154 .RS 4
155 .\}
156 .nf
157 TRUNCATE bigtable, fattable RESTART IDENTITY;
158 .fi
159 .if n \{\
160 .RE
161 .\}
162 .PP
163 Truncate the table
164 othertable, and cascade to any tables that reference
165 othertable
166 via foreign\-key constraints:
167 .sp
168 .if n \{\
169 .RS 4
170 .\}
171 .nf
172 TRUNCATE othertable CASCADE;
173 .fi
174 .if n \{\
175 .RE
176 .\}
177 .SH "COMPATIBILITY"
178 .PP
179 The SQL:2008 standard includes a
180 \fBTRUNCATE\fR
181 command with the syntax
182 TRUNCATE TABLE \fItablename\fR\&. The clauses
183 CONTINUE IDENTITY/RESTART IDENTITY
184 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\&.
185 .SH "SEE ALSO"
186 \fBDELETE\fR(7)