BUG #14870: wrong query results when using WITH with UPDATE

From: andreigorita(at)gmail(dot)com
To: pgsql-bugs(at)postgresql(dot)org
Cc: andreigorita(at)gmail(dot)com
Subject: BUG #14870: wrong query results when using WITH with UPDATE
Date: 2017-10-24 15:53:58
Message-ID: 20171024155358.1471.82377@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 14870
Logged by: Andrei Gorita
Email address: andreigorita(at)gmail(dot)com
PostgreSQL version: 9.6.1
Operating system: CentOS
Description:

when updating a table with unique index within a WITH part of a query, in
certain conditions the query reports that updated more than one row.

A simple way to reproduce:

test=> create table tmp_test(id int not null, test text not null);
CREATE TABLE
test=> create unique index on tmp_test(id);
CREATE INDEX
test=> INSERT INTO tmp_test VALUES (1, 'test');
INSERT 0 1
test=> create table tmp_test2(id int not null, test text not null);
CREATE TABLE

test=> WITH updated AS (UPDATE tmp_test SET test = 'test' WHERE id = 1
RETURNING id), inserted AS (INSERT INTO tmp_test2 (id, test) SELECT 1,
'test' WHERE NOT EXISTS (SELECT 1 FROM updated) RETURNING id) SELECT * FROM
updated;
id
----
1
(1 row)

This is the expected result, but when another session is executing in
parallel:
test=> begin;
BEGIN
test=> UPDATE tmp_test SET test = 'test' WHERE id = 1;
UPDATE 1
test=> commit;
COMMIT

the result is:

test=> WITH updated AS (UPDATE tmp_test SET test = 'test' WHERE id = 1
RETURNING id), inserted AS (INSERT INTO tmp_test2 (id, test) SELECT 1,
'test' WHERE NOT EXISTS (SELECT 1 FROM updated) RETURNING id) SELECT * FROM
updated;
id
----
1
1
(2 rows)

which is at least strange.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message banzaitron 2017-10-24 23:31:49 BUG #14871: RLS join query plan
Previous Message YasonTR 2017-10-24 15:47:33 Re: Possible regression in 'UPDATE ... SET (<column list>) = <row expression>' with just one single column/row value since v10