pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alban Hertroys <alban@magproductions.nl>
To: Heather Johnson <hjohnson@nypost.com>
Cc: pgsql-general@postgresql.org
Subject: Re: help with plpgsql function called by trigger
Date: Thu, 17 Mar 2005 10:51:53 +0100
Message-ID: <42395339.1080708@magproductions.nl> (raw)
In-Reply-To: <42388A58.6020604@nypost.com>
References: <421B8D2D.6050800@nypost.com>
	<20050222200915.GA71591@winnie.fuhr.org>
	<421B9674.4060807@nypost.com>
	<1109105459.30529.119.camel@state.g2switchworks.com>
	<421B9E75.9050304@nypost.com>
	<42388A58.6020604@nypost.com>

Heather Johnson wrote:
> Hello--
> 
> I need to make sure that every time a row is inserted into a table 
> called "users" rows are automatically inserted into two other tables: 
> "join_bd" and "behavior_demographics". The inserts on join_bd and 
> behavior_demographics need to create rows that are keyed to the users 
> table with an integer id (called "users_id"). The join_bd row that's 
> created also needs to contain a key for a record in 
> behavior_demographics (bd_id). Here's what I did to try and accomplish 
> this:
> 
> CREATE FUNCTION insert_bd_join_bd (integer) RETURNS opaque AS '
> 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';

Wouldn't it be nicer to put the second part of that SP in a trigger on 
behavioral_demographics? That can only work if inserting into users is 
the only way to insert records into behavioral_demographics, of course.

I don't know the names of your columns in behavioral_demographics, but 
it would look something like this:

CREATE FUNCTION insert_bd () RETURNS trigger AS '
begin
     INSERT INTO behavioral_demographics (users_id)
     VALUES (NEW.users_id);	-- fires trigger on bd
end;
' LANGUAGE 'plpgsql';

CREATE FUNCTION insert_join_bd () RETURNS trigger AS '
begin
     INSERT INTO join_bd (bd_id, users_id)
     VALUES (NEW.bd_id, NEW.users_id);
end;
' LANGUAGE 'plpgsql';

CREATE TRIGGER insert_bd_on_users
AFTER INSERT ON users
FOR EACH ROW EXECUTE PROCEDURE insert_bd();

CREATE TRIGGER insert_join_bd_on_users
AFTER INSERT ON behavioral_demographics
FOR EACH ROW EXECUTE PROCEDURE insert_join_bd();


Considering these SP's are basically simple SQL statements, the triggers 
could also be implemented as a set of query rewrite rules (see CREATE 
RULE). That's usually more efficient, but I don't have a lot of 
experience with those...

Regards,

-- 
Alban Hertroys
MAG Productions



view thread (4+ messages)  latest in thread

Message-ID: <42395339.1080708@magproductions.nl>
Permalink:  ../42395339.1080708@magproductions.nl/
Also on:    postgresql.org/message-id/42395339.1080708@magproductions.nl

 · 

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-admin@postgresql.org
  Cc: alban@magproductions.nl, hjohnson@nypost.com, pgsql-general@postgresql.org
  Subject: Re: help with plpgsql function called by trigger
  In-Reply-To: <42395339.1080708@magproductions.nl>
  References: <421B8D2D.6050800@nypost.com> <20050222200915.GA71591@winnie.fuhr.org> <421B9674.4060807@nypost.com> <1109105459.30529.119.camel@state.g2switchworks.com> <421B9E75.9050304@nypost.com> <42388A58.6020604@nypost.com> <42395339.1080708@magproductions.nl>

* 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