PostgreSQL INSTEAD OF Triggers
Summary: in this tutorial, you will learn about PostgreSQL INSTEAD OF
a trigger to insert, update, or delete data of base tables through a view.
Introduction to PostgreSQL INSTEAD OF triggers
In PostgreSQL, INSTEAD OF
triggers are a special type of triggers that intercept insert, update, and delete operations on views.
It means that when you execute an INSERT
, UPDATE
, or DELETE
statement on a view, PostgreSQL does not directly execute the statement. Instead, it executes the statements defined in the INSTEAD OF
trigger.
To create an INSTEAD OF
trigger, you follow these steps:
First, define a function that will execute when a trigger is fired:
Inside the function, you can customize the behavior for each operation including INSERT
, UPDATE
, and DELETE
.
Second, create an INSTEAD OF
trigger and bind the function to it:
PostgreSQL INSTEAD OF trigger example
Let’s take an example of creating an INSTEAD OF
trigger.
1) Setting up a view with an INSTEAD OF trigger
First, create two tables employees
and salaries
:
Next, insert rows into the employees
and salaries
tables:
Then, create a view based on the employees
and salaries
tables:
After that, create a function that will execute when the INSTEAD
OF
trigger associated with the view activates:
If you execute an insert against the employee_salaries
view, the INSTEAD OF
trigger will insert a new row into the employees table first, then insert a new row into the salaries table.
When you update an employee’s salary by id, the INSTEAD OF
trigger will update the data in the salaries
table.
If you delete a row from the employee_salaries
view, the INSTEAD OF
trigger will delete a row from the employees
table. The DELETE CASCADE
will automatically delete a corresponding row from the salaries table.
Finally, create an INSTEAD OF
trigger that will be fired for the INSERT
, UPDATE
, or DELETE
on the employee_salaries
view:
1) Inserting data into tables via the view
First, insert a new employee with a salary via the view:
PostgreSQL does not execute this statement. Instead, it executes the statement defined in the INSTEAD
OF
trigger. More specifically, it executes two statements:
1) Insert a new row into the employees
table and get the employee id:
2) Insert a new row into the salaries table using the employee id, salary, and effective date:
Second, verify the inserts by retrieving data from the employees
and salaries
tables:
Output:
Output:
2) Updating data into tables via the view
First, update the salary of the employee id 3 via the employee_salaries
view:
Second, retrieve data from the salaries
table:
Output:
3) Deleting data via views
First, delete the employee with id 3 via the employee_salaries
view:
Second, retrieve data from the employees
table:
Output:
Because of the DELETE
CASCADE
, PostgreSQL also deletes the corresponding row in the salaries
table:
Output:
Summary
- Use the
INSTEAD OF
trigger to customize the behavior ofINSERT
,UPDATE
, andDELETE
operations on a database view.