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

From: Marko Tiikkaja <marko(at)joh(dot)to>
To: andreigorita(at)gmail(dot)com
Cc: Pg Bugs <pgsql-bugs(at)postgresql(dot)org>
Subject: Re: BUG #14870: wrong query results when using WITH with UPDATE
Date: 2017-11-11 12:05:33
Message-ID: CAL9smLBOO8w9j2Y0zrRcdt-Kw2hiC8Ntdwro1FDq=j+z8Q9LqA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi Andrei,

On Tue, Oct 24, 2017 at 6:53 PM, <andreigorita(at)gmail(dot)com> wrote:

> 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:
>
> the result is:
>
> id
> ----
> 1
> 1
> (2 rows)
>

Right. We don't actually need tmp_test2 to trigger the bug, we simply need
an InitPlan on the CTE. This query:

WITH updated AS (
UPDATE tmp_test SET test = 'test' WHERE id = 1
RETURNING id
)
SELECT (SELECT 1 FROM updated), * FROM updated

has the same problem. The query plan looks like this:

CTE Scan on updated (actual rows=2 loops=1)
CTE updated
-> Update on tmp_test (actual rows=1 loops=1)
-> Index Scan using tmp_test_id_idx on tmp_test (actual rows=1
loops=1)
Index Cond: (id = 1)
InitPlan 2 (returns $2)
-> CTE Scan on updated updated_1 (actual rows=1 loops=1)

As you said, this problem only occurs when the row changed by the CTE is
concurrently UPDATEd, so I'm guessing something goes wrong with EPQ here.

.m

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Vincent Lachenal 2017-11-11 12:39:00 Re: BUG #14897: Segfault on statitics SQL request
Previous Message Kotadiya Dhrupesh 2017-11-11 10:56:09 Re: Help me plz