Firing triggers based on certain Insert conditions

Started by Harpreet Dhaliwalabout 19 years ago4 messagesgeneral
Jump to latest
#1Harpreet Dhaliwal
harpreet.dhaliwal01@gmail.com

Hi
I have a table in which i have a field named 'source'
A trigger is written on this table.
I want this trigger to fire only when after Insert this field 'source'
has value = 'from', otherwise trigger should not be fired at all.
Just wondering if its really possible?

Thanks in advance.
Harpreet

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Harpreet Dhaliwal (#1)
Re: Firing triggers based on certain Insert conditions

"Harpreet Dhaliwal" <harpreet.dhaliwal01@gmail.com> writes:

I want this trigger to fire only when after Insert this field 'source'
has value = 'from', otherwise trigger should not be fired at all.
Just wondering if its really possible?

No, and it seems pretty silly as a feature request. Why don't you just
put the test in the trigger, and have it do nothing when you don't want
it to do anything?

regards, tom lane

#3Brendan Jurd
direvus@gmail.com
In reply to: Harpreet Dhaliwal (#1)
Re: Firing triggers based on certain Insert conditions

On 1/29/07, Harpreet Dhaliwal <harpreet.dhaliwal01@gmail.com> wrote:

Hi
I have a table in which i have a field named 'source'
A trigger is written on this table.
I want this trigger to fire only when after Insert this field 'source'
has value = 'from', otherwise trigger should not be fired at all.
Just wondering if its really possible?

AFAIK you can't prevent the trigger from firing, but you can make the
trigger behave differently based on what's in the fields of the new
row:

CREATE FUNCTION source_insert() RETURNS trigger AS $$
BEGIN
IF NEW.source = 'from' THEN

/* do stuff */

END IF;

RETURN NULL;
END;
$$ LANGUAGE plpgsql VOLATILE;

CREATE TRIGGER post_insert AFTER INSERT ON [your table name] FOR EACH
ROW EXECUTE PROCEDURE source_insert();

Regards,
BJ

#4Harpreet Dhaliwal
harpreet.dhaliwal01@gmail.com
In reply to: Tom Lane (#2)
Re: Firing triggers based on certain Insert conditions

I never said I don't want my trigger to do anything.
My subject line only made it pretty clear that I want to fire my trigger
based on
certain conditions.
I know its kind of a silly feature request but you really can't help it
when you are working with stupid advisors :)
thanks for your reponse anyways.
Harpreet

Show quoted text

On 1/28/07, Tom Lane <tgl@sss.pgh.pa.us> wrote:

"Harpreet Dhaliwal" <harpreet.dhaliwal01@gmail.com> writes:

I want this trigger to fire only when after Insert this field 'source'
has value = 'from', otherwise trigger should not be fired at all.
Just wondering if its really possible?

No, and it seems pretty silly as a feature request. Why don't you just
put the test in the trigger, and have it do nothing when you don't want
it to do anything?

regards, tom lane