RULE does not like the NOT EXISTS condition

From: Szima Gábor <sygma(at)tesla(dot)hu>
To: pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: RULE does not like the NOT EXISTS condition
Date: 2018-09-08 15:20:15
Message-ID: alpine.LSU.2.03.1809081705070.1822@tesla.hu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


Hi!

I found an "interest" in RULE's operation:

CREATE TABLE ruletest (id int4);
CREATE TABLE rulelog (id int4, ts timestamp NOT NULL DEFAULT current_timestamp);

CREATE RULE ruletest_rule AS ON INSERT TO ruletest DO INSERT INTO rulelog (id) VALUES (NEW.id);

INSERT INTO ruletest (id) VALUES (1);
INSERT INTO ruletest (id) SELECT 2 WHERE NOT EXISTS (SELECT 1 FROM ruletest WHERE id=2);
INSERT INTO ruletest (id) SELECT 3;

SELECT * FROM ruletest;
SELECT * FROM rulelog;

------
Output:

CREATE TABLE
CREATE TABLE
CREATE RULE
INSERT 0 1
INSERT 0 1
INSERT 0 1

id
----
1
2
3
(3 rows)

id | ts
----+----------------------------
1 | 2018-09-08 17:18:41.661292
3 | 2018-09-08 17:18:41.683459
(2 rows)

That is, in the NOT EXISTS condition the rule is not executed.

Version:
PostgreSQL 9.5.14 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.0, 64-bit
PostgreSQL 8.3.23 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 4.7.4

Is this bug or feature? :)

-Sygma

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2018-09-08 16:01:09 Re: [HACKERS] [BUGS] BUG #14825: enum type: unsafe use?
Previous Message Fabien COELHO 2018-09-08 13:03:51 Re: [HACKERS] WIP Patch: Pgbench Serialization and deadlock errors