Summary: in this tutorial, you will learn about PostgreSQL triggers, why you use should use the triggers, and when to use them.
A PostgreSQL trigger is a function invoked automatically whenever an event associated with a table occurs. An event could be any of the following: INSERT, UPDATE, DELETE or TRUNCATE.
A trigger is a special user-defined function that binds to a table. To create a new trigger, you must define a trigger function first, and then bind this trigger function to a table. The difference between a trigger and a user-defined function is that a trigger is automatically invoked when an event occurs.
PostgreSQL provides two main types of triggers: row and statement level triggers. The differences between the two are how many times the trigger is invoked and at what time. For example, if you issue an UPDATE
statement that affects 20 rows, the row level trigger will be invoked 20 times, while the statement level trigger will be invoked 1 time.
You can specify whether the trigger is invoked before or after an event. If the trigger is invoked before an event, it can skip the operation for the current row or even change the row being updated or inserted. In case the trigger is invoked after the event, all changes are available to the trigger.
Triggers are useful in case the database is accessed by various applications, and you want to keep the cross-functionality within database that runs automatically whenever the data of the table is modified. For example, if you want to keep history of data without requiring application to have logic to check for every event such as INSERT
or UDPATE
.
You can also use triggers to maintain complex data integrity rules which you cannot implement elsewhere except at the database level. For example, when a new row is added into the customer
table, other rows must be also created in tables of banks and credits.
The main drawback of using trigger is that you must know the trigger exists and understand its logic in order to figure it out the effects when data changes.
Even though PostgreSQL implements SQL
standard, triggers in PostgreSQL has some specific features as follows:
- PostgreSQL fires trigger for the
TRUNCATE
event. - PostgreSQL allows you to define statement-level trigger on views.
- PostgreSQL requires you to define a user-defined function as the action of the trigger, while the
SQL
standard allows you to use any number ofSQL
commands.
In this tutorial, you have learned about PostgreSQL trigger, why you should use it, and when you can use it.