PostgreSQL BEFORE TRUNCATE Trigger
Summary: in this tutorial, you will learn how to define a PostgreSQL BEFORE TRUNCATE
trigger that fires before a TRUNCATE
event occurs on a table.
Introduction to the PostgreSQL BEFORE TRUNCATE trigger
A TRUNCATE TABLE
statement removes all from a table without creating any logs, making it faster than a DELETE
operation.
PostgreSQL allows you to create a trigger that fires before a TRUNCATE
event occurs.
A BEFORE TRUNCATE
trigger is a statement-level trigger because the TRUNCATE
statement deletes all the rows from the table, not individual rows.
Although the TRUNCATE
operation deletes rows from a table, it does not activate the DELETE
trigger including BEFORE
and AFTER DELETE
triggers.
Here’s the step for creating a BEFORE TRUNCATE
trigger:
First, define a user-defined function that will execute before the TRUNCATE
event:
The function returns NULL
indicating that the trigger doesn’t return additional output.
Second, create a BEFORE TRUNCATE
trigger and associate the function with it:
Since the BEFORE TRUNCATE
trigger is a statement-level trigger, you need to specify the FOR EACH STATEMENT
clause in the CREATE TRIGGER
statement.
Unlike INSERT
, UPDATE
, or DELETE
, which support BEFORE
and AFTER
trigger types, TRUNCATE
only supports BEFORE
triggers.
PostgreSQL BEFORE TRUNCATE trigger example
We’ll create a BEFORE TRUNCATE
trigger to prevent applications from truncating a table.
First, create a new table called companies
to store company data:
Second, insert some rows into the companies
table:
Output:
Third, define a function that will execute when a TRUNCATE
event occurs:
Fourth, create a trigger that fires before a TRUNCATE
event occurs:
Fifth, attempt to truncate the companies
table:
Output:
The output indicates that the BEFORE TRUNCATE
trigger fires, raising an exception that aborts the TRUNCATE
operation.
Summary
- A
BEFORE TRUNCATE
trigger is a statement-level trigger. - Create a
BEFORE TRUNCATE
trigger to fire before aTRUNCATE
event.