Re: Problem creating trigger-function with arguments (8.0rc4)

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: David Fetter <david(at)fetter(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, "Florian G(dot) Pflug" <fgp(at)phlo(dot)org>, pgsql-general(at)postgresql(dot)org
Subject: Re: Problem creating trigger-function with arguments (8.0rc4)
Date: 2005-01-07 22:57:44
Message-ID: 20050107225744.GA92005@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-01-07 23:08:38 Re: Problem creating trigger-function with arguments (8.0rc4)
Previous Message Tzahi Fadida 2005-01-07 22:56:52 returning a setof tuples like a subquery was(RE: ERROR: cache lookup failed for type 0 )