This page in other versions: 9.0 / 9.1 / 9.2 / 9.3  |  Development versions: devel / 9.4  |  Unsupported versions: 7.3 / 7.4 / 8.0 / 8.1 / 8.2 / 8.3 / 8.4

40.2. Trigger Functions

When a function is used in a trigger, the dictionary TD contains trigger-related values. The trigger rows are in TD["new"] and/or TD["old"] depending on the trigger event. TD["event"] contains the event as a string (INSERT, UPDATE, DELETE, or UNKNOWN). TD["when"] contains one of BEFORE, AFTER, and UNKNOWN. TD["level"] contains one of ROW, STATEMENT, and UNKNOWN. TD["name"] contains the trigger name, and TD["relid"] contains the OID of the table on which the trigger occurred. If the trigger was called with arguments they are available in TD["args"][0] to TD["args"][(n-1)].

If TD["when"] is BEFORE, you may return None or "OK" from the Python function to indicate the row is unmodified, "SKIP" to abort the event, or "MODIFY" to indicate you've modified the row.

Comments


Oct. 31, 2004, 9:18 a.m.

This is my trigger. I think this infomation will help some people.

-----------------------------------------------------

CREATE FUNCTION semailt() RETURNS trigger AS \'
import sys
sys.path.append(\"/var/lib/postgres/python2.1\")
import simpleemail
of = open ( \"/tmp/trigger\", \"w\")
sys.stdout = of
print \"This is the TD new: \",
print TD[\"new\"]
print \"This is the TD old: \",
print TD[\"old\"]
print \"This is the TD : \",
print TD
print \"This is the TD email : \",
print TD[\"new\"][\"email\"]
TD[\"new\"][\"email\"] = simpleemail.semail(TD[\"new\"][\"email\"]).getsimple()
return \"MODIFY\"
\' LANGUAGE plpythonu;

CREATE TRIGGER semailt BEFORE INSERT OR UPDATE ON pytrigger
FOR EACH ROW EXECUTE PROCEDURE semailt();

------------------------------------------------------
--- This is my pytrigger table.

maillist=# \\d pytrigger
Table \"public.pytrigger\"
Column | Type | Modifiers
--------+---------+-----------------------------------------------------------
id | integer | not null default nextval(\'public.pytrigger_id_seq\'::text)
email | text |
Triggers:
semailt BEFORE INSERT OR UPDATE ON pytrigger FOR EACH ROW EXECUTE PROCEDURE semailt()

maillist=#

--- when you insert some data, you can find infomation
--- in /tmp/trigger

--------------------------------------------------------

wandell:~# cat /tmp/trigger
This is the TD new: {\'email\': \'Frankie@Samba.HK\', \'id\': 7}
This is the TD old: None
This is the TD : {\'event\': \'INSERT\', \'when\': \'BEFORE\', \'args\': None, \'new\': {\'email\': \'Frankie@Samba.HK\', \'id\': 7}, \'level\': \'ROW\', \'name\': \'semailt\', \'old\': None, \'relid\': \'378878\'}
This is the TD email : Frankie@Samba.HK
wandell:~#

-----------------------------------------------------

Thank you for pypythonu language

Frankie

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group