Re: The rule question before, request official documentation on the problem

From: ptjm(at)interlog(dot)com (Patrick TJ McPhee)
To: pgsql-general(at)postgresql(dot)org
Subject: Re: The rule question before, request official documentation on the problem
Date: 2007-04-12 04:40:15
Message-ID: 131re1ff6rk9k39@corp.supernews.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In article <461D0B1A(dot)6030407(at)metatrontech(dot)com>,
Chris Travers <chris(at)verkiel(dot)metatrontech(dot)com> wrote:

% DO ALSO rules involving NEW are fundamentally dangerous to the integrity
% of data because NEW is not guaranteed to be internally consistent. DO
% INSTEAD rules are fine (there is only one NEW), as are any DO ALSO rules
% involving OLD.

It seems to me that this sort of dogmatism is fundamentally dangerous.

CREATE TABLE x (a varchar(20) PRIMARY KEY, b INT NOT NULL);
CREATE TABLE y (a varchar(20) NOT NULL, b INT NOT NULL);
CREATE RULE y_ins AS ON INSERT TO y DO UPDATE x SET b=b+new.b WHERE a=new.a;
CREATE RULE y_del AS ON DELETE TO y DO UPDATE x SET b=b-old.b WHERE a=old.a;
INSERT INTO x VALUES ('a', 0);
INSERT INTO y VALUES ('a', 2);
INSERT INTO y VALUES ('a', 2);
SELECT * FROM x;
a | b
---+---
a | 4

DELETE FROM y;
SELECT * FROM x;
a | b
---+---
a | 2

The DO ALSO rules involving OLD didn't do so well here.

The section on rules v. triggers could do with a caveat or two, but
it's a bit much to call them "fundamentally dangerous".
--

Patrick TJ McPhee
North York Canada
ptjm(at)interlog(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jonathan Vanasco 2007-04-12 04:58:52 seeking: advice on reordering table
Previous Message Andrew - Supernews 2007-04-12 03:54:59 Re: hashtext () and collisions