Re: Queries getting older values (autocommit enabled)

From: Eudald Valcàrcel Lacasa <eudald(dot)valcarcel(at)gmail(dot)com>
To: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: Queries getting older values (autocommit enabled)
Date: 2020-04-25 19:44:06
Message-ID: CANEx+AWFL=3oqNNZJf5WnkpYTr6wTOkxFAP7aufPKe7YciqrkA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hello David,
Thanks for your answer.
The actual problem is that my SELECT query is being executed meanwhile the
UPDATE one, but instead of waiting for the UPDATE to be executed, it
doesn't wait and gets the value from the field prior to the update.
I believed it was because of the MVCC, and I was looking for a workaround
to force the select to wait for the UPDATE to finish, instead of taking
values of a snapshot.

Eudald

El sáb., 25 abr. 2020 a las 21:36, David G. Johnston (<
david(dot)g(dot)johnston(at)gmail(dot)com>) escribió:

> On Sat, Apr 25, 2020 at 12:07 PM Eudald Valcàrcel Lacasa <
> eudald(dot)valcarcel(at)gmail(dot)com> wrote:
>
>> Hello again,
>> I've been looking for this issue and I'd like to know the behavior of FOR
>> UPDATE SKIP LOCKED in the following scenario:
>> * One query does an UPDATE targeting a row in the table
>> * Another query run in parallel does a SELECT...FOR UPDATE SKIP LOCKED
>> targeting the same (being updated) row on the table.
>> From SKIP LOCKED definition: . With SKIP LOCKED, any selected rows that
>> cannot be immediately locked are skipped.
>>
>> Would it mean that the 2nd query wouldn't check the affected row since
>> it's locked by the first query?
>>
>
> Yes. What else would it mean?
>
> If that's the behavior, is there any way I could make the SELECT query
>> wait for the UPDATE LOCK? Is it recommended? Are there downsides to this
>> approach?
>>
>>>
>>>>
> "To prevent the operation from waiting for other transactions to commit,
> use either the NOWAIT or SKIP LOCKED option."
>
> Which means that if you don't include the NOWAIT clause you, well, wait.
>
> David J.
>
>

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message David G. Johnston 2020-04-25 20:09:00 Re: Queries getting older values (autocommit enabled)
Previous Message David G. Johnston 2020-04-25 19:36:20 Re: Queries getting older values (autocommit enabled)