Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mWnWx-0001XL-9G for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Oct 2021 22:30:55 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mWnWw-0005LW-6B for pgsql-bugs@arkaria.postgresql.org; Sat, 02 Oct 2021 22:30:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mWnWv-0005LN-W0 for pgsql-bugs@lists.postgresql.org; Sat, 02 Oct 2021 22:30:53 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mWnWt-0004cb-Ru for pgsql-bugs@lists.postgresql.org; Sat, 02 Oct 2021 22:30:53 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 192MUiAA3720265; Sat, 2 Oct 2021 18:30:44 -0400 From: Tom Lane To: Alvaro Herrera cc: Emil Iggland , David Christensen , PostgreSQL mailing lists , Surafel Temesgen Subject: Re: BUG #17141: SELECT LIMIT WITH TIES FOR UPDATE SKIP LOCKED returns wrong number of rows In-reply-to: <202110022205.jwz6b5bdyja5@alvherre.pgsql> References: <202110022205.jwz6b5bdyja5@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Sat, 02 Oct 2021 19:05:14 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3720263.1633213844.1@sss.pgh.pa.us> Date: Sat, 02 Oct 2021 18:30:44 -0400 Message-ID: <3720264.1633213844@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alvaro Herrera writes: > On 2021-Aug-25, Emil Iggland wrote: >> I see a lot of thought being put into how this issue can be worked >> around, but very little discussion on if this behaviour is correct or >> not. Without having thought about it much deeper, this seems to only be >> a problem with the "WITH TIES" clause which provokes this extra row >> being locked. Perhaps that is where the problem should be attacked. > The problem is that when the WITH TIES clause is added, we need to read > one extra row after the one that reaches the LIMIT count, in order to > verify whether it (the next one) should be included due to a tie. With > the executor structure that we currently have, there is no way to read > that row and not lock it. So a good fix would be to separate the act of > locking the row from the act of reading it. I'm not convinced that's a "good fix". SELECT FOR UPDATE generally considers that it should return rows only if they still match the WHERE condition after they're locked. The natural extension of that to WITH TIES is that you ought to check for equality after locking. So these features seem rather fundamentally in conflict. regards, tom lane