pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: David Fetter <david@fetter.org>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Florian G. Pflug <fgp@phlo.org>
Cc: pgsql-general@postgresql.org
Subject: Re: Problem creating trigger-function with arguments (8.0rc4)
Date: Fri, 7 Jan 2005 15:57:44 -0700
Message-ID: <20050107225744.GA92005@winnie.fuhr.org> (raw)
In-Reply-To: <20050107220007.GC16967@fetter.org>
References: <41DEEA4C.8090903@phlo.org>
	<29386.1105131135@sss.pgh.pa.us>
	<20050107220007.GC16967@fetter.org>

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/



view thread (7+ messages)  latest in thread

Message-ID: <20050107225744.GA92005@winnie.fuhr.org>
Permalink:  ../20050107225744.GA92005@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20050107225744.GA92005@winnie.fuhr.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: mike@fuhr.org, david@fetter.org, tgl@sss.pgh.pa.us, fgp@phlo.org
  Subject: Re: Problem creating trigger-function with arguments (8.0rc4)
  In-Reply-To: <20050107225744.GA92005@winnie.fuhr.org>
  References: <41DEEA4C.8090903@phlo.org> <29386.1105131135@sss.pgh.pa.us> <20050107220007.GC16967@fetter.org> <20050107225744.GA92005@winnie.fuhr.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox