Re: CREATE RULE ON UPDATE/DELETE

From: Stephan Szabo <sszabo(at)megazone23(dot)bigpanda(dot)com>
To: Joel Burton <joel(at)joelburton(dot)com>
Cc: Aasmund Midttun Godal <postgresql(at)envisity(dot)com>, pgsql-sql(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: CREATE RULE ON UPDATE/DELETE
Date: 2001-10-21 07:41:29
Message-ID: Pine.BSF.4.21.0110210025001.41306-100000@megazone23.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-sql


On Sat, 20 Oct 2001, Joel Burton wrote:

> On Sat, 20 Oct 2001, Aasmund Midttun Godal wrote:
>
> > Can a rule see the where statement in a query which it has been
> > triggered by? or is it simply ignored?? what happens?
> >
>
> Looking over your question, I wanted to clarify the problem a bit, so:
> (cleaned up example a bit from Aasmund)

> drop view normal;
> drop view dbl;
> drop table raw;
>
> CREATE TABLE raw (id INT PRIMARY KEY, name TEXT );
> INSERT INTO raw VALUES(1, 'a');
> INSERT INTO raw VALUES(2, 'b');
> INSERT INTO raw VALUES(12, 'c');
> INSERT INTO raw VALUES(15, 'd');
> INSERT INTO raw VALUES(14, 'e');
>
>
> -- set up two views: "normal", a simple view,
> -- and "dbl", which shows id * 2
>
> -- create basic rules to allow update to both views
>
> CREATE VIEW normal AS SELECT * FROM raw;
>
> CREATE RULE normal_update AS ON UPDATE TO normal DO INSTEAD UPDATE raw SET
> id = NEW.id, name = NEW.name WHERE OLD.id = id;
>
> CREATE VIEW dbl AS SELECT id * 2 as id, name FROM raw;
>
> CREATE RULE dbl_update AS ON UPDATE TO dbl DO INSTEAD UPDATE raw SET
> id = NEW.id, name = NEW.name WHERE OLD.id = id;

> The issue is that there are no IDs over 10 that have another ID that is
> exactly their value, so the first update to "dbl" does nothing.
>
> The second time, w/o the ID>10 restriction, it finds 1(a), and double
> that, 2(b), and adds 10; getting confused about which record to edit.
>
> Is this the best way to interpret this? Is this a bug?

Don't think so. I think the rule doesn't make any sense.
NEW.id and OLD.id are probably dbl values, so saying OLD.id=id (where id
is raw.id since that's the update table) isn't correct. It probably
should be OLD.id=id*2 (which seems to work for me, btw) It's editing
a different row than the one that's being selected.

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Stephan Szabo 2001-10-21 08:33:59 Re: CREATE RULE ON UPDATE/DELETE
Previous Message Joel Burton 2001-10-21 03:31:10 Re: CREATE RULE ON UPDATE/DELETE

Browse pgsql-sql by date

  From Date Subject
Next Message Stephan Szabo 2001-10-21 08:33:59 Re: CREATE RULE ON UPDATE/DELETE
Previous Message Joel Burton 2001-10-21 03:31:10 Re: CREATE RULE ON UPDATE/DELETE