PostgreSQL TRUNCATE TABLE
Summary: in this tutorial, you will learn how to use PostgreSQL TRUNCATE TABLE
statement to quickly delete all data from large tables.
Introduction to PostgreSQL TRUNCATE TABLE statement
To remove all data from a table, you use the DELETE
statement without a WHERE clause. However, when the table has numerous data, the DELETE
statement is not efficient. In this case, you can use the TRUNCATE TABLE
statement.
The TRUNCATE TABLE
statement deletes all data from a table very fast. Here’s the basic syntax of the TRUNCATE TABLE
statement:
In this syntax, you specify the name of the table that you want to delete data after the TRUNCATE TABLE keywords.
Remove all data from multiple tables
To remove all data from multiple tables at once, you separate the tables by commas (,) as follows:
In this syntax, you specify the name of the tables that you want to delete all data after the TRUNCATE TABLE
keywords.
Remove all data from a table that has foreign key references
In practice, the table you want to delete all data often has foreign key references from other tables.
By default, the TRUNCATE TABLE
statement does not remove any data from the table that has foreign key references.
To remove data from a table and other tables that have foreign key references the table, you use CASCADE
option in the TRUNCATE TABLE
statement as follows :
PostgreSQL TRUNCATE TABLE statement examples
Let’s explore some examples of using the TRUNCATE TABLE
statement.
1) Basic PostgreSQL TRUNCATE TABLE statement example
First, create a new table called products
:
Second, insert some rows into the products
table:
Output:
Third, delete all data from the products
table using the TRUNCATE TABLE
statement:
Output:
2) Using PostgreSQL TRUNCATE TABLE statement to delete all data from multiple tables
First, create a table called customers
and insert data into it:
Second, create a table called vendors
and insert data into it:
Third, delete data from the customers and vendors tables using the TRUNCATE TABLE statement:
3) Using PostgreSQL TRUNCATE TABLE statement to delete data from a table referenced by a foreign key
First, create tables orders
and order_details
:
Second, attempt to truncate data from the orders
table:
PostgreSQL issues the following error:
The reason is that the orders
table is referenced by the order_items
table. To truncate both the orders
and order_items
tables at the same time, you can use the CASCADE
option.
Third, truncate data from both orders
and order_items
tables:
PostgreSQL issues the following notice indicating that the order_items
is also truncated:
Note that the TRUNCATE TABLE
statement uses the RESTRICT
option by default to prevent a table that is referenced by a foreign key from being truncated.
Restarting sequence
Besides removing data, you may want to reset the values of the identity column by using the RESTART IDENTITY
option like this:
For example, the following statement removes all rows from the products
table and resets the sequence associated with the id
column:
By default, the TRUNCATE TABLE
statement uses the CONTINUE IDENTITY
option. This option does not restart the value in the sequence associated with the column in the table.
TRUNCATE TABLE statement and ON DELETE trigger
Even though the TRUNCATE TABLE
statement removes all data from a table, it does not fire any ON DELETE
triggers associated with the table.
To fire the trigger when the TRUNCATE TABLE
statement executes, you need to define BEFORE TRUNCATE
and/or AFTER TRUNCATE
triggers for that table.
TRUNCATE TABLE statement and transactions
The TRUNCATE TABLE
is transaction-safe, meaning that you can place it within a transaction.
Why TRUNCATE TABLE statement is more efficient than the DELETE statement
The TRUNCATE TABLE
statement is more efficient than the DELETE
statement due to the following main reasons:
- Minimal logging: The
TRUNCATE TABLE
statement doesn’t generate individual row deletion logs. Instead, it deallocates entire data pages making it faster than theDELETE
statement. - Fewer resources: The truncate operation is more lightweight than the delete option because it doesn’t generate as much undo and redo information. It releases storage space without scanning individual rows.
- Lower-level locking mechanism: The truncate operation often requires lower-level locks and is less prone to conflicts with other transactions, which improves overall system concurrency.
Summary
- Use the
TRUNCATE TABLE
statement to delete all data from a large table very fast. - Use the
CASCADE
option to truncate a table that is referenced by foreign key constraints. - The
TRUNCATE TABLE
deletes data but does not fireON DELETE
triggers. Instead, it fires theBEFORE TRUNCATE
andAFTER TRUNCATE
triggers. - The
TRUNCATE TABLE
statement is transaction-safe.