]> begriffs open source - ai-pg/blob - full-docs/txt/plperl-triggers.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / plperl-triggers.txt
1
2 43.6. PL/Perl Triggers #
3
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
8    are:
9
10    $_TD->{new}{foo}
11           NEW value of column foo
12
13    $_TD->{old}{foo}
14           OLD value of column foo
15
16    $_TD->{name}
17           Name of the trigger being called
18
19    $_TD->{event}
20           Trigger event: INSERT, UPDATE, DELETE, TRUNCATE, or UNKNOWN
21
22    $_TD->{when}
23           When the trigger was called: BEFORE, AFTER, INSTEAD OF, or
24           UNKNOWN
25
26    $_TD->{level}
27           The trigger level: ROW, STATEMENT, or UNKNOWN
28
29    $_TD->{relid}
30           OID of the table on which the trigger fired
31
32    $_TD->{table_name}
33           Name of the table on which the trigger fired
34
35    $_TD->{relname}
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.
39
40    $_TD->{table_schema}
41           Name of the schema in which the table on which the trigger
42           fired, is
43
44    $_TD->{argc}
45           Number of arguments of the trigger function
46
47    @{$_TD->{args}}
48           Arguments of the trigger function. Does not exist if
49           $_TD->{argc} is 0.
50
51    Row-level triggers can return one of the following:
52
53    return;
54           Execute the operation
55
56    "SKIP"
57           Don't execute the operation
58
59    "MODIFY"
60           Indicates that the NEW row was modified by the trigger function
61
62    Here is an example of a trigger function, illustrating some of the
63    above:
64 CREATE TABLE test (
65     i int,
66     v varchar
67 );
68
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
75     } else {
76         return;           # execute INSERT/UPDATE command
77     }
78 $$ LANGUAGE plperl;
79
80 CREATE TRIGGER test_valid_id_trig
81     BEFORE INSERT OR UPDATE ON test
82     FOR EACH ROW EXECUTE FUNCTION valid_id();