©
This document usesPHP Chinese website manualRelease
Trigger procedures can be written in PL/Tcl.PostgreSQLrequires that a procedure that is to be called as a trigger must be declared as a function with no arguments and a return type oftrigger.
The information from the trigger manager is passed to the procedure body in the following variables:
The name of the trigger from theCREATE TRIGGERstatement.
The object ID of the table that caused the trigger procedure to be invoked.
The name of the table that caused the trigger procedure to be invoked.
The schema of the table that caused the trigger procedure to be invoked.
A Tcl list of the table column names, prefixed with an empty list element. So looking up a column name in the list withTcl'slsearch
command returns the element's number starting with 1 for the first column, the same way the columns are customarily numbered inPostgreSQL. (Empty list elements also appear in the positions of columns that have been dropped, so that the attribute numbering is correct for columns to their right.)
The stringBEFOREorAFTERdepending on the type of trigger event.
The stringROWorSTATEMENTdepending on the type of trigger event.
The stringINSERT,UPDATE,DELETE, orTRUNCATEdepending on the type of trigger event.
An associative array containing the values of the new table row forINSERTorUPDATEactions, or empty forDELETE. The array is indexed by column name. Columns that are null will not appear in the array. This is not set for statement-level triggers.
An associative array containing the values of the old table row forUPDATEorDELETEactions, or empty forINSERT. The array is indexed by column name. Columns that are null will not appear in the array. This is not set for statement-level triggers.
A Tcl list of the arguments to the procedure as given in theCREATE TRIGGERstatement. These arguments are also accessible as$1...$nin the procedure body.
The return value from a trigger procedure can be one of the stringsOKorSKIP, or a list as returned by thearray getTcl command. If the return value isOK, the operation (INSERT/UPDATE/DELETE) that fired the trigger will proceed normally.SKIPtells the trigger manager to silently suppress the operation for this row. If a list is returned, it tells PL/Tcl to return a modified row to the trigger manager that will be inserted instead of the one given in$NEW. (This works forINSERTandUPDATEonly.) Needless to say that all this is only meaningful when the trigger isBEFOREandFOR EACH ROW; otherwise the return value is ignored.
Here's a little example trigger procedure that forces an integer value in a table to keep track of the number of updates that are performed on the row. For new rows inserted, the value is initialized to 0 and then incremented on every update operation.
CREATE FUNCTION trigfunc_modcount() RETURNS trigger AS $$ switch $TG_op { INSERT { set NEW($1) 0 } UPDATE { set NEW($1) $OLD($1) incr NEW($1) } default { return OK } } return [array get NEW] $$ LANGUAGE pltcl; CREATE TABLE mytab (num integer, description text, modcnt integer); CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab FOR EACH ROW EXECUTE PROCEDURE trigfunc_modcount('modcnt');
Notice that the trigger procedure itself does not know the column name; that's supplied from the trigger arguments. This lets the trigger procedure be reused with different tables.