pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Joe Wildish <joe-postgresql.org@elusive.cx>
To: pgsql-general@postgresql.org
Subject: Inexplicable UPDATE...RETURNING behaviour
Date: Tue, 16 Apr 2019 00:07:53 +0100
Message-ID: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx> (raw)

Hello all,

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;

However, when using the following statement, which (AFAIK) is semantically equivalent, we see only a single row being updated/dequeued:

UPDATE queue.some_queue AS q
   SET (state, awaiting) = ('executing', FALSE)
 WHERE uid = (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)
 RETURNING uid;

IMO the two statements should yield the same result. But, we see the first one updating multiple rows and therefore dequeing multiple uids, yet the second one functions as intended (ie. single item is dequeued).

We can replicate this locally in tests but I can't explain it. Is this a bug, or am I overlooking something?

Cheers,
-Joe

PG. Postgres 10.6 in production, and the same behaviour with 10.5 + 11.2 in dev.




view thread (3+ messages)  latest in thread

Message-ID: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx>
Permalink:  ../9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx/
Also on:    postgresql.org/message-id/9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx

 · 

reply

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: joe-postgresql.org@elusive.cx
  Subject: Re: Inexplicable UPDATE...RETURNING behaviour
  In-Reply-To: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx>
  References: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx>

* 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