2 42.6. Trigger Functions in PL/Tcl #
4 Trigger functions can be written in PL/Tcl. PostgreSQL requires that a
5 function that is to be called as a trigger must be declared as a
6 function with no arguments and a return type of trigger.
8 The information from the trigger manager is passed to the function body
9 in the following variables:
12 The name of the trigger from the CREATE TRIGGER statement.
15 The object ID of the table that caused the trigger function to
19 The name of the table that caused the trigger function to be
23 The schema of the table that caused the trigger function to be
27 A Tcl list of the table column names, prefixed with an empty
28 list element. So looking up a column name in the list with Tcl's
29 lsearch command returns the element's number starting with 1 for
30 the first column, the same way the columns are customarily
31 numbered in PostgreSQL. (Empty list elements also appear in the
32 positions of columns that have been dropped, so that the
33 attribute numbering is correct for columns to their right.)
36 The string BEFORE, AFTER, or INSTEAD OF, depending on the type
40 The string ROW or STATEMENT depending on the type of trigger
44 The string INSERT, UPDATE, DELETE, or TRUNCATE depending on the
45 type of trigger event.
48 An associative array containing the values of the new table row
49 for INSERT or UPDATE actions, or empty for DELETE. The array is
50 indexed by column name. Columns that are null will not appear in
51 the array. This is not set for statement-level triggers.
54 An associative array containing the values of the old table row
55 for UPDATE or DELETE actions, or empty for INSERT. The array is
56 indexed by column name. Columns that are null will not appear in
57 the array. This is not set for statement-level triggers.
60 A Tcl list of the arguments to the function as given in the
61 CREATE TRIGGER statement. These arguments are also accessible as
62 $1 ... $n in the function body.
64 The return value from a trigger function can be one of the strings OK
65 or SKIP, or a list of column name/value pairs. If the return value is
66 OK, the operation (INSERT/UPDATE/DELETE) that fired the trigger will
67 proceed normally. SKIP tells the trigger manager to silently suppress
68 the operation for this row. If a list is returned, it tells PL/Tcl to
69 return a modified row to the trigger manager; the contents of the
70 modified row are specified by the column names and values in the list.
71 Any columns not mentioned in the list are set to null. Returning a
72 modified row is only meaningful for row-level BEFORE INSERT or UPDATE
73 triggers, for which the modified row will be inserted instead of the
74 one given in $NEW; or for row-level INSTEAD OF INSERT or UPDATE
75 triggers where the returned row is used as the source data for INSERT
76 RETURNING or UPDATE RETURNING clauses. In row-level BEFORE DELETE or
77 INSTEAD OF DELETE triggers, returning a modified row has the same
78 effect as returning OK, that is the operation proceeds. The trigger
79 return value is ignored for all other types of triggers.
83 The result list can be made from an array representation of the
84 modified tuple with the array get Tcl command.
86 Here's a little example trigger function that forces an integer value
87 in a table to keep track of the number of updates that are performed on
88 the row. For new rows inserted, the value is initialized to 0 and then
89 incremented on every update operation.
90 CREATE FUNCTION trigfunc_modcount() RETURNS trigger AS $$
103 return [array get NEW]
106 CREATE TABLE mytab (num integer, description text, modcnt integer);
108 CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
109 FOR EACH ROW EXECUTE FUNCTION trigfunc_modcount('modcnt');
111 Notice that the trigger function itself does not know the column name;
112 that's supplied from the trigger arguments. This lets the trigger
113 function be reused with different tables.