]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/sql-truncate.html
WIP: toc builder
[ai-pg] / full-docs / src / sgml / html / sql-truncate.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>TRUNCATE</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-start-transaction.html" title="START TRANSACTION" /><link rel="next" href="sql-unlisten.html" title="UNLISTEN" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">TRUNCATE</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-start-transaction.html" title="START TRANSACTION">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><th width="60%" align="center">SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-unlisten.html" title="UNLISTEN">Next</a></td></tr></table><hr /></div><div class="refentry" id="SQL-TRUNCATE"><div class="titlepage"></div><a id="id-1.9.3.181.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">TRUNCATE</span></h2><p>TRUNCATE — empty a table or set of tables</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 TRUNCATE [ TABLE ] [ ONLY ] <em class="replaceable"><code>name</code></em> [ * ] [, ... ]
4     [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]
5 </pre></div><div class="refsect1" id="id-1.9.3.181.5"><h2>Description</h2><p>
6    <code class="command">TRUNCATE</code> quickly removes all rows from a set of
7    tables. It has the same effect as an unqualified
8    <code class="command">DELETE</code> on each table, but since it does not actually
9    scan the tables it is faster. Furthermore, it reclaims disk space
10    immediately, rather than requiring a subsequent <code class="command">VACUUM</code>
11    operation. This is most useful on large tables.
12   </p></div><div class="refsect1" id="id-1.9.3.181.6"><h2>Parameters</h2><div class="variablelist"><dl class="variablelist"><dt><span class="term"><em class="replaceable"><code>name</code></em></span></dt><dd><p>
13       The name (optionally schema-qualified) of a table to truncate.
14       If <code class="literal">ONLY</code> is specified before the table name, only that table
15       is truncated.  If <code class="literal">ONLY</code> is not specified, the table and all
16       its descendant tables (if any) are truncated.  Optionally, <code class="literal">*</code>
17       can be specified after the table name to explicitly indicate that
18       descendant tables are included.
19      </p></dd><dt><span class="term"><code class="literal">RESTART IDENTITY</code></span></dt><dd><p>
20       Automatically restart sequences owned by columns of
21       the truncated table(s).
22      </p></dd><dt><span class="term"><code class="literal">CONTINUE IDENTITY</code></span></dt><dd><p>
23       Do not change the values of sequences.  This is the default.
24      </p></dd><dt><span class="term"><code class="literal">CASCADE</code></span></dt><dd><p>
25       Automatically truncate all tables that have foreign-key references
26       to any of the named tables, or to any tables added to the group
27       due to <code class="literal">CASCADE</code>.
28      </p></dd><dt><span class="term"><code class="literal">RESTRICT</code></span></dt><dd><p>
29       Refuse to truncate if any of the tables have foreign-key references
30       from tables that are not listed in the command.  This is the default.
31      </p></dd></dl></div></div><div class="refsect1" id="id-1.9.3.181.7"><h2>Notes</h2><p>
32    You must have the <code class="literal">TRUNCATE</code> privilege on a table
33    to truncate it.
34   </p><p>
35    <code class="command">TRUNCATE</code> acquires an <code class="literal">ACCESS EXCLUSIVE</code> lock on each
36    table it operates on, which blocks all other concurrent operations
37    on the table.  When <code class="literal">RESTART IDENTITY</code> is specified, any
38    sequences that are to be restarted are likewise locked exclusively.
39    If concurrent access to a table is required, then
40    the <code class="command">DELETE</code> command should be used instead.
41   </p><p>
42    <code class="command">TRUNCATE</code> cannot be used on a table that has foreign-key
43    references from other tables, unless all such tables are also truncated
44    in the same command.  Checking validity in such cases would require table
45    scans, and the whole point is not to do one.  The <code class="literal">CASCADE</code>
46    option can be used to automatically include all dependent tables —
47    but be very careful when using this option, or else you might lose data you
48    did not intend to!
49    Note in particular that when the table to be truncated is a partition,
50    siblings partitions are left untouched, but cascading occurs to all
51    referencing tables and all their partitions with no distinction.
52   </p><p>
53    <code class="command">TRUNCATE</code> will not fire any <code class="literal">ON DELETE</code>
54    triggers that might exist for the tables.  But it will fire
55    <code class="literal">ON TRUNCATE</code> triggers.
56    If <code class="literal">ON TRUNCATE</code> triggers are defined for any of
57    the tables, then all <code class="literal">BEFORE TRUNCATE</code> triggers are
58    fired before any truncation happens, and all <code class="literal">AFTER
59    TRUNCATE</code> triggers are fired after the last truncation is
60    performed and any sequences are reset.
61    The triggers will fire in the order that the tables are
62    to be processed (first those listed in the command, and then any
63    that were added due to cascading).
64   </p><p>
65    <code class="command">TRUNCATE</code> is not MVCC-safe.  After truncation, the table will
66    appear empty to concurrent transactions, if they are using a snapshot
67    taken before the truncation occurred.
68    See <a class="xref" href="mvcc-caveats.html" title="13.6. Caveats">Section 13.6</a> for more details.
69   </p><p>
70    <code class="command">TRUNCATE</code> is transaction-safe with respect to the data
71    in the tables: the truncation will be safely rolled back if the surrounding
72    transaction does not commit.
73   </p><p>
74    When <code class="literal">RESTART IDENTITY</code> is specified, the implied
75    <code class="command">ALTER SEQUENCE RESTART</code> operations are also done
76    transactionally; that is, they will be rolled back if the surrounding
77    transaction does not commit.  Be aware that if any additional
78    sequence operations are done on the restarted sequences before the
79    transaction rolls back, the effects of these operations on the sequences
80    will be rolled back, but not their effects on <code class="function">currval()</code>;
81    that is, after the transaction <code class="function">currval()</code> will continue to
82    reflect the last sequence value obtained inside the failed transaction,
83    even though the sequence itself may no longer be consistent with that.
84    This is similar to the usual behavior of <code class="function">currval()</code> after
85    a failed transaction.
86   </p><p>
87    <code class="command">TRUNCATE</code> can be used for foreign tables if
88    supported by the foreign data wrapper, for instance,
89    see <a class="xref" href="postgres-fdw.html" title="F.38. postgres_fdw — access data stored in external PostgreSQL servers">postgres_fdw</a>.
90   </p></div><div class="refsect1" id="id-1.9.3.181.8"><h2>Examples</h2><p>
91    Truncate the tables <code class="literal">bigtable</code> and
92    <code class="literal">fattable</code>:
93
94 </p><pre class="programlisting">
95 TRUNCATE bigtable, fattable;
96 </pre><p>
97   </p><p>
98    The same, and also reset any associated sequence generators:
99
100 </p><pre class="programlisting">
101 TRUNCATE bigtable, fattable RESTART IDENTITY;
102 </pre><p>
103   </p><p>
104    Truncate the table <code class="literal">othertable</code>, and cascade to any tables
105    that reference <code class="literal">othertable</code> via foreign-key
106    constraints:
107
108 </p><pre class="programlisting">
109 TRUNCATE othertable CASCADE;
110 </pre></div><div class="refsect1" id="id-1.9.3.181.9"><h2>Compatibility</h2><p>
111    The SQL:2008 standard includes a <code class="command">TRUNCATE</code> command
112    with the syntax <code class="literal">TRUNCATE TABLE
113    <em class="replaceable"><code>tablename</code></em></code>.  The clauses
114    <code class="literal">CONTINUE IDENTITY</code>/<code class="literal">RESTART IDENTITY</code>
115    also appear in that standard, but have slightly different though related
116    meanings.  Some of the concurrency behavior of this command is left
117    implementation-defined by the standard, so the above notes should be
118    considered and compared with other implementations if necessary.
119   </p></div><div class="refsect1" id="id-1.9.3.181.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="sql-delete.html" title="DELETE"><span class="refentrytitle">DELETE</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-start-transaction.html" title="START TRANSACTION">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-commands.html" title="SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-unlisten.html" title="UNLISTEN">Next</a></td></tr><tr><td width="40%" align="left" valign="top">START TRANSACTION </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> UNLISTEN</td></tr></table></div></body></html>