]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/truncate.sgml
Create a "sort support" interface API for faster sorting.
[postgresql] / doc / src / sgml / ref / truncate.sgml
1 <!--
2 doc/src/sgml/ref/truncate.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="SQL-TRUNCATE">
7  <refmeta>
8   <refentrytitle>TRUNCATE</refentrytitle>
9   <manvolnum>7</manvolnum>
10   <refmiscinfo>SQL - Language Statements</refmiscinfo>
11  </refmeta>
12
13  <refnamediv>
14   <refname>TRUNCATE</refname>
15   <refpurpose>empty a table or set of tables</refpurpose>
16  </refnamediv>
17
18  <indexterm zone="sql-truncate">
19   <primary>TRUNCATE</primary>
20  </indexterm>
21
22  <refsynopsisdiv>
23 <synopsis>
24 TRUNCATE [ TABLE ] [ ONLY ] <replaceable class="PARAMETER">name</replaceable> [, ... ]
25     [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
26 </synopsis>
27  </refsynopsisdiv>
28
29  <refsect1>
30   <title>Description</title>
31
32   <para>
33    <command>TRUNCATE</command> quickly removes all rows from a set of
34    tables. It has the same effect as an unqualified
35    <command>DELETE</command> on each table, but since it does not actually
36    scan the tables it is faster. Furthermore, it reclaims disk space
37    immediately, rather than requiring a subsequent <command>VACUUM</command>
38    operation. This is most useful on large tables.
39   </para>
40  </refsect1>
41
42  <refsect1>
43   <title>Parameters</title>
44
45   <variablelist>
46    <varlistentry>
47     <term><replaceable class="PARAMETER">name</replaceable></term>
48     <listitem>
49      <para>
50       The name (optionally schema-qualified) of a table to be
51       truncated.  If <literal>ONLY</> is specified, only that table is
52       truncated.  If <literal>ONLY</> is not specified, the table and
53       all its descendant tables (if any) are truncated.
54      </para>
55     </listitem>
56    </varlistentry>
57
58    <varlistentry>
59     <term><literal>RESTART IDENTITY</literal></term>
60     <listitem>
61      <para>
62       Automatically restart sequences owned by columns of
63       the truncated table(s).
64      </para>
65     </listitem>
66    </varlistentry>
67
68    <varlistentry>
69     <term><literal>CONTINUE IDENTITY</literal></term>
70     <listitem>
71      <para>
72       Do not change the values of sequences.  This is the default.
73      </para>
74     </listitem>
75    </varlistentry>
76
77    <varlistentry>
78     <term><literal>CASCADE</literal></term>
79     <listitem>
80      <para>
81       Automatically truncate all tables that have foreign-key references
82       to any of the named tables, or to any tables added to the group
83       due to <literal>CASCADE</literal>.
84      </para>
85     </listitem>
86    </varlistentry>
87
88    <varlistentry>
89     <term><literal>RESTRICT</literal></term>
90     <listitem>
91      <para>
92       Refuse to truncate if any of the tables have foreign-key references
93       from tables that are not listed in the command.  This is the default.
94      </para>
95     </listitem>
96    </varlistentry>
97   </variablelist>
98  </refsect1>
99
100  <refsect1>
101   <title>Notes</title>
102
103   <para>
104    You must have the <literal>TRUNCATE</literal> privilege on a table
105    to truncate it.
106   </para>
107
108   <para>
109    <command>TRUNCATE</> acquires an <literal>ACCESS EXCLUSIVE</> lock on each
110    table it operates on, which blocks all other concurrent operations
111    on the table.  When <literal>RESTART IDENTITY</> is specified, any
112    sequences that are to be restarted are likewise locked exclusively.
113    If concurrent access to a table is required, then
114    the <command>DELETE</> command should be used instead.
115   </para>
116
117   <para>
118    <command>TRUNCATE</> cannot be used on a table that has foreign-key
119    references from other tables, unless all such tables are also truncated
120    in the same command.  Checking validity in such cases would require table
121    scans, and the whole point is not to do one.  The <literal>CASCADE</>
122    option can be used to automatically include all dependent tables &mdash;
123    but be very careful when using this option, or else you might lose data you
124    did not intend to!
125   </para>
126
127   <para>
128    <command>TRUNCATE</> will not fire any <literal>ON DELETE</literal>
129    triggers that might exist for the tables.  But it will fire
130    <literal>ON TRUNCATE</literal> triggers.
131    If <literal>ON TRUNCATE</> triggers are defined for any of
132    the tables, then all <literal>BEFORE TRUNCATE</literal> triggers are
133    fired before any truncation happens, and all <literal>AFTER
134    TRUNCATE</literal> triggers are fired after the last truncation is
135    performed and any sequences are reset.
136    The triggers will fire in the order that the tables are
137    to be processed (first those listed in the command, and then any
138    that were added due to cascading).
139   </para>
140
141   <warning>
142    <para>
143     <command>TRUNCATE</> is not MVCC-safe (see <xref linkend="mvcc">
144      for general information about MVCC).  After truncation, the table
145      will appear empty to all concurrent transactions, even if they
146      are using a snapshot taken before the truncation occurred.  This
147      will only be an issue for a transaction that did not access the
148      truncated table before the truncation happened &mdash; any
149      transaction that has done so would hold at least an
150      <literal>ACCESS SHARE</literal> lock, which would block
151      <command>TRUNCATE</> until that transaction completes.  So
152      truncation will not cause any apparent inconsistency in the table
153      contents for successive queries on the same table, but it could
154      cause visible inconsistency between the contents of the truncated
155      table and other tables in the database.
156    </para>
157   </warning>
158
159   <para>
160    <command>TRUNCATE</> is transaction-safe with respect to the data
161    in the tables: the truncation will be safely rolled back if the surrounding
162    transaction does not commit.
163   </para>
164
165   <para>
166    When <literal>RESTART IDENTITY</> is specified, the implied
167    <command>ALTER SEQUENCE RESTART</> operations are also done
168    transactionally; that is, they will be rolled back if the surrounding
169    transaction does not commit.  This is unlike the normal behavior of
170    <command>ALTER SEQUENCE RESTART</>.  Be aware that if any additional
171    sequence operations are done on the restarted sequences before the
172    transaction rolls back, the effects of these operations on the sequences
173    will be rolled back, but not their effects on <function>currval()</>;
174    that is, after the transaction <function>currval()</> will continue to
175    reflect the last sequence value obtained inside the failed transaction,
176    even though the sequence itself may no longer be consistent with that.
177    This is similar to the usual behavior of <function>currval()</> after
178    a failed transaction.
179   </para>
180  </refsect1>
181
182  <refsect1>
183   <title>Examples</title>
184
185   <para>
186    Truncate the tables <literal>bigtable</literal> and
187    <literal>fattable</literal>:
188
189 <programlisting>
190 TRUNCATE bigtable, fattable;
191 </programlisting>
192   </para>
193
194   <para>
195    The same, and also reset any associated sequence generators:
196
197 <programlisting>
198 TRUNCATE bigtable, fattable RESTART IDENTITY;
199 </programlisting>
200   </para>
201
202   <para>
203    Truncate the table <literal>othertable</literal>, and cascade to any tables
204    that reference <literal>othertable</literal> via foreign-key
205    constraints:
206
207 <programlisting>
208 TRUNCATE othertable CASCADE;
209 </programlisting></para>
210  </refsect1>
211
212  <refsect1>
213   <title>Compatibility</title>
214
215   <para>
216    The SQL:2008 standard includes a <command>TRUNCATE</command> command
217    with the syntax <literal>TRUNCATE TABLE
218    <replaceable>tablename</replaceable></literal>.  The clauses
219    <literal>CONTINUE IDENTITY</literal>/<literal>RESTART IDENTITY</literal>
220    also appear in that standard, but have slightly different though related
221    meanings.  Some of the concurrency behavior of this command is left
222    implementation-defined by the standard, so the above notes should be
223    considered and compared with other implementations if necessary.
224   </para>
225  </refsect1>
226 </refentry>