Text Size: Normal / Large

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.


User Comments


Frankie Chow <frankie AT samba.hk>
31 Oct 2004 9:18:23

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

Add Comment

Please use this form to add your own comments regarding your experience with particular features of PostgreSQL, clarifications of the documentation, or hints for other users. Please note, this is not a support forum, and your IP address will be logged. If you have a question or need help, please see the faq, try a mailing list, or join us on IRC. Note that submissions containing URLs or other keywords commonly found in 'spam' comments may be silently discarded. Please contact the webmaster if you think this is happening to you in error.

In order to submit a comment, you must have a community account.

* Comment
 

* denotes required field

Privacy Policy | Project hosted by hub.org | Designed by tinysofa
Copyright © 1996 – 2007 PostgreSQL Global Development Group