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/