Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm trying to check what event called my trigger, like INSERT, UPDATE or DELETE.

Oracle triggers can check this in a simple 'IF' statement:

IF INSERTING, IF UPDATING or even IF DELETING

Is there a similar statement on PostgreSQL?

Usually I just create a function for INSERT, an other one for UPDATE and one more for DELETE, but it's a waste of code if there's some way to create just one function.

Can someone help?

share|improve this question
1  
"Can someone help?" - the manual contains this information. –  a_horse_with_no_name Oct 27 '12 at 8:02
    
I don't think it's waste of code. Generally and even more in the case of complex trigger behaviour I feel safer to keep my triggers and corresponding procedures separated from each other. It makes debugging cleaner, for example. –  dezso Oct 27 '12 at 9:15
add comment

1 Answer

up vote 7 down vote accepted

Trigger functions can check the value of the TG_OP variable to determine the event which caused the trigger to fire. There are a number of other special variables as well listed in the documentation.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.