pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Triggers and SQL functions
3+ messages / 2 participants
[nested] [flat]

* Triggers and SQL functions
@ 1998-05-29 00:36  Pierre Padovani <pierre@desertmoon.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Pierre Padovani @ 1998-05-29 00:36 UTC (permalink / raw)
  To: pgsql-sql

Can or will postgresql support trigger calls to sql defined functions?

When attempting this I get....

create function effect_of_add_prod(bpchar) returns boolean
 as
 'update developer
  set developer_tot_prod = developer_tot_prod + 1
  where exists (select developer_id
  from developer
  where developer_id = $1);
  select true as ignore_this;'
 language 'sql';
CREATE

 create trigger add_product after insert
 on product for each row
 execute procedure effect_of_add_prod(product_developer);
ERROR:  CreateTrigger: function effect_of_add_prod () does not exist

Searching through documentation reveals only mention of calls to C
functions...is there no way to call SQL functions?

-=pierre





^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Triggers and SQL functions
@ 1998-10-16 22:23  Philippe Rochat (RSR: 318 17 93) <prochat@lbdsun.epfl.ch>
  1 sibling, 0 replies; 3+ messages in thread

From: Philippe Rochat (RSR: 318 17 93) @ 1998-10-16 22:23 UTC (permalink / raw)
  To: pierre@desertmoon.com; pgsql-general@postgresql.org; pgsql-sql

pierre@desertmoon.com wrote:
> 
> >
> > I have exactly the same problem as you mentioned in a mail
> > sent to postgres mailing lists about creating trigger using
> > sql function ...
> >
> > Did you get the solution to you problem ?
> >
> > Ph.R.
> >
> >
> 
> I actually found the solution in the docs. The solution is to understand
> the difference between triggers and rules. Triggers are used primarily
> to call C/C++ library functions that you can create and add onto the
> postgresql system.
> 
> Rules on the other-hand act just like 'triggers' but only call/usr
> sql to perform their functions.
> 
> So the solution: Use rules to create trigger-like actions that run
> stored sql procedures. There is a man page on 'create rule' that you
> can use to get more info.
> 
> -=pierre

The problem is the following:

CREATE TABLE user (
	id int,
	username char(10),
	passwd char(10)
);
CREATE sequence user_seq start 1 increment 1;
CREATE RULE adduser as
        on insert to user where id isnull
        do update user set id=nextval('user_seq') where id isnull;
-- 
As you can see, I implement kind of autoinc on id.

CREATE TABLE user_realm (
	uid int,
	rlid int,
	adminuser boolean
);

I would like to defaultly add a user in the realm 1.
I tried the following:
CREATE RULE adduservoid as
	on insert to user
	do insert into user_realm values(new.id, 1, false);

But new.id is very probably null !
The problem is that rules is a rewriting system and not function calls
(and so no way to define after/befor).
So I would like to do this auto-insert into user_realm with a trigger
after insert. I tried the following :

-- Makes all user at least belonging to Void ... just in case
CREATE FUNCTION adduservoid () returns boolean
 as 'insert into user_realm 
        select id as uid,1 as rlid,false as adminuser
        from user where not exists (
           select uid from user_realm where user.id=user_realm.uid
        );
     select true as ignore_this;'
 language 'sql';
CREATE TRIGGER autoadduservoid AFTER INSERT OR UPDATE ON user for each
row
EXECUTE PROCEDURE adduservoid();


But this doesnt work for the reason you mentioned.
Another solution would be if the create rule would support many
statements instead of just one, like that:
CREATE RULE adduser as
        on insert to user where id isnull
        do 'update user set id=nextval('user_seq') where id isnull;
		insert into user_realm(new.id,1,false);'

But because I have also a check_primary key on user_realm that ensure
that uid exists in user ... I would still have a problem !

Ph.R.

-- 
_____________________________________________________________________
Philippe Rochat,				EPFL   DI-LBD
Database Laboratory 				CH-1015 LAUSANNE
Swiss Federal Institute of Technology (EPFL) 	tel:++41 21 693 52 53
Beep-EPFL: 181 4644				fax:++41 21 693 51 95
Private: Grammont, 9 1007 LAUSANNE 		tel:++41 21 617 03 05 
mailto:Philippe.Rochat@epfl.ch, http://lbdwww.epfl.ch/~prochat



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Triggers and SQL functions
@ 1998-10-16 22:38  Philippe Rochat (RSR: 318 17 93) <prochat@lbdsun.epfl.ch>
  1 sibling, 0 replies; 3+ messages in thread

From: Philippe Rochat (RSR: 318 17 93) @ 1998-10-16 22:38 UTC (permalink / raw)
  To: pierre@desertmoon.com; pgsql-sql

pierre@desertmoon.com wrote:
> 
> >
> > I have exactly the same problem as you mentioned in a mail
> > sent to postgres mailing lists about creating trigger using
> > sql function ...
> >
> > Did you get the solution to you problem ?
> >
> > Ph.R.
> >
> >
> 
> I actually found the solution in the docs. The solution is to understand
> the difference between triggers and rules. Triggers are used primarily
> to call C/C++ library functions that you can create and add onto the
> postgresql system.
> 
> Rules on the other-hand act just like 'triggers' but only call/usr
> sql to perform their functions.
> 
> So the solution: Use rules to create trigger-like actions that run
> stored sql procedures. There is a man page on 'create rule' that you
> can use to get more info.
> 
> -=pierre
I found out the following trick ... but I m not sure it s clean !

-- Makes all user at least belonging to Void ... just in case
CREATE FUNCTION adduservoid () returns boolean
 as 'insert into user_realm 
        select id as uid,1 as rlid,false as adminuser
        from user where not exists (
           select uid from user_realm where user.id=user_realm.uid
        );
     select true as ignore_this;'
 language 'sql';
CREATE RULE autoadduservoid as
	on insert to user
	do select adduservoid();

Ph.R.

-- 
_____________________________________________________________________
Philippe Rochat,				EPFL   DI-LBD
Database Laboratory 				CH-1015 LAUSANNE
Swiss Federal Institute of Technology (EPFL) 	tel:++41 21 693 52 53
Beep-EPFL: 181 4644				fax:++41 21 693 51 95
Private: Grammont, 9 1007 LAUSANNE 		tel:++41 21 617 03 05 
mailto:Philippe.Rochat@epfl.ch, http://lbdwww.epfl.ch/~prochat



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~1998-10-16 22:38 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1998-05-29 00:36 Triggers and SQL functions Pierre Padovani <pierre@desertmoon.com>
1998-10-16 22:23 ` Philippe Rochat (RSR: 318 17 93) <prochat@lbdsun.epfl.ch>
1998-10-16 22:38 ` Philippe Rochat (RSR: 318 17 93) <prochat@lbdsun.epfl.ch>

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