Current File : //usr/share/doc/postgresql-9.2.24/html/sql-truncate.html |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>TRUNCATE</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.2.24 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="SQL Commands"
HREF="sql-commands.html"><LINK
REL="PREVIOUS"
TITLE="START TRANSACTION"
HREF="sql-start-transaction.html"><LINK
REL="NEXT"
TITLE="UNLISTEN"
HREF="sql-unlisten.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2017-11-06T22:43:11"></HEAD
><BODY
CLASS="REFENTRY"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.2.24 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="START TRANSACTION"
HREF="sql-start-transaction.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="UNLISTEN"
HREF="sql-unlisten.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><H1
><A
NAME="SQL-TRUNCATE"
></A
>TRUNCATE</H1
><DIV
CLASS="REFNAMEDIV"
><A
NAME="AEN79342"
></A
><H2
>Name</H2
>TRUNCATE -- empty a table or set of tables</DIV
><DIV
CLASS="REFSYNOPSISDIV"
><A
NAME="AEN79347"
></A
><H2
>Synopsis</H2
><PRE
CLASS="SYNOPSIS"
>TRUNCATE [ TABLE ] [ ONLY ] <TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
> [ * ] [, ... ]
[ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]</PRE
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79350"
></A
><H2
>Description</H2
><P
> <TT
CLASS="COMMAND"
>TRUNCATE</TT
> quickly removes all rows from a set of
tables. It has the same effect as an unqualified
<TT
CLASS="COMMAND"
>DELETE</TT
> 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 <TT
CLASS="COMMAND"
>VACUUM</TT
>
operation. This is most useful on large tables.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79356"
></A
><H2
>Parameters</H2
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="REPLACEABLE"
><I
>name</I
></TT
></DT
><DD
><P
> The name (optionally schema-qualified) of a table to truncate.
If <TT
CLASS="LITERAL"
>ONLY</TT
> is specified before the table name, only that table
is truncated. If <TT
CLASS="LITERAL"
>ONLY</TT
> is not specified, the table and all
its descendant tables (if any) are truncated. Optionally, <TT
CLASS="LITERAL"
>*</TT
>
can be specified after the table name to explicitly indicate that
descendant tables are included.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>RESTART IDENTITY</TT
></DT
><DD
><P
> Automatically restart sequences owned by columns of
the truncated table(s).
</P
></DD
><DT
><TT
CLASS="LITERAL"
>CONTINUE IDENTITY</TT
></DT
><DD
><P
> Do not change the values of sequences. This is the default.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>CASCADE</TT
></DT
><DD
><P
> 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 <TT
CLASS="LITERAL"
>CASCADE</TT
>.
</P
></DD
><DT
><TT
CLASS="LITERAL"
>RESTRICT</TT
></DT
><DD
><P
> 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.
</P
></DD
></DL
></DIV
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79388"
></A
><H2
>Notes</H2
><P
> You must have the <TT
CLASS="LITERAL"
>TRUNCATE</TT
> privilege on a table
to truncate it.
</P
><P
> <TT
CLASS="COMMAND"
>TRUNCATE</TT
> acquires an <TT
CLASS="LITERAL"
>ACCESS EXCLUSIVE</TT
> lock on each
table it operates on, which blocks all other concurrent operations
on the table. When <TT
CLASS="LITERAL"
>RESTART IDENTITY</TT
> is specified, any
sequences that are to be restarted are likewise locked exclusively.
If concurrent access to a table is required, then
the <TT
CLASS="COMMAND"
>DELETE</TT
> command should be used instead.
</P
><P
> <TT
CLASS="COMMAND"
>TRUNCATE</TT
> 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 <TT
CLASS="LITERAL"
>CASCADE</TT
>
option can be used to automatically include all dependent tables —
but be very careful when using this option, or else you might lose data you
did not intend to!
</P
><P
> <TT
CLASS="COMMAND"
>TRUNCATE</TT
> will not fire any <TT
CLASS="LITERAL"
>ON DELETE</TT
>
triggers that might exist for the tables. But it will fire
<TT
CLASS="LITERAL"
>ON TRUNCATE</TT
> triggers.
If <TT
CLASS="LITERAL"
>ON TRUNCATE</TT
> triggers are defined for any of
the tables, then all <TT
CLASS="LITERAL"
>BEFORE TRUNCATE</TT
> triggers are
fired before any truncation happens, and all <TT
CLASS="LITERAL"
>AFTER
TRUNCATE</TT
> 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).
</P
><P
> <TT
CLASS="COMMAND"
>TRUNCATE</TT
> 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 <A
HREF="mvcc-caveats.html"
>Section 13.5</A
> for more details.
</P
><P
> <TT
CLASS="COMMAND"
>TRUNCATE</TT
> 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.
</P
><P
> When <TT
CLASS="LITERAL"
>RESTART IDENTITY</TT
> is specified, the implied
<TT
CLASS="COMMAND"
>ALTER SEQUENCE RESTART</TT
> operations are also done
transactionally; that is, they will be rolled back if the surrounding
transaction does not commit. This is unlike the normal behavior of
<TT
CLASS="COMMAND"
>ALTER SEQUENCE RESTART</TT
>. 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 <CODE
CLASS="FUNCTION"
>currval()</CODE
>;
that is, after the transaction <CODE
CLASS="FUNCTION"
>currval()</CODE
> 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 <CODE
CLASS="FUNCTION"
>currval()</CODE
> after
a failed transaction.
</P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79419"
></A
><H2
>Examples</H2
><P
> Truncate the tables <TT
CLASS="LITERAL"
>bigtable</TT
> and
<TT
CLASS="LITERAL"
>fattable</TT
>:
</P><PRE
CLASS="PROGRAMLISTING"
>TRUNCATE bigtable, fattable;</PRE
><P>
</P
><P
> The same, and also reset any associated sequence generators:
</P><PRE
CLASS="PROGRAMLISTING"
>TRUNCATE bigtable, fattable RESTART IDENTITY;</PRE
><P>
</P
><P
> Truncate the table <TT
CLASS="LITERAL"
>othertable</TT
>, and cascade to any tables
that reference <TT
CLASS="LITERAL"
>othertable</TT
> via foreign-key
constraints:
</P><PRE
CLASS="PROGRAMLISTING"
>TRUNCATE othertable CASCADE;</PRE
><P></P
></DIV
><DIV
CLASS="REFSECT1"
><A
NAME="AEN79431"
></A
><H2
>Compatibility</H2
><P
> The SQL:2008 standard includes a <TT
CLASS="COMMAND"
>TRUNCATE</TT
> command
with the syntax <TT
CLASS="LITERAL"
>TRUNCATE TABLE
<TT
CLASS="REPLACEABLE"
><I
>tablename</I
></TT
></TT
>. The clauses
<TT
CLASS="LITERAL"
>CONTINUE IDENTITY</TT
>/<TT
CLASS="LITERAL"
>RESTART IDENTITY</TT
>
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.
</P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="sql-start-transaction.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="sql-unlisten.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>START TRANSACTION</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="sql-commands.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>UNLISTEN</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>