From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joe Wildish <joe-postgresql.org@elusive.cx>
Cc: pgsql-general@postgresql.org
Subject: Re: Inexplicable UPDATE...RETURNING behaviour
Date: Mon, 15 Apr 2019 19:58:49 -0400
Message-ID: <25988.1555372729@sss.pgh.pa.us> (raw)
In-Reply-To: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx>
References: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx>
Joe Wildish <joe-postgresql.org@elusive.cx> writes:
> We are seeing an inexplicable behaviour when issuing an "UPDATE..RETURNING" statement. I am unsure if it is a Postgres bug. Additional eyes-on would be much appreicated.
> When issuing the following statement we are seeing multiple rows UPDATE'd despite the use of LIMIT 1 and despite the "uid" column in the "some_queue" table having a PRIMARY KEY constraint on it:
> UPDATE queue.some_queue AS q
> SET (state, awaiting) = ('executing', FALSE)
> FROM (SELECT uid
> FROM queue.some_queue
> WHERE awaiting
> AND process_after <= CURRENT_TIMESTAMP AT TIME ZONE 'UTC'
> ORDER BY process_after ASC
> FOR UPDATE SKIP LOCKED
> LIMIT 1)
> AS dq(uid)
> WHERE q.uid = dq.uid
> RETURNING q.uid;
Yeah, there was another similar complaint a few weeks ago --- has this
suddenly gotten to be a popular coding idea?
The basic problem with what you have here is that FOR UPDATE (especially
with SKIP LOCKED) makes the sub-select's output unstable by definition.
If it's executed more than once then you might get different rows back,
allowing the outer UPDATE's join to potentially match multiple rows from
the outer instance of queue.some_queue. Typically, since it's LIMIT 1,
I'd think that the planner would put dq on the outside of a nestloop plan
and you'd escape seeing any problem --- but if it gets put on the inside
of a nestloop, it's going to misbehave.
There are (at least) two different ways that the sub-select's output
might change when re-executed, even though it's still using the same
snapshot as before:
1. If some other transaction releases a row lock in between, SKIP LOCKED
might not skip that row any more.
2. The row returned the first time will absolutely not get chosen the
second time, given this particular query formulation, because its latest
updated version will have awaiting = false thanks to the action of the
outer UPDATE, so it'll fail the inner WHERE test.
The way I'd recommend fixing it is to put the FOR UPDATE into a WITH
to guarantee single execution:
WITH dq(uid) AS (SELECT uid ... LIMIT 1)
UPDATE queue.some_queue q SET ...
FROM dq
WHERE q.uid = dq.uid
RETURNING q.uid;
regards, tom lane
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-general@postgresql.org
Cc: tgl@sss.pgh.pa.us, joe-postgresql.org@elusive.cx
Subject: Re: Inexplicable UPDATE...RETURNING behaviour
In-Reply-To: <25988.1555372729@sss.pgh.pa.us>
References: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx> <25988.1555372729@sss.pgh.pa.us>
* 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