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

PostgreSQL - TRUNCATE TABLE

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:

  1. Basic Usage:

    To remove all rows from a table named "my_table", you would use:

    TRUNCATE TABLE my_table;
    
  2. Truncating Multiple Tables:

    You can truncate multiple tables in a single command:

    TRUNCATE TABLE table1, table2, table3;
    
  3. 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;
    
  4. 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;
    
  5. Performance:

    TRUNCATE is faster than DELETE because:

    • It doesn't generate individual row delete statements, which reduces I/O operations.
    • It doesn't log individual row deletions, which makes the operation faster and uses less log space.
  6. MVCC and Locking:

    • While 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.
    • As 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.
  7. Constraints and Triggers:

    • TRUNCATE respects triggers. If a table has an AFTER TRUNCATE trigger, it will be invoked.
    • As mentioned, if there are foreign key constraints, using the CASCADE option can help avoid violations.
  8. 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.

  9. Considerations:

    • Because 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.
    • Always ensure backups are in place before performing operations that can lead to significant data modifications or 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.

  1. 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;
    
  2. How to use TRUNCATE TABLE in PostgreSQL: TRUNCATE TABLE removes all rows from a table, providing a faster alternative to DELETE for large tables.

  3. PostgreSQL TRUNCATE CASCADE: Use CASCADE to automatically truncate dependent tables:

    TRUNCATE TABLE parent_table CASCADE;
    
  4. Reset identity column after TRUNCATE TABLE in PostgreSQL: After truncating a table, reset the identity column using SETVAL:

    TRUNCATE TABLE example_table RESTART IDENTITY;
    
  5. 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';
    
  6. PostgreSQL TRUNCATE TABLE and foreign keys: TRUNCATE TABLE automatically truncates dependent tables when used with CASCADE:

    TRUNCATE TABLE parent_table CASCADE;
    
  7. 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.