PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the TRUNCATE TABLE
command is used to delete all the rows from a table or a set of tables, but it does so without logging individual row deletions. This makes TRUNCATE
significantly faster than using a DELETE
command without a WHERE
clause, especially for tables with a large number of rows.
Here are the key concepts and considerations related to TRUNCATE TABLE
in PostgreSQL:
Basic Usage:
To remove all rows from a table named "my_table", you would use:
TRUNCATE TABLE my_table;
Truncating Multiple Tables:
You can truncate multiple tables in a single command:
TRUNCATE TABLE table1, table2, table3;
Cascade:
If foreign-key constraints exist, you might encounter a constraint violation when attempting to truncate a table. To automatically truncate all tables that have foreign key references to the specified table(s), you can use the CASCADE
option:
TRUNCATE TABLE my_table CASCADE;
Restarting Sequences:
By default, truncating a table does not affect its associated sequences. However, if you want to reset any associated sequences to their starting value, you can use the RESTART IDENTITY
option:
TRUNCATE TABLE my_table RESTART IDENTITY;
Performance:
TRUNCATE
is faster than DELETE
because:
MVCC and Locking:
TRUNCATE
is fast, it requires an exclusive lock on the table(s), which means other operations (both read and write) will be blocked until the TRUNCATE
completes.TRUNCATE
removes all rows, it can quickly free up space. However, due to PostgreSQL's Multi-Version Concurrency Control (MVCC), space will be reclaimed over time and not immediately.Constraints and Triggers:
TRUNCATE
respects triggers. If a table has an AFTER TRUNCATE
trigger, it will be invoked.CASCADE
option can help avoid violations.Rollback:
Just like other SQL operations, TRUNCATE
can be rolled back if executed inside a transaction:
BEGIN; TRUNCATE TABLE my_table; ROLLBACK;
The above commands will result in the table not being truncated, as the operation is rolled back.
Considerations:
TRUNCATE
is a fast and non-logged operation, you should be cautious while using it, especially in production environments. Accidentally truncating a table can lead to data loss.In summary, TRUNCATE TABLE
is a powerful command in PostgreSQL that allows for fast removal of all rows from a table or set of tables. It's essential to understand its behavior, especially regarding foreign key constraints, sequences, and locking, to use it effectively and safely.
PostgreSQL TRUNCATE TABLE example:
Use the TRUNCATE TABLE
statement to quickly delete all rows from a table without logging individual row deletions:
TRUNCATE TABLE example_table;
How to use TRUNCATE TABLE in PostgreSQL:
TRUNCATE TABLE removes all rows from a table, providing a faster alternative to DELETE
for large tables.
PostgreSQL TRUNCATE CASCADE:
Use CASCADE
to automatically truncate dependent tables:
TRUNCATE TABLE parent_table CASCADE;
Reset identity column after TRUNCATE TABLE in PostgreSQL:
After truncating a table, reset the identity column using SETVAL
:
TRUNCATE TABLE example_table RESTART IDENTITY;
TRUNCATE TABLE with conditions in PostgreSQL:
Use a WHERE
clause with TRUNCATE TABLE
to remove specific rows:
TRUNCATE TABLE example_table WHERE condition_column = 'some_condition';
PostgreSQL TRUNCATE TABLE and foreign keys:
TRUNCATE TABLE automatically truncates dependent tables when used with CASCADE
:
TRUNCATE TABLE parent_table CASCADE;
Undo TRUNCATE TABLE in PostgreSQL:
There is no direct undo for TRUNCATE TABLE
. To recover data, restore from a backup or use other recovery methods.