CREATE TRIGGER creates a trigger. The trigger will be associated with a specified table or view, and will execute a specified function when certain events occur.
1 2 3 4 5 6 7 | CREATE [ CONSTRAINT ] TRIGGER trigger_name { BEFORE | AFTER | INSTEAD OF } { event [ OR ... ] } ON table_name [ FROM referenced_table_name ] { NOT DEFERRABLE | [ DEFERRABLE ] { INITIALLY IMMEDIATE | INITIALLY DEFERRED } } [ FOR [ EACH ] { ROW | STATEMENT } ] [ WHEN ( condition ) ] EXECUTE PROCEDURE function_name ( arguments ); |
Events include:
1 2 3 4 | INSERT UPDATE [ OF column_name [, ... ] ] DELETE TRUNCATE |
(Optional) Creates a constraint trigger, that is, a trigger is used as a constraint. Such a trigger is similar to a regular trigger except that the timing of the trigger firing can be adjusted using SET CONSTRAINTS. Constraint triggers must be AFTER ROW triggers.
Specifies the name of a new trigger. The name cannot be schema-qualified because the trigger inherits the schema of its table. In addition, triggers on the same table cannot be named the same. For a constraint trigger, this is also the name to use when you modify the trigger's behavior using SET CONSTRAINTS.
Value range: a string that complies with the identifier naming convention. A value can contain a maximum of 63 characters.
Specifies that a trigger function is called before the trigger event.
Specifies that a trigger function is called after the trigger event. A constraint trigger can only be specified as AFTER.
Specifies that a trigger function directly replaces the trigger event.
Specifies the event that will fire a trigger. Values are INSERT, UPDATE, DELETE, and TRUNCATE. You can also specify multiple trigger events through OR.
For UPDATE events, use the following syntax to specify a list of columns:
1 | UPDATE OF column_name1 [, column_name2 ... ] |
The trigger will only fire if at least one of the listed columns is mentioned as a target of the UPDATE statement. INSTEAD OF UPDATE events do not support lists of columns.
Specifies the name of the table where a trigger needs to be created.
Value range: name of an existing table in the database
Specifies the name of another table referenced by a constraint. This parameter can be specified only for constraint triggers. It does not support foreign key constraints and is not recommended for general use.
Value range: name of an existing table in the database
Controls whether a constraint can be deferred. The two parameters determine the timing for firing a constraint trigger, and can be specified only for constraint triggers.
For details, see CREATE TABLE.
If a constraint is deferrable, the two clauses specify the default time to check the constraint, and can be specified only for constraint triggers.
For details, see CREATE TABLE.
Specifies the frequency of firing a trigger.
If this parameter is not specified, the default value FOR EACH STATEMENT will be used. Constraint triggers can only be specified as FOR EACH ROW.
Specifies a Boolean expression that determines whether a trigger function will actually be executed. If WHEN is specified, the function will be called only when condition returns true.
In FOR EACH ROW triggers, the WHEN condition can reference the columns of old or new row values by writing OLD.column_name or NEW.column_name, respectively. In addition, INSERT triggers cannot reference OLD and DELETE triggers cannot reference NEW.
INSTEAD OF triggers do not support WHEN conditions.
WHEN expressions cannot contain subqueries.
For constraint triggers, evaluation of the WHEN condition is not deferred, but occurs immediately after the update operation is performed. If the condition does not return true, the trigger will not be queued for deferred execution.
Specifies a user-defined function, which must be declared as taking no parameters and returning data of the trigger type. This function is executed when a trigger fires.
Specifies an optional, comma-separated list of parameters to be provided to a function when a trigger is executed. Parameters are literal string constants. Simple names and numeric constants can also be included, but they will all be converted to strings. Check descriptions of the implementation language of a trigger function to find out how these parameters are accessed within the function.
The following details trigger types:
Trigger Timing |
Trigger Event |
Row-level |
Statement-level |
---|---|---|---|
BEFORE |
INSERT/UPDATE/DELETE |
Tables |
Tables and views |
TRUNCATE |
Not supported |
Tables |
|
AFTER |
INSERT/UPDATE/DELETE |
Tables |
Tables and views |
TRUNCATE |
Not supported |
Tables |
|
INSTEAD OF |
INSERT/UPDATE/DELETE |
Views |
Not supported |
TRUNCATE |
Not supported |
Not supported |
Variable |
Description |
---|---|
NEW |
New tuple for INSERT/UPDATE operations. This variable is NULL for DELETE operations. |
OLD |
Old tuple for UPDATE/DELETE operations. This variable is NULL for INSERT operations. |
TG_NAME |
Trigger name |
TG_WHEN |
Trigger timing (BEFORE/AFTER/INSTEAD OF) |
TG_LEVEL |
Trigger frequency (ROW/STATEMENT) |
TG_OP |
Trigger event (INSERT/UPDATE/DELETE/TRUNCATE) |
TG_RELID |
OID of the table where a trigger is located |
TG_RELNAME |
Name of the table where a trigger is located. (This variable is now discarded and is replaced by TG_TABLE_NAME.) |
TG_TABLE_NAME |
Name of the table where a trigger is located. |
TG_TABLE_SCHEMA |
Schema information of the table where a trigger is located |
TG_NARGS |
Number of parameters for a trigger function |
TG_ARGV[] |
List of parameters for a trigger function |
Create a source table and a target table.
1 | CREATE TABLE test_trigger_src_tbl(id1 INT, id2 INT, id3 INT); |
1 | CREATE TABLE test_trigger_des_tbl(id1 INT, id2 INT, id3 INT); |
Create the trigger function tri_insert_func().
1 2 3 4 5 6 7 8 | CREATE OR REPLACE FUNCTION tri_insert_func() RETURNS TRIGGER AS $$ DECLARE BEGIN INSERT INTO test_trigger_des_tbl VALUES(NEW.id1, NEW.id2, NEW.id3); RETURN NEW; END $$ LANGUAGE PLPGSQL; |
Create the trigger function tri_update_func().
1 2 3 4 5 6 7 8 | CREATE OR REPLACE FUNCTION tri_update_func() RETURNS TRIGGER AS $$ DECLARE BEGIN UPDATE test_trigger_des_tbl SET id3 = NEW.id3 WHERE id1=OLD.id1; RETURN OLD; END $$ LANGUAGE PLPGSQL; |
Create the trigger function tri_delete_func().
1 2 3 4 5 6 7 8 | CREATE OR REPLACE FUNCTION tri_delete_func() RETURNS TRIGGER AS $$ DECLARE BEGIN DELETE FROM test_trigger_des_tbl WHERE id1=OLD.id1; RETURN OLD; END $$ LANGUAGE PLPGSQL; |
Create an INSERT trigger.
1 2 3 4 | CREATE TRIGGER insert_trigger BEFORE INSERT ON test_trigger_src_tbl FOR EACH ROW EXECUTE PROCEDURE tri_insert_func(); |
Create an UPDATE trigger.
1 2 3 4 | CREATE TRIGGER update_trigger AFTER UPDATE ON test_trigger_src_tbl FOR EACH ROW EXECUTE PROCEDURE tri_update_func(); |
Create a DELETE trigger.
1 2 3 4 | CREATE TRIGGER delete_trigger BEFORE DELETE ON test_trigger_src_tbl FOR EACH ROW EXECUTE PROCEDURE tri_delete_func(); |