<!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.0.15 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="2014-01-24T13:16:52"></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.0.15 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="AEN70160" ></A ><H2 >Name</H2 >TRUNCATE -- empty a table or set of tables</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN70165" ></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="AEN70168" ></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="AEN70174" ></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="AEN70206" ></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. 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. 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 ><DIV CLASS="WARNING" ><P ></P ><TABLE CLASS="WARNING" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > <TT CLASS="COMMAND" >TRUNCATE</TT > is not MVCC-safe (see <A HREF="mvcc.html" >Chapter 13</A > for general information about MVCC). After truncation, the table will appear empty to all concurrent transactions, even if they are using a snapshot taken before the truncation occurred. This will only be an issue for a transaction that did not access the truncated table before the truncation happened — any transaction that has done so would hold at least an <TT CLASS="LITERAL" >ACCESS SHARE</TT > lock, which would block <TT CLASS="COMMAND" >TRUNCATE</TT > until that transaction completes. So truncation will not cause any apparent inconsistency in the table contents for successive queries on the same table, but it could cause visible inconsistency between the contents of the truncated table and other tables in the database. </P ></TD ></TR ></TABLE ></DIV ><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 ><DIV CLASS="WARNING" ><P ></P ><TABLE CLASS="WARNING" BORDER="1" WIDTH="100%" ><TR ><TD ALIGN="CENTER" ><B >Warning</B ></TD ></TR ><TR ><TD ALIGN="LEFT" ><P > Any <TT CLASS="COMMAND" >ALTER SEQUENCE RESTART</TT > operations performed as a consequence of using the <TT CLASS="LITERAL" >RESTART IDENTITY</TT > option are nontransactional and will not be rolled back on failure. To minimize the risk, these operations are performed only after all the rest of <TT CLASS="COMMAND" >TRUNCATE</TT >'s work is done. However, there is still a risk if <TT CLASS="COMMAND" >TRUNCATE</TT > is performed inside a transaction block that is aborted afterwards. For example, consider </P><PRE CLASS="PROGRAMLISTING" >BEGIN; TRUNCATE TABLE foo RESTART IDENTITY; COPY foo FROM ...; COMMIT;</PRE ><P> If the <TT CLASS="COMMAND" >COPY</TT > fails partway through, the table data rolls back correctly, but the sequences will be left with values that are probably smaller than they had before, possibly leading to duplicate-key failures or other problems in later transactions. If this is likely to be a problem, it's best to avoid using <TT CLASS="LITERAL" >RESTART IDENTITY</TT >, and accept that the new contents of the table will have higher serial numbers than the old. </P ></TD ></TR ></TABLE ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN70241" ></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="AEN70253" ></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 but 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 >