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 1h8uxI-0008GB-Tt for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Mar 2019 22:54:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h8uxG-0007Yq-Kr for pgsql-bugs@arkaria.postgresql.org; Tue, 26 Mar 2019 22:54:02 +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 1h8uxG-0007Yj-96 for pgsql-bugs@lists.postgresql.org; Tue, 26 Mar 2019 22:54:02 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h8uxD-0005y5-AO for pgsql-bugs@lists.postgresql.org; Tue, 26 Mar 2019 22:54:00 +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 x2QMruPK016498; Tue, 26 Mar 2019 18:53:57 -0400 From: Tom Lane To: Lars Vonk 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: References: <15715-1eccf9c7dffc4093@postgresql.org> <1134.1553619797@sss.pgh.pa.us> Comments: In-reply-to Lars Vonk message dated "Tue, 26 Mar 2019 21:19:22 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <16496.1553640836.1@sss.pgh.pa.us> Date: Tue, 26 Mar 2019 18:53:56 -0400 Message-ID: <16497.1553640836@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Lars Vonk writes: > Thanks for the explanation. Our hotfix was to replace the IN with a = so > the query fails and the transaction is rolled back when more than one is > returned from the sub-SELECT. And have you actually seen any such failures? If you have, then there is certainly something going wrong here. The LIMIT should never allow more than one row to be returned by the sub-SELECT, period, full stop. The issue here is that the sub-SELECT is, in principle, executed afresh for every row of the outer query, and you do not have a guarantee that each such execution returns the *same* single row. Ordinary query execution would provide such a guarantee, but you're using FOR UPDATE on a table that's being modified concurrently (including by this query itself), and therefore the guarantee disappears. You might think that since the sub-SELECT is uncorrelated with the outer query, there's no need to execute it more than once ... but that's an optimization, not part of the guaranteed semantics. Without seeing an EXPLAIN for this query on your system, we can't know whether it's being done like that (though the fact that you're complaining suggests that it isn't). Interestingly, it's quite likely that your "hotfix" made the problem go away, because PG generally *does* optimize uncorrelated sub-SELECTs to be executed only once if they return scalar results. That doesn't happen for IN subqueries though, as those are treated as joins. Either way, a WITH is probably preferable because that does get you into a place where we guarantee single-evaluation semantics. regards, tom lane