Re: [SQL] rules help - Mailing list pgsql-sql

From jwieck@debis.com (Jan Wieck)
Subject Re: [SQL] rules help
Date
Msg-id m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de
Whole thread Raw
In response to rules help  (Brook Milligan <brook@trillium.NMSU.Edu>)
Responses Re: [SQL] rules help
List 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@debis.com (Jan Wieck) #

pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: [SQL] Arrays and count()
Next
From: José Soares
Date:
Subject: Re: [SQL] substring