TRUNCATE(7)

NAME

   TRUNCATE - empty a table or set of tables

SYNOPSIS

   TRUNCATE [ TABLE ] [ ONLY ] name [ * ] [, ... ]
       [ RESTART IDENTITY | CONTINUE IDENTITY ] [ CASCADE | RESTRICT ]

DESCRIPTION

   TRUNCATE quickly removes all rows from a set of tables. It has the same
   effect as an unqualified DELETE 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 VACUUM operation.
   This is most useful on large tables.

PARAMETERS

   name
       The name (optionally schema-qualified) of a table to truncate. If
       ONLY is specified before the table name, only that table is
       truncated. If ONLY is not specified, the table and all its
       descendant tables (if any) are truncated. Optionally, * can be
       specified after the table name to explicitly indicate that
       descendant tables are included.

   RESTART IDENTITY
       Automatically restart sequences owned by columns of the truncated
       table(s).

   CONTINUE IDENTITY
       Do not change the values of sequences. This is the default.

   CASCADE
       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 CASCADE.

   RESTRICT
       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.

NOTES

   You must have the TRUNCATE privilege on a table to truncate it.

   TRUNCATE acquires an ACCESS EXCLUSIVE lock on each table it operates
   on, which blocks all other concurrent operations on the table. When
   RESTART IDENTITY is specified, any sequences that are to be restarted
   are likewise locked exclusively. If concurrent access to a table is
   required, then the DELETE command should be used instead.

   TRUNCATE 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 CASCADE 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!

   TRUNCATE will not fire any ON DELETE triggers that might exist for the
   tables. But it will fire ON TRUNCATE triggers. If ON TRUNCATE triggers
   are defined for any of the tables, then all BEFORE TRUNCATE triggers
   are fired before any truncation happens, and all AFTER TRUNCATE
   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).

   TRUNCATE 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 Section 13.5, "Caveats", in the
   documentation for more details.

   TRUNCATE 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.

   When RESTART IDENTITY is specified, the implied ALTER SEQUENCE RESTART
   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 ALTER SEQUENCE RESTART. 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 currval();
   that is, after the transaction currval() 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 currval() after a failed transaction.

   TRUNCATE is not currently supported for foreign tables. This implies
   that if a specified table has any descendant tables that are foreign,
   the command will fail.

EXAMPLES

   Truncate the tables bigtable and fattable:

       TRUNCATE bigtable, fattable;

   The same, and also reset any associated sequence generators:

       TRUNCATE bigtable, fattable RESTART IDENTITY;

   Truncate the table othertable, and cascade to any tables that reference
   othertable via foreign-key constraints:

       TRUNCATE othertable CASCADE;

COMPATIBILITY

   The SQL:2008 standard includes a TRUNCATE command with the syntax
   TRUNCATE TABLE tablename. The clauses CONTINUE IDENTITY/RESTART
   IDENTITY 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.



Opportunity


Personal Opportunity - Free software gives you access to billions of dollars of software at no cost. Use this software for your business, personal use or to develop a profitable skill. Access to source code provides access to a level of capabilities/information that companies protect though copyrights. Open source is a core component of the Internet and it is available to you. Leverage the billions of dollars in resources and capabilities to build a career, establish a business or change the world. The potential is endless for those who understand the opportunity.

Business Opportunity - Goldman Sachs, IBM and countless large corporations are leveraging open source to reduce costs, develop products and increase their bottom lines. Learn what these companies know about open source and how open source can give you the advantage.


Free Software


Free Software provides computer programs and capabilities at no cost but more importantly, it provides the freedom to run, edit, contribute to, and share the software. The importance of free software is a matter of access, not price. Software at no cost is a benefit but ownership rights to the software and source code is far more significant.

Free Office Software - The Libre Office suite provides top desktop productivity tools for free. This includes, a word processor, spreadsheet, presentation engine, drawing and flowcharting, database and math applications. Libre Office is available for Linux or Windows.


Free Books


The Free Books Library is a collection of thousands of the most popular public domain books in an online readable format. The collection includes great classical literature and more recent works where the U.S. copyright has expired. These books are yours to read and use without restrictions.

Source Code - Want to change a program or know how it works? Open Source provides the source code for its programs so that anyone can use, modify or learn how to write those programs themselves. Visit the GNU source code repositories to download the source.


Education


Study at Harvard, Stanford or MIT - Open edX provides free online courses from Harvard, MIT, Columbia, UC Berkeley and other top Universities. Hundreds of courses for almost all major subjects and course levels. Open edx also offers some paid courses and selected certifications.

Linux Manual Pages - A man or manual page is a form of software documentation found on Linux/Unix operating systems. Topics covered include computer programs (including library and system calls), formal standards and conventions, and even abstract concepts.