PostgreSQL Truncate Table
Overview
The TRUNCATE TABLE statement quickly removes all data from a table while preserving the table structure. Compared to DELETE, TRUNCATE is faster and more efficient, especially for large tables.
TRUNCATE Statement
Basic Syntax
Simple Examples
Complete Syntax
Options Explained
Reset Sequences
Cascade Truncate
Truncating Multiple Tables
TRUNCATE vs DELETE
Performance Comparison
Use Case Examples
Practical Examples
Clearing Test Data
Data Reset Script
Periodic Log Cleanup
Trigger Behavior
Row-Level Triggers Not Fired by Default
Handling Foreign Key Constraints
When Foreign Key References Exist
Important Notes
- Permission required: Requires TRUNCATE privilege or owner rights on the table
- Locking: TRUNCATE acquires ACCESS EXCLUSIVE lock
- MVCC: TRUNCATE is MVCC-safe
- Partitioned tables: Can TRUNCATE individual partitions
- Replication: In logical replication, TRUNCATE is replicated