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 1h8pTy-000067-3B for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Mar 2019 17:03:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h8pTw-0000cU-H2 for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Mar 2019 17:03:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8pTw-0000ZY-9U for pgsql-bugs@lists.postgresql.org; Tue, 26 Mar 2019 17:03:24 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8pTt-0004Ag-HZ for pgsql-bugs@lists.postgresql.org; Tue, 26 Mar 2019 17:03:23 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x2QH3Hq2001135; Tue, 26 Mar 2019 13:03:17 -0400 From: Tom Lane To: lars.vonk@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15715: UPDATE using "in (subquery for update limit 1)" does not respect the limit in subquery In-reply-to: <15715-1eccf9c7dffc4093@postgresql.org> References: <15715-1eccf9c7dffc4093@postgresql.org> Comments: In-reply-to PG Bug reporting form message dated "Tue, 26 Mar 2019 15:08:49 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1133.1553619797.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 26 Mar 2019 13:03:17 -0400 Message-ID: <1134.1553619797@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PG Bug reporting form writes: > We are using a ruby library called delayed-job > (https://github.com/collectiveidea/delayed_job) that generates the follo= wing > sql = > LOG: duration: 82797.554 ms execute : UPDATE "delayed_jobs" S= ET > locked_at =3D '2019-03-26 13:25:20.808244', locked_by =3D 'host:myhost p= id:9958' > WHERE id IN (SELECT "delayed_jobs"."id" FROM "delayed_jobs" WHERE ((run= _at > <=3D '2019-03-26 13:25:20.807815' AND (locked_at IS NULL OR locked_at < > '2019-03-26 09:25:20.807828') OR locked_by =3D 'host:myhost pid:9958') A= ND > failed_at IS NULL) ORDER BY priority ASC, run_at ASC LIMIT 1 FOR UPDATE) > RETURNING * > We noticed that all the "delayed_jobs" were getting locked instead of ju= st > 1. That'd depend on the query plan you're getting, but in principle this query is unstable if there are other transactions changing the table contents: if the sub-SELECT is executed more than once then it could return (and lock) a different row each time, since the FOR UPDATE implies it will see latest values not those that applied when the query started. For that matter it could be unstable even without any concurrent transactions, seeing that the UPDATE's action is to change some of the fields the sub-SELECT's WHERE examines, and again the FOR UPDATE means that the sub-SELECT can see those changes. Given these particular values it seems like a row that satisfied the WHERE conditions to start with still would do so after the UPDATE, but I wonder whether that's always true in your usage. Any given execution of the sub-SELECT is definitely going to lock only one (or at most one) row, but whether it's the same row each time is harder to say. You might be well advised to replace the sub-SELECT with a CTE so that it's guaranteed to execute only once, ie WITH x AS (SELECT id ... FOR UPDATE LIMIT 1) UPDATE delayed_jobs SET ... WHERE id IN (SELECT id FROM x) > We also found some related stackoverflow content which sort of hints tha= t > this is a bug: > - https://dba.stackexchange.com/questions/69471/postgres-update-limit-1 > - > https://github.com/feikesteenbergen/demos/blob/master/bugs/update_limit_= bug.txt In that case the query is intentionally changing the sub-query's results (by changing the sort ordering field). I don't think it's a bug; it's really closer to being unspecified behavior. regards, tom lane