Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGAhu-0005fF-01 for pgsql-general@arkaria.postgresql.org; Mon, 15 Apr 2019 23:08:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hGAhq-0001md-UI for pgsql-general@arkaria.postgresql.org; Mon, 15 Apr 2019 23:08:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGAhq-0001mR-Cz for pgsql-general@lists.postgresql.org; Mon, 15 Apr 2019 23:08:06 +0000 Received: from mx.ij.cx ([212.13.201.16] helo=mail.ij.cx) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGAhm-0001oG-SH for pgsql-general@postgresql.org; Mon, 15 Apr 2019 23:08:04 +0000 Received: from 0541332d.skybroadband.com ([5.65.51.45] helo=imac.localdomain) by mail.ij.cx with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1hGAdY-00070p-Gd for pgsql-general@postgresql.org; Tue, 16 Apr 2019 00:03:42 +0100 From: Joe Wildish Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.8\)) Subject: Inexplicable UPDATE...RETURNING behaviour Message-Id: <9C47F3F5-D646-4A81-A8CC-FA8FD40EFF07@elusive.cx> Date: Tue, 16 Apr 2019 00:07:53 +0100 To: pgsql-general@postgresql.org X-Mailer: Apple Mail (2.3445.104.8) X-Spam-Score: -1.0 X-Spam-Level: - X-Spam-Report: Spam detection software, running on the system "wes.ijneb.com", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: 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: [...] Content analysis details: (-1.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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) =3D ('executing', FALSE) FROM (SELECT uid FROM queue.some_queue WHERE awaiting AND process_after <=3D CURRENT_TIMESTAMP AT TIME ZONE 'UTC' ORDER BY process_after ASC FOR UPDATE SKIP LOCKED LIMIT 1) AS dq(uid) WHERE q.uid =3D 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) =3D ('executing', FALSE) WHERE uid =3D (SELECT uid FROM queue.some_queue WHERE awaiting AND process_after <=3D 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.=