pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: jwieck@debis.com
To: brook@trillium.NMSU.Edu
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] rules help
Date: Tue, 27 Apr 1999 17:45:42 +0200 (MET DST)
Message-ID: <m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de> (raw)
In-Reply-To: <199904271426.IAA18768@trillium.nmsu.edu>

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) #





view thread (3+ messages)  latest in thread

Message-ID: <m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de>
Permalink:  ../m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de/
Also on:    postgresql.org/message-id/m10cA3K-000EBYC@orion.SAPserv.Hamburg.dsh.de

 · 

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

* 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