Use the TRUNCATE command to remove all rows from a table.
This has the same effect as the DELETE command, but is faster than
the DELETE command for large tables. In addition, the TRUNCATE command
frees up all disk space that is allocated to a table, which makes
the space available for use.
You can use TRUNCATE within an explicit transaction or
stored procedure to make other related changes to data in one atomic
unit. When you use TRUNCATE in this manner:
- Except in the case where the table being truncated was created
in the same transaction, the table storage is not freed (made available
for reuse) until the truncating transaction commits.
- The truncating transaction can be rolled back, in which case the
table storage and contents will remain intact.
- The table is locked, and no concurrent access (including SELECT
commands) to the table from other sessions or transactions is permitted
until the truncating transaction commits or rolls back. The rollback
could happen as a result of a ROLLBACK command or an execution failure
in the transaction.
- If the truncating transaction accesses other tables, there is
an increased likelihood of deadlock. You can avoid this problem issuing
LOCK TABLE <tablename> IN ACCESS EXCLUSIVE MODE at the beginning
of any transaction or procedure that truncates a table or tables.
See LOCK TABLE for
guidelines on the use of that command.
- TRUNCATE TABLE is much faster than deleting all rows with DELETE
FROM <table> and also does not require a subsequent GROOM TABLE
to reclaim the space. But it does incur a small risk that in some
cases, concurrent transactions attempting to access and truncate the
same table could see the message ERROR: Cannot access table
because of concurrent truncateor ERROR: Cannot truncate
table because of concurrent insert or update. You can minimize
the risk by using the LOCK TABLE command and by keeping a transaction
that truncates one or more tables active for no longer than is necessary
to achieve its goal.
Syntax
Syntax for truncating an empty table:
TRUNCATE [ TABLE ] <name>
Input
The TRUNCATE command
takes the following input:
Table 1. TRUNCATE input| Input |
Description |
| <name> |
The name of the table to be truncated. |
Output
The TRUNCATE command
has the following output:
Table 2. TRUNCATE output| Output |
Description |
| TRUNCATE TABLE |
The command was successful. |
Privileges
You must be the
admin user, the table owner, the owner of the database or schema,
or your account must have the Truncate command on the table or the
Table object class.
Usage
The following provides
sample usage.