Re: [SQL] rules help

From: jwieck(at)debis(dot)com (Jan Wieck)
To: brook(at)trillium(dot)NMSU(dot)Edu (Brook Milligan)
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] rules help
Date: 1999-04-27 15:45:42
Message-ID: m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Brook Milligan wrote:

>
> I am trying to create a view with rules to make data entry, etc. more
> convenient. The view must be a union of two tables and the rules must
> manipulate the underlying tables. Everything is fine except for one
> thing I need help on.
>
> [...]
>
> Apparently, putting a where condition on a rule doesn't work (see
> error message below).

Looks strange - must dive into soon. But anyway, in your case
the order in which the rule actions take place is important.
So the entire approach to setup two separate rules is wrong,
because it is not guaranteed in which order the rules will
get applied later! If the rule inserting into table 2 get's
applied first, the entry in table 1 might not be there and
thus, the insert will become a noop since the join
t1.id+new.address would be empty.

This is a case where multi action rules are required. And in
that case, the qualification must be part of the actions, not
the entire rule because you want the second action to execute
allways.

I got it working, but another very strange thing surfaced and
absolutely have no clue where that was coming from.

>
> ===========================================================================
> -- tables
> create table rule_table_1
> (
> id serial,
> name text,
> unique (name)
> );
>
> create table rule_table_2
> (
> id serial,
> table_1_id int4 references rule_table_1 (id),
> address text
> );
>
> -- view
>
> 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;
>
> -- rules

create rule rule_view_ins 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;
);

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.

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.

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 :-(

Jan

--

#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#======================================== jwieck(at)debis(dot)com (Jan Wieck) #

In response to

  • rules help at 1999-04-27 14:26:08 from Brook Milligan

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Brook Milligan 1999-04-27 17:20:33 Re: [SQL] rules help
Previous Message José Soares 1999-04-27 15:18:47 Re: [SQL] substring