Skip to main content
Version: 2.0.0 (latest)

3.5. Triggers

Triggers are objects associated with a table that are used to execute operations stored in special functions when some operation (or event) happens on a single row or over the entire table. The trigger can be activated before or after the following events: INSERT, DELETE, UPDATE, or TRUNCATE. Besides ordinary triggers, there is a special kind known as a constraint trigger, whose firing time can be adjusted.

ch3.trigger-dialog.attributes12345678910

Attributes
#FieldGroup boxDescription
1Execution

The moment when the trigger fires in relation to the event: BEFORE, AFTER, or INSTEAD OF (the last one is meant for triggers on views and requires For each row). It is disabled for constraint triggers, which always fire AFTER.

2For each row

Executes the trigger function once for each row affected by the event (FOR EACH ROW). When unchecked, it is executed once per statement (FOR EACH STATEMENT). It is always checked, and disabled, for constraint triggers.

3Constraint trigger

Turns the trigger into a constraint trigger. Constraint triggers always fire AFTER the event and for each row, so the fields Execution and For each row are disabled; the fields of the tab Constraint are enabled instead.

4Function

The function executed when the trigger fires. It must take no arguments and return trigger; the values defined in the tab Arguments are passed to it through its special variables.

5INSERTEvents

Fires the trigger on INSERT commands.

6DELETEEvents

Fires the trigger on DELETE commands.

7OLDTransition tables names

Name of the transition table that exposes the old versions of the affected rows (REFERENCING OLD TABLE AS). Enabled only for AFTER triggers with a single event, either UPDATE or DELETE.

8UPDATEEvents

Fires the trigger on UPDATE commands.

9TRUNCATEEvents

Fires the trigger on TRUNCATE commands.

10NEWTransition tables names

Name of the transition table that exposes the new versions of the affected rows (REFERENCING NEW TABLE AS). Enabled only for AFTER triggers with a single event, either UPDATE or INSERT.

ch3.trigger-dialog.arguments12

Arguments
#FieldDescription
1Argument

A literal argument passed to the trigger function at runtime. These arguments must be handled inside the function.

2Arguments grid

Lists the arguments to be passed to the trigger function. Fill the field above and use the add button to include an item.

ch3.trigger-dialog.columns12

Columns
#FieldDescription
1Column

The columns of the table that cause the trigger to fire in case of an UPDATE on them. This tab is enabled only when UPDATE is checked and the trigger belongs to a table (not to a view). A trigger configured with INSTEAD OF can't be used with a list of columns.

2Columns grid

Lists the columns whose update fires the trigger.

ch3.trigger-dialog.constraint1234

Constraint
#FieldDescription
1Deferrable

Indicates whether the constraint trigger can be deferred. Constraint triggers work like regular triggers, but their firing time can be adjusted with the command SET CONSTRAINTS. The deferral mode is configurable only when this option is checked.

2Deferral

Specifies when a deferrable constraint trigger is checked: INITIALLY IMMEDIATE (at the end of each statement) or INITIALLY DEFERRED (at the end of the transaction).

3Reference table

Table referenced by the constraint trigger (FROM clause). It is enabled only when the option Constraint trigger is checked.

4Condition

A boolean expression that determines whether the trigger function is executed (WHEN clause).

References​

Trigger DDL
https://www.postgresql.org/docs/current/static/sql-createtrigger.html