BUG #2562: In rules: recalculation of input expression on each access

From: "IvankoB" <ivankob(at)front(dot)ru>
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #2562: In rules: recalculation of input expression on each access
Date: 2006-08-03 20:23:59
Message-ID: 200608032023.k73KNxSD061331@wwwmaster.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs


The following bug has been logged online:

Bug reference: 2562
Logged by: IvankoB
Email address: ivankob(at)front(dot)ru
PostgreSQL version: 8.1.4
Operating system: Linux ( Debia 3.1)
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 ?

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2006-08-03 21:30:48 Re: [BUGS] Patch to allow C extension modules to initialize/finish
Previous Message Ralf S. Engelschall 2006-08-03 17:04:29 Re: Patch to allow C extension modules to initialize/finish