Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1YrfEf-0006Wp-58 for pgsql-bugs@arkaria.postgresql.org; Mon, 11 May 2015 04:22:33 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1YrfEe-000338-KJ for pgsql-bugs@arkaria.postgresql.org; Mon, 11 May 2015 04:22:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1YrVpU-0002wn-GO for pgsql-bugs@postgresql.org; Sun, 10 May 2015 18:19:56 +0000 Received: from mx1.rootmx.com ([173.255.221.196]) by magus.postgresql.org with esmtps (TLS1.1:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1YrVpP-0005X6-Ng for pgsql-bugs@postgresql.org; Sun, 10 May 2015 18:19:54 +0000 Received: from [192.168.200.33] (AMontsouris-655-1-32-194.w90-44.abo.wanadoo.fr [90.44.79.194]) by mx1.rootmx.com (Postfix) with ESMTPSA id 004802F5E4; Sun, 10 May 2015 18:19:43 +0000 (UTC) Message-ID: <554FA13D.5020208@realityexists.net> Date: Sun, 10 May 2015 20:19:41 +0200 From: postgresql2@realityexists.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 MIME-Version: 1.0 To: "David G. Johnston" CC: "pgsql-bugs@postgresql.org" Subject: Re: BUG #13148: Unexpected deferred EXCLUDE constraint violation on derived table References: <20150424165722.2548.62492@wrigleys.postgresql.org> In-Reply-To: Content-Type: multipart/alternative; boundary="------------040806080309020605090106" X-Pg-Spam-Score: -1.9 (-) 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 This is a multi-part message in MIME format. --------------040806080309020605090106 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit That's odd! I just confirmed again that I get the exact same error with COMMIT (and no warnings). Are you able to run all the commands before the COMMIT successfully and get 1 row back from the SELECT? On 10/05/2015 7:47 PM, David G. Johnston wrote: > On Fri, Apr 24, 2015 at 9:57 AM, >wrote: > > The following bug has been logged on the website: > > Bug reference: 13148 > Logged by: Evan Martin > Email address: postgresql2@realityexists.net > > PostgreSQL version: 9.3.6 > Operating system: Windows 7 x64 SP1 > Description: > > I have a deferred EXCLUDE constraint on a derived table. Inside a > transaction I insert a new row that conflicts with an existing one > (so the > constraint would fail if it was immediate), delete the old row and > run an > unrelated UPDATE on the new row, then try to commit. I would > expect the > commit to succeed, since there is now no conflict, but it fails with > > ERROR: conflicting key value violates exclusion constraint > "uq_derived_timeslice_dup_time_ex" > SQL state: 23P01 > Detail: Key (feature_id, valid_time_begin, interpretation, > (COALESCE(sequence_number, (-1))))=(1, 2015-01-01 00:00:00, X, -1) > conflicts > with existing key (feature_id, valid_time_begin, interpretation, > (COALESCE(sequence_number, (-1))))=(1, 2015-01-01 00:00:00, X, -1). > > If I run the delete statement first it works. If I remove the > (seemingly > unrelated) update statement it also works. Reproducible under > PostgreSQL > 9.3.6 64-bit on Windows 7 and Postgresql 9.2.10 32-bit on Ubuntu > using the > script below. > > > -- **** One-off set-up **** > /* > -- DROP TABLE IF EXISTS base_timeslice CASCADE; > > CREATE TABLE base_timeslice > ( > timeslice_id integer NOT NULL, > feature_id integer NOT NULL, > valid_time_begin timestamp NOT NULL, > interpretation text NOT NULL, > sequence_number integer, > CONSTRAINT pk_base_timeslice PRIMARY KEY (timeslice_id) > ); > > CREATE TABLE derived_timeslice > ( > timeslice_id integer NOT NULL, > feature_id integer NOT NULL, > name text NOT NULL, > CONSTRAINT pk_derived_timeslice PRIMARY KEY (timeslice_id), > CONSTRAINT uq_derived_timeslice_dup_time_ex EXCLUDE > USING btree (feature_id WITH =, valid_time_begin WITH =, > interpretation > WITH =, (COALESCE(sequence_number::integer, (-1))) WITH =) > DEFERRABLE INITIALLY DEFERRED > ) > INHERITS (base_timeslice); > > INSERT INTO derived_timeslice (timeslice_id, feature_id, > valid_time_begin, > interpretation, name) > VALUES (51, 1, '2015-01-01', 'X', 'Test'); > */ > > -- **** Repro **** > > BEGIN; > > -- Insert row that violates deferred constraint > INSERT INTO derived_timeslice (timeslice_id, feature_id, > valid_time_begin, > interpretation, name) > VALUES (52, 1, '2015-01-01', 'X', 'Test'); > > -- Delete the old row - now there should be no more conflict > DELETE FROM derived_timeslice WHERE timeslice_id = 51; > > -- Problem doesn't occur without an UPDATE statement > UPDATE derived_timeslice SET name = 'Updated' WHERE timeslice_id = 52; > > -- This confirms there is only 1 row - no conflict > SELECT timeslice_id, valid_time_begin FROM derived_timeslice WHERE > feature_id = 1; > > --COMMIT; > SET CONSTRAINTS ALL IMMEDIATE; -- Enfore constraint - error occurs > here > > ROLLBACK; > > > ​ I can provide a limited affirmation that the above example is > problematic on 9.3 > > Changing "SET CONSTRAINTS ALL IMMEDIATE" to "COMMIT;" results in a > warning that there is no currently active transaction and the > transaction itself appears to have been rolled back. > > David J.​ > --------------040806080309020605090106 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
That's odd! I just confirmed again that I get the exact same error with COMMIT (and no warnings). Are you able to run all the commands before the COMMIT successfully and get 1 row back from the SELECT?

On 10/05/2015 7:47 PM, David G. Johnston wrote:
On Fri, Apr 24, 2015 at 9:57 AM, <postgresql2@realityexists.net> wrote:
The following bug has been logged on the website:

Bug reference:      13148
Logged by:          Evan Martin
Email address:      postgresql2@realityexists.net
PostgreSQL version: 9.3.6
Operating system:   Windows 7 x64 SP1
Description:

I have a deferred EXCLUDE constraint on a derived table. Inside a
transaction I insert a new row that conflicts with an existing one (so the
constraint would fail if it was immediate), delete the old row and run an
unrelated UPDATE on the new row, then try to commit. I would expect the
commit to succeed, since there is now no conflict, but it fails with

        ERROR: conflicting key value violates exclusion constraint
"uq_derived_timeslice_dup_time_ex"
        SQL state: 23P01
        Detail: Key (feature_id, valid_time_begin, interpretation,
(COALESCE(sequence_number, (-1))))=(1, 2015-01-01 00:00:00, X, -1) conflicts
with existing key (feature_id, valid_time_begin, interpretation,
(COALESCE(sequence_number, (-1))))=(1, 2015-01-01 00:00:00, X, -1).

If I run the delete statement first it works. If I remove the (seemingly
unrelated) update statement it also works. Reproducible under PostgreSQL
9.3.6 64-bit on Windows 7 and Postgresql 9.2.10 32-bit on Ubuntu using the
script below.


-- **** One-off set-up ****
/*
-- DROP TABLE IF EXISTS base_timeslice CASCADE;

CREATE TABLE base_timeslice
(
  timeslice_id integer NOT NULL,
  feature_id integer NOT NULL,
  valid_time_begin timestamp NOT NULL,
  interpretation text NOT NULL,
  sequence_number integer,
  CONSTRAINT pk_base_timeslice PRIMARY KEY (timeslice_id)
);

CREATE TABLE derived_timeslice
(
  timeslice_id integer NOT NULL,
  feature_id integer NOT NULL,
  name text NOT NULL,
  CONSTRAINT pk_derived_timeslice PRIMARY KEY (timeslice_id),
  CONSTRAINT uq_derived_timeslice_dup_time_ex EXCLUDE
        USING btree (feature_id WITH =, valid_time_begin WITH =, interpretation
WITH =, (COALESCE(sequence_number::integer, (-1))) WITH =)
        DEFERRABLE INITIALLY DEFERRED
)
INHERITS (base_timeslice);

INSERT INTO derived_timeslice (timeslice_id, feature_id, valid_time_begin,
interpretation, name)
VALUES (51, 1, '2015-01-01', 'X', 'Test');
*/

-- **** Repro ****

BEGIN;

-- Insert row that violates deferred constraint
INSERT INTO derived_timeslice (timeslice_id, feature_id, valid_time_begin,
interpretation, name)
VALUES (52, 1, '2015-01-01', 'X', 'Test');

-- Delete the old row - now there should be no more conflict
DELETE FROM derived_timeslice WHERE timeslice_id = 51;

-- Problem doesn't occur without an UPDATE statement
UPDATE derived_timeslice SET name = 'Updated' WHERE timeslice_id = 52;

-- This confirms there is only 1 row - no conflict
SELECT timeslice_id, valid_time_begin FROM derived_timeslice WHERE
feature_id = 1;

--COMMIT;
SET CONSTRAINTS ALL IMMEDIATE; -- Enfore constraint - error occurs here

ROLLBACK;


​ I can provide a limited affirmation that the above example is problematic on 9.3

Changing "SET CONSTRAINTS ALL IMMEDIATE" to "COMMIT;" results in a warning that there is no currently active transaction and the transaction itself appears to have been rolled back.

David J.​


--------------040806080309020605090106--