Re: Firing triggers based on certain Insert conditions

From: "Brendan Jurd" <direvus(at)gmail(dot)com>
To: "Harpreet Dhaliwal" <harpreet(dot)dhaliwal01(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Firing triggers based on certain Insert conditions
Date: 2007-01-28 18:24:37
Message-ID: 37ed240d0701281024s1308f2c3u994a34aefe9ccdac@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 1/29/07, Harpreet Dhaliwal <harpreet(dot)dhaliwal01(at)gmail(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Harpreet Dhaliwal 2007-01-28 18:29:28 Re: Firing triggers based on certain Insert conditions
Previous Message Bill Moran 2007-01-28 18:21:09 Re: Limit on number of users in postgresql?