2 43.6. PL/Perl Triggers #
4 PL/Perl can be used to write trigger functions. In a trigger function,
5 the hash reference $_TD contains information about the current trigger
6 event. $_TD is a global variable, which gets a separate local value for
7 each invocation of the trigger. The fields of the $_TD hash reference
11 NEW value of column foo
14 OLD value of column foo
17 Name of the trigger being called
20 Trigger event: INSERT, UPDATE, DELETE, TRUNCATE, or UNKNOWN
23 When the trigger was called: BEFORE, AFTER, INSTEAD OF, or
27 The trigger level: ROW, STATEMENT, or UNKNOWN
30 OID of the table on which the trigger fired
33 Name of the table on which the trigger fired
36 Name of the table on which the trigger fired. This has been
37 deprecated, and could be removed in a future release. Please use
38 $_TD->{table_name} instead.
41 Name of the schema in which the table on which the trigger
45 Number of arguments of the trigger function
48 Arguments of the trigger function. Does not exist if
51 Row-level triggers can return one of the following:
57 Don't execute the operation
60 Indicates that the NEW row was modified by the trigger function
62 Here is an example of a trigger function, illustrating some of the
69 CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
70 if (($_TD->{new}{i} >= 100) || ($_TD->{new}{i} <= 0)) {
71 return "SKIP"; # skip INSERT/UPDATE command
72 } elsif ($_TD->{new}{v} ne "immortal") {
73 $_TD->{new}{v} .= "(modified by trigger)";
74 return "MODIFY"; # modify row and execute INSERT/UPDATE command
76 return; # execute INSERT/UPDATE command
80 CREATE TRIGGER test_valid_id_trig
81 BEFORE INSERT OR UPDATE ON test
82 FOR EACH ROW EXECUTE FUNCTION valid_id();