X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 5CFDA3A5AE2 for ; Thu, 27 Jan 2005 01:41:28 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 84503-02 for ; Thu, 27 Jan 2005 01:41:15 +0000 (GMT) Received: from lilly.baden-online.de (lilly.baden-online.de [217.28.101.4]) by svr1.postgresql.org (Postfix) with ESMTP id 4C58D3A5908 for ; Thu, 27 Jan 2005 01:41:16 +0000 (GMT) Received: from lilly.baden-online.de (lilly.baden-online.de [217.28.101.4]) by lilly.baden-online.de (8.12.11/8.12.11) with ESMTP id j0R1gX0t012325 for ; Thu, 27 Jan 2005 02:42:33 +0100 Date: Thu, 27 Jan 2005 02:42:33 +0100 (MET) From: Ralph Graulich X-X-Sender: graulich@lilly.baden-online.de Reply-To: maillist@shauny.de To: pgsql-sql@postgresql.org Subject: Rule problem with OLD / NEW record set Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200501/277 X-Sequence-Number: 20065 Hello everyone, given is a table with a version history kind of thing I am currently working on. Upon this table there is a view and the application interacts with the view only, updating/inserting/deleting is controlled by rules. It seems like the record set "OLD" gets changed when it is used in a SQL expression: CREATE TABLE table1 ( id INTEGER NOT NULL, version INTEGER NOT NULL DEFAULT 0, vnoflag CHAR(1), content VARCHAR(20) ); INSERT INTO table1 (id, version, vnoflag, content) VALUES (1, 1, 'Y', 'Test'); CREATE VIEW view_table1 AS SELECT * FROM table1; -- create a rule for update CREATE OR REPLACE RULE ru_view_table1_update AS ON UPDATE TO view_table1 DO INSTEAD ( -- insert a new record with the old id, old version number incremented -- by one, versionflag set to 'Y' and the new content INSERT INTO table1 (id, version, vnoflag, content) VALUES (OLD.id, OLD.version+1, 'Y', NEW.content); -- update the old version and set its versionflag to 'N' as it is no -- longer the current record UPDATE table1 SET vnoflag = 'N' WHERE id = OLD.id AND version = OLD.version; ); SELECT * FROM view_table1; id | version | vnoflag | content ----+---------+---------+--------- 1 | 1 | Y | Test (1 row) UPDATE view_table1 SET content = 'New Test' WHERE id = 1 AND vnoflag = 'Y'; SELECT * FROM view_table1; id | version | vnoflag | content ----+---------+---------+---------- 1 | 1 | N | Test 1 | 2 | N | New Test It seems like the UPDATE statement updates both the old and the new version. If I correctly go through the statements by hand, they should read: INSERT INTO table1 (id, version, vnoflag, content) VALUES (1, 1+1, 'Y', 'New Test'); UPDATE table1 SET vnoflag = 'N' WHERE id = 1 AND version = 1; If I change the UPDATE statement to read: UPDATE table1 SET vnoflag = 'N' WHERE id = OLD.id AND vno = NEW.vno-1; it works like expected: id | version | vnoflag | content ----+---------+---------+---------- 1 | 2 | Y | New Test 1 | 1 | N | Test Where is my logical error? Shouldn't the first UPDATE statement suffice? Best regards ... Ralph ...