From: mtesfaye@gmail.com
To: pgsql-bugs@postgresql.org
Subject: BUG #7662: INSERT rule doesn't work as expected
Date: Thu, 15 Nov 2012 16:48:34 +0000
Message-ID: <E1TZ2cE-0001oY-1M@wrigleys.postgresql.org> (raw)
The following bug has been logged on the website:
Bug reference: 7662
Logged by: Melese Tesfaye
Email address: mtesfaye@gmail.com
PostgreSQL version: 9.2.1
Operating system: FreeBSD 9.0-RELEASE
Description:
ON INSERT RULE doesn't always work as expected.
The reasoning for this particular rule was to add the new value to an
existing value if it is determined that the key already exists.
Here is the table definition:
CREATE TABLE test_r(id INT PRIMARY KEY,val INT NOT NULL);
Here is the rule definition:
CREATE OR REPLACE RULE "dup_add_test_r_rule" AS
ON INSERT TO test_r
WHERE
EXISTS(SELECT 1 FROM test_r a WHERE a.id=NEW.id)
DO INSTEAD
(UPDATE test_r a SET val=a.val+NEW.val WHERE a.id=NEW.id);
Empty table:
SELECT * FROM test_r;
+----+-----+
| id | val |
+----+-----+
+----+-----+
(0 rows)
Time: 0.775 ms
Now, insert the following.
INSERT INTO test_r VALUES(1,10);
INSERT 0 1
Time: 2.038 ms
Query the table after insert (expected val to be 10 since the rule would
have
been igonred as id 1 didn't exist prior to inserting.
SELECT * FROM test_r;
+----+-----+
| id | val |
+----+-----+
| 1 | 20 |
+----+-----+
(1 row)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: mtesfaye@gmail.com
Subject: Re: BUG #7662: INSERT rule doesn't work as expected
In-Reply-To: <E1TZ2cE-0001oY-1M@wrigleys.postgresql.org>
References: <E1TZ2cE-0001oY-1M@wrigleys.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox