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 1mZvy3-0003BG-IK for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Oct 2021 14:07:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mZvy2-0002Vh-Gt for pgsql-bugs@arkaria.postgresql.org; Mon, 11 Oct 2021 14:07:50 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mZvy2-0002VY-8j for pgsql-bugs@lists.postgresql.org; Mon, 11 Oct 2021 14:07:50 +0000 Received: from mail-gateway-shared01.cyon.net ([194.126.200.215]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mZvxz-0002uX-21 for pgsql-bugs@lists.postgresql.org; Mon, 11 Oct 2021 14:07:49 +0000 Received: from s065.cyon.net ([149.126.4.74]) by mail-gateway-shared01.cyon.net with esmtpsa (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim) (envelope-from ) id 1mZvxv-00015N-E2 for pgsql-bugs@lists.postgresql.org; Mon, 11 Oct 2021 16:07:45 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=iggland.com ; s=default; h=Content-Transfer-Encoding:Content-Type:In-Reply-To: MIME-Version:Date:Message-ID:From:References:Cc:To:Subject:Sender:Reply-To: Content-ID:Content-Description; bh=kEx8Pcjy0OiSrYs0BBz5oz8LFVHXAqaaaLn10Q/DrZY=; b=aP3joIwiLLvgppChrGzKK1M44s zi0ME3JBHOSsH2ngwGdQ3po3Bl0h3UCWv1PV0vFoEyKudaNMWIcJGtDW9n5Wut4ebLwH6T/Nlzej6 O5HumgQMZJAm7N5D5yq5vbWG8IR81Y4y2Bsu01tvw+WGNCxGwOekkcNGGWw7b057nyCgZM4paQZ2p kH8CcRsb1MB/4Q+LOowzXDMf5ss+zmqkgRgyj4FysfhCwNeBBlQ9WJwV1UfIpBEGNF1aE7hFlYPHg kKAbO/eTVpYbKZXD9/ee/CEXSRtvKSkswiXyqfM3wOwH2fIWJsCaOOOzjPDVOllEjgAidskbOX4CC F+neht1g==; Received: from [10.20.10.232] (port=20890 helo=mail.cyon.ch) by s065.cyon.net with esmtpa (Exim 4.94.2) (envelope-from ) id 1mZvxu-00AYRG-6N; Mon, 11 Oct 2021 16:07:42 +0200 Subject: Re: BUG #17141: SELECT LIMIT WITH TIES FOR UPDATE SKIP LOCKED returns wrong number of rows To: Tom Lane , Alvaro Herrera Cc: David Christensen , PostgreSQL mailing lists , Surafel Temesgen References: <202110022205.jwz6b5bdyja5@alvherre.pgsql> <3720264.1633213844@sss.pgh.pa.us> From: Emil Iggland Message-ID: Date: Mon, 11 Oct 2021 16:07:40 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <3720264.1633213844@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - s065.cyon.net X-AntiAbuse: Original Domain - lists.postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - iggland.com X-Get-Message-Sender-Via: s065.cyon.net: authenticated_id: emil@iggland.com X-Authenticated-Sender: s065.cyon.net: emil@iggland.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk To be it would seem that a "peek()" type function would do a good job also, you can always peek at the next row to determine if it is not a candidate, if it is the "look and lock"-it. Then you can keep both features. On 2021-10-03 00:30, Tom Lane wrote: > 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 >