Re: Problem creating trigger-function with arguments (8.0rc4) - Mailing list pgsql-general

From Michael Fuhr
Subject Re: Problem creating trigger-function with arguments (8.0rc4)
Date
Msg-id 20050107225744.GA92005@winnie.fuhr.org
Whole thread Raw
In response to Re: Problem creating trigger-function with arguments (8.0rc4)  (David Fetter <david@fetter.org>)
Responses Re: Problem creating trigger-function with arguments (8.0rc4)
List pgsql-general
On Fri, Jan 07, 2005 at 02:00:07PM -0800, David Fetter wrote:
> On Fri, Jan 07, 2005 at 03:52:15PM -0500, Tom Lane wrote:
> >
> > The CREATE TRIGGER parameter comes to the trigger function via
> > TGARGS, not as a regular parameter.
>
> Um, so how would one write a trigger that takes arguments?

By accessing TG_ARGV (not TGARGS) in the function.  See the "Trigger
Procedures" documentation.

CREATE TABLE foo (x INTEGER);

CREATE FUNCTION trigfunc() RETURNS TRIGGER AS $$
BEGIN
    RAISE INFO 'trigger argument = %', TG_ARGV[0];
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER trig_insert BEFORE INSERT ON foo
  FOR EACH ROW EXECUTE PROCEDURE trigfunc('insert argument');

CREATE TRIGGER trig_update BEFORE UPDATE ON foo
  FOR EACH ROW EXECUTE PROCEDURE trigfunc('update argument');


test=> INSERT INTO foo VALUES (123);
INFO:  trigger argument = insert argument
INSERT 0 1

test=> UPDATE foo SET x = 456;
INFO:  trigger argument = update argument
UPDATE 1

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

pgsql-general by date:

Previous
From: Tzahi Fadida
Date:
Subject: returning a setof tuples like a subquery was(RE: ERROR: cache lookup failed for type 0 )
Next
From: Michael Fuhr
Date:
Subject: Re: Problem creating trigger-function with arguments (8.0rc4)