PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Tutorial / PostgreSQL TRUNCATE TABLE

PostgreSQL TRUNCATE TABLE

This tutorial shows you how to use PostgreSQL TRUNCATE TABLE statement to remove all data from large tables quickly.

To remove all data from a table, you use DELETE statement. However, for a large table, it is more efficient to use  TRUNCATE TABLE statement.

The  TRUNCATE TABLE statement removes all rows from a table without scanning the table. This is the reason why it is faster than the  DELETE statement.  In addition, it reclaims the storage right away so you don’t have to perform a subsequent  VACUMM operation, which is useful in case of large tables.

Remove all data from one table

The simplest form of the  TRUNCATE TABLE statement is as follows:

1
TRUNCATE TABLE table_name;

For example, to remove all rows from the invoice table, you use the following statement:

1
TRUNCATE TABLE invoice;

PostgreSQL  TRUNCATE TABLE statement allows you to not only remove all data from a table but also reset associated sequence generator by specifying  RESET IDENTITY option as follows:

1
TRUNCATE TABLE table_name RESET IDENTITY;

For example, to remove all rows from the invoice table and reset sequence associated with the  invoice_no column, you use the following statement:

1
TRUNCATE TABLE invoice RESET IDENTITY;

By default, the  TRUNCATE TABLE statement does not reset the associated sequence. You can use  CONTINUE IDENTITY option explicitly in the statement to retain the sequence. However, this is not necessary.

Remove all data from multiple tables

To remove all data from multiple tables at once, you separate each table by a comma (,) as follows:

1
TRUNCATE TABLE table_name1, table_name2, …

For example, to remove all data from invoice and customer tables, you use the following statement:

1
TRUNCATE TABLE invoice, customer;

Remove all data from table that has foreign key references

In practice, the table you want to truncate often has the foreign key references from other tables which are not listed in the  TRUNCATE TABLE statement. By default, the  TRUNCATE TABLE statement does not remove any data from the table that has foreign key references.

To remove data from the main table and all tables that have foreign key references to the main table, you use CASCADE option as follows:

1
TRUNCATE TABLE table_name CASCADE;

For example, to remove all data from the invoice table and cascade to any tables that reference to invoice table via foreign key constraints, you use the following statement:

1
TRUNCATE TABLE invoice CASCADE;

You should be careful when you use the CASCADE option, or else you might potentially delete data from the tables that you did not intend to.

PostgreSQL TRUNCATE TABLE 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 that table.

To fire the trigger when the  TRUNCATE TABLE command applied to a table, you must define  BEFORE TRUNCATE and/or  AFTER TRUNCATE triggers for that table.

PostgreSQL TRUNCATE TABLE and transaction

The  TRUNCATE TABLE is transaction-safe, which means if you place it within the transaction statements such as BEGIN … ROLLBACK, the truncation operation will be rolled back safely.

In this tutorial, we have shown you how to remove all data from large tables quickly and efficiently by using the  TRUNCATE TABLE statement. In addition, we introduced you to some other options that the statement provides.

Previous Tutorial: PostgreSQL DROP TABLE
Next Tutorial: PostgreSQL Copy Table: A Step-by-Step Guide with Practical Examples

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.