Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XjTno-0006fg-QR for pgsql-bugs@arkaria.postgresql.org; Wed, 29 Oct 2014 14:00:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1XjTno-0007Ga-9E for pgsql-bugs@arkaria.postgresql.org; Wed, 29 Oct 2014 14:00:44 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1XjPY7-0004zh-Cf for pgsql-bugs@postgresql.org; Wed, 29 Oct 2014 09:28:15 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1XjPXx-0001cs-NG for pgsql-bugs@postgresql.org; Wed, 29 Oct 2014 09:28:12 +0000 MIME-Version: 1.0 Subject: BUG #11821: The delete rule problem To: pgsql-bugs@postgresql.org From: djlu126@126.com Date: Wed, 29 Oct 2014 09:27:27 +0000 Message-ID: <20141029092727.2613.83924@wrigleys.postgresql.org> X-Pg-Spam-Score: -1.9 (-) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org The following bug has been logged on the website: Bug reference: 11821 Logged by: Justin Lu Email address: djlu126@126.com PostgreSQL version: 9.3.5 Operating system: Windows 7 Ultimate Description: The situation as following: CREATE TABLE test ( test_id integer NOT NULL, CONSTRAINT test_pkey PRIMARY KEY (test_id) ); CREATE TABLE details ( test_id integer NOT NULL, prd_id integer NOT NULL, quantity integer, CONSTRAINT details_pkey PRIMARY KEY (test_id, prd_id) ); CREATE TABLE stock ( prd_id integer NOT NULL, remaining integer, CONSTRAINT stock_pkey PRIMARY KEY (prd_id) ); CREATE OR REPLACE RULE test_d1 AS ON DELETE TO test DO DELETE FROM details WHERE details.test_id = old.test_id; CREATE OR REPLACE RULE details_d1 AS ON DELETE TO details WHERE (EXISTS ( SELECT stock_1.prd_id FROM stock stock_1 WHERE stock_1.prd_id = old.prd_id)) DO UPDATE stock SET remaining = stock.remaining - old.quantity WHERE stock.prd_id = old.prd_id; CREATE OR REPLACE RULE details_d2 AS ON DELETE TO details WHERE NOT (EXISTS ( SELECT stock_1.prd_id FROM stock stock_1 WHERE stock_1.prd_id = old.prd_id)) DO INSERT INTO stock (prd_id, remaining) VALUES (old.prd_id, - old.quantity); COPY stock (prd_id, remaining) FROM stdin; 1 9 2 9 3 9 4 9 5 9 6 9 7 9 8 9 9 9 \. COPY test (test_id) FROM stdin; 1 2 3 4 5 6 7 8 9 \. COPY details (test_id, prd_id, quantity) FROM stdin; 1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 2 1 1 2 2 1 2 3 1 2 4 1 2 5 1 2 6 1 2 7 1 2 8 1 2 9 1 3 1 1 3 2 1 3 3 1 3 4 1 3 5 1 3 6 1 3 7 1 3 8 1 3 9 1 4 1 1 4 2 1 4 3 1 4 4 1 4 5 1 4 6 1 4 7 1 4 8 1 4 9 1 5 1 1 5 2 1 5 3 1 5 4 1 5 5 1 5 6 1 5 7 1 5 8 1 5 9 1 6 1 1 6 2 1 6 3 1 6 4 1 6 5 1 6 6 1 6 7 1 6 8 1 6 9 1 7 1 1 7 2 1 7 3 1 7 4 1 7 5 1 7 6 1 7 7 1 7 8 1 7 9 1 8 1 1 8 2 1 8 3 1 8 4 1 8 5 1 8 6 1 8 7 1 8 8 1 8 9 1 9 1 1 9 2 1 9 3 1 9 4 1 9 5 1 9 6 1 9 7 1 9 8 1 9 9 1 \. Then I execute the command: delete from test where test_id in(1,2); select * from stock; The result is: prd_id | remaining --------+----------- 1 | 8 2 | 8 3 | 8 4 | 8 5 | 8 6 | 8 7 | 8 8 | 8 9 | 8 Shouldn't it be: prd_id | remaining --------+----------- 1 | 7 2 | 7 3 | 7 4 | 7 5 | 7 6 | 7 7 | 7 8 | 7 9 | 7 ? -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs