pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Brook Milligan <brook@trillium.NMSU.Edu>
To: jwieck@debis.com
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] rules help
Date: Tue, 27 Apr 1999 11:20:33 -0600 (MDT)
Message-ID: <199904271720.LAA19445@trillium.nmsu.edu> (raw)
In-Reply-To: <m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de>
References: <m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de>

Thanks for the QUICK response, Jan.  I guess I didn't realize one
could put multiple actions in a rule.

       The strangeness here is, that (for me) it should work without
       the  GROUP  BY  clause.  But then I got multiple entries into
       table 2. Exactly the number of rows in table 1, but they  all
       referenced the correct entry. So with your test data I got 2x
       Sue in Boston, 3x Bill in Chicago and 3x Tom in Boston.

I don't see this problem (NetBSD 1.3.2/i386, postgresql 6.4.2).  See
my script below which does what I really want (with one exception).

       The next problem is, that setting up  another  table  with  2
       text fields and doing a

	   insert into rule_view select * from addr_data;

       doesn't work at all. Seems the rules aren't triggered and the
       data is stored in the view relation.

I'll see if I see that, too.

       And the final problem is that after defining the above rule a
       select from pg_rules crashes the backend. Oh man - think I've
       held my hands for too long off of the rule system :-(

I see the crash, too.

Now, for the one exception.  The delete rule below has one action
commented out, the removal of the original name.  If this is
uncommented, it is still not removed.  Have I misconstrued that action
or is this something else odd?

Thanks again for your help.

Cheers,
Brook

===========================================================================
drop sequence rule_table_1_id_seq;
drop table rule_table_1;
create table rule_table_1
(
 id		serial,
 name		text,

 unique (name)
);

drop sequence rule_table_2_id_seq;
drop table rule_table_2;
create table rule_table_2
(
 id		serial,
 table_1_id	int4		references rule_table_1 (id),
 address	text
);

drop view rule_view;
create view rule_view as select a.name, b.address from rule_table_1 a, rule_table_2 b
where a.id = b.table_1_id;

create rule rule_view_insert as on insert to rule_view
do instead (
        insert into rule_table_1 (name)
            select new.name
            where not exists
            (select a.id from rule_table_1 a where a.name = new.name);
        insert into rule_table_2 (table_1_id, address)
            select b.id, new.address from rule_table_1 b
            where new.name = b.name group by 1;
);

create rule rule_view_delete as on delete to rule_view
do instead (
	delete from rule_table_2 where table_1_id = (select id from rule_table_1 where name = current.name);
--	delete from rule_table_1 where name = current.name;
);

create rule rule_view_update as on update to rule_view
do instead (
	update rule_table_2 set address = new.address where table_1_id = (select id from rule_table_1 where name = current.name);
	update rule_table_1 set name = new.name where name = current.name;
);

insert into rule_view (name, address) values ('Tom', 'New York');
insert into rule_view (name, address) values ('Sue', 'Boston');
insert into rule_view (name, address) values ('Bill', 'Chicago');
insert into rule_view (name, address) values ('Tom', 'Boston');
select * from rule_view order by name, address;
select * from rule_table_1 order by name;
select * from rule_table_2 order by table_1_id;

update rule_view set address = 'Boston' where name = 'Bill';
select * from rule_view order by name, address;
select * from rule_table_1 order by name;
select * from rule_table_2 order by table_1_id;

delete from rule_view where name = 'Tom';
select * from rule_view order by name, address;
select * from rule_table_1 order by name;
select * from rule_table_2 order by table_1_id;



view thread (3+ messages)

Message-ID: <199904271720.LAA19445@trillium.nmsu.edu>
Permalink:  ../199904271720.LAA19445@trillium.nmsu.edu/
Also on:    postgresql.org/message-id/199904271720.LAA19445@trillium.nmsu.edu

 · 

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-sql@postgresql.org
  Cc: brook@trillium.NMSU.Edu, jwieck@debis.com
  Subject: Re: [SQL] rules help
  In-Reply-To: <199904271720.LAA19445@trillium.nmsu.edu>
  References: <m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de> <199904271720.LAA19445@trillium.nmsu.edu>

* 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