Re: help with plpgsql function called by trigger

From: Michael Fuhr <mike(at)fuhr(dot)org>
To: Heather Johnson <hjohnson(at)nypost(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: help with plpgsql function called by trigger
Date: 2005-03-17 03:48:16
Message-ID: 20050317034816.GA13674@winnie.fuhr.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-general

On Wed, Mar 16, 2005 at 02:34:48PM -0500, Heather Johnson wrote:

> CREATE FUNCTION insert_bd_join_bd (integer) RETURNS opaque AS '

What version of PostgreSQL are you using? The "opaque" type has
been deprecated since 7.3; recent versions should use "trigger".
And trigger functions aren't declared with arguments -- if the
function needs arguments, then use TG_ARGV. But I think your code
can use NEW instead of a function argument.

> declare
> r RECORD;
> uid ALIAS FOR $1;
> begin
> INSERT INTO behavioral_demographics (users_id) VALUES (uid);
> SELECT INTO r bdid FROM behavioral_demographics WHERE users_id=uid;
> INSERT INTO join_bd (bd_id, users_id) VALUES (bdid, uid);
> end;
> ' LANGUAGE 'plpgsql';

Based on the rest of your description, I think you're looking for
something like this:

CREATE FUNCTION insert_bd_join_bd() RETURNS trigger AS '
DECLARE
bdid integer;
BEGIN
INSERT INTO behavioral_demographics (users_id) VALUES (NEW.uid);
bdid := currval(''behavioral_demographics_bdid_seq'');
INSERT INTO join_bd (bd_id, users_id) VALUES (bdid, NEW.uid);

RETURN NULL;
END;
' LANGUAGE plpgsql VOLATILE;

See the "Trigger Procedures" section of the PL/pgSQL chapter in the
documentation to learn more about NEW (and OLD, TG_ARGV, etc.), and
see the "Sequence Manipulation Functions" section of the "Functions
and Operators" chapter to learn more about currval().

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

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Christopher Browne 2005-03-17 05:22:43 Re: Scheduling/Automated Jobs in Postgre
Previous Message Tom Lane 2005-03-17 03:24:49 Re: Cannot get postgres started on Fedora core 3

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2005-03-17 05:02:55 Re: Fast major-version upgrade (was: [GENERAL] postgresql 8.0
Previous Message Vern 2005-03-17 02:35:25 Re: 3rd RFD: comp.databases.postgresql (was: comp.databases.postgresql.*)