pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: djlu126@126.com
To: pgsql-bugs@postgresql.org
Subject: BUG #11804: The delete rule problem
Date: Mon, 27 Oct 2014 06:29:43 +0000
Message-ID: <20141027062943.2615.76034@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

The following bug has been logged on the website:

Bug reference:      11804
Logged by:          Justin Lu
Email address:      djlu126@126.com
PostgreSQL version: 9.3.5
Operating system:   Windows 7 Ultimate x86
Description:        

The situation as flowing:

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 dtls (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



view thread (2+ messages)  latest in thread

Message-ID: <20141027062943.2615.76034@wrigleys.postgresql.org>
Permalink:  ../20141027062943.2615.76034@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20141027062943.2615.76034@wrigleys.postgresql.org

 · 

reply

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: djlu126@126.com
  Subject: Re: BUG #11804: The delete rule problem
  In-Reply-To: <20141027062943.2615.76034@wrigleys.postgresql.org>
  References: <20141027062943.2615.76034@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