The following bug has been logged online:
Bug reference: 2563
Logged by: ivankob
Email address: ivankob@front.ru
PostgreSQL version: 8.1.4
Operating system: Linux
Description: In rules: recalculation of input expression on each
access
Details:
For a testcase like :
CREATE SEQUENCE seq1;
CREATE TABLE t1 (
id INTEGER DEFAULT NEXTVAL('"seq1"'::text),
code INTEGER,
data TEXT
);
CREATE TABLE s1 (
id INTEGER,
code INTEGER,
state BOOLEAN
);
INSERT INTO t1 (code,data) VALUES (100,'Green elaphant');
INSERT INTO s1 VALUES (currval('"seq1"'::text),100,'t');
INSERT INTO t1 (code,data) VALUES (101,'Mad tortoise');
INSERT INTO s1 VALUES (currval('"seq1"'::text),101,'f');
-----------
CREATE VIEW v1 AS
SELECT a.code, a.data FROM t1 a, s1 b WHERE a.id=b.id AND a.code=b.code AND
b.state = 't';
------------
CREATE RULE rule1 ON INSERT TO v1 DO INSTEAD (
INSERT INTO t1 ( id, code, data) VALUES ( nextval('"seq1"'::text),
NEW.code, NEW.data );
INSERT INTO s1 ( id, code, state ) VALUES ( currval('"seq1"'::text),
NEW.code, 't' );
);
------------
INSERT INTO v1 (code,data) VALUES (
(SELECT MAX(code) FROM t1)+1,
'Penguin-sprinter'
);
after run, it seems that "(SELECT MAX(code) FROM t1)+1" is performed twice
(
on each NEW.code ) in the rule since "s1.code" results in greater than
"t1.code" by "1" what's wrong.
Access by reference not value ?