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 1mE4d0-0004kD-1C for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Aug 2021 06:55:46 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mE4cy-0002ZW-P7 for pgsql-bugs@arkaria.postgresql.org; Thu, 12 Aug 2021 06:55:44 +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 1mE4Vb-0007tm-Bn for pgsql-bugs@lists.postgresql.org; Thu, 12 Aug 2021 06:48:07 +0000 Received: from mail-gateway-shared12.cyon.net ([194.126.200.65]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mE4VY-0005Uc-R8 for pgsql-bugs@lists.postgresql.org; Thu, 12 Aug 2021 06:48:06 +0000 Received: from s065.cyon.net ([149.126.4.74]) by mail-gateway-shared12.cyon.net with esmtpsa (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim) (envelope-from ) id 1mE4VT-0007kB-NA for pgsql-bugs@lists.postgresql.org; Thu, 12 Aug 2021 08:48:01 +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=i/1IAHFUlBaMx4XPiY9KOTav7d7DL+COwFJt3SGiSVU=; b=pbN8tpaGA3SR18P2dJVReODvS4 qCzsTsXetJn7uthBa4AxlnMgKnR5wol33IvzyEIxJWygkjlQ94gGanEptGGK9Vjix+5U6i2Rtpd9b nX9j/FOUvUKaIjP9Wh5UkNMlmr9pmAIjkpNO5EJrMuezmQ4eIgO+Knavj8Hzzsd1TG7N8KxfmAACl YYNDlBjT2I6fOKfwtkAKxCn4jY5xAiKrzZjJAuaJjRKlOizsqSW0j9xSw+/jZpOkrgr7+yiL6RRbv qc9/g7DwrzTejE+VdrgqG/WT7vM+sLy3V2VX0YItWf8pDXDwQaGQ/vrkP/5k/r4Gl+hZ505HOi+Q1 JfsIal8A==; Received: from [10.20.10.231] (port=30752 helo=mail.cyon.ch) by s065.cyon.net with esmtpa (Exim 4.94.2) (envelope-from ) id 1mE4VS-00GMZh-9q; Thu, 12 Aug 2021 08:47:58 +0200 Subject: Re: BUG #17141: SELECT LIMIT WITH TIES FOR UPDATE SKIP LOCKED returns wrong number of rows To: David Christensen , Alvaro Herrera Cc: pgsql-bugs@lists.postgresql.org, Surafel Temesgen References: <17141-913d78b9675aac8e@postgresql.org> <202108112007.evylbmzueapg@alvherre.pgsql> From: Emil Iggland Message-ID: <58fd3519-8866-daed-0ca5-768f5bab1c00@iggland.com> Date: Thu, 12 Aug 2021 08:47:56 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0 MIME-Version: 1.0 In-Reply-To: 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 I continued trying the variations in the linked thread. * Sub-query BEGIN; SELECT * FROM (SELECT * FROM queue ORDER BY task DESC FETCH NEXT 1 ROWS WITH TIES) t FOR UPDATE SKIP LOCKED; COMMIT; This behaves the same way, this does not work around the bug. The same goes for my previous "find" with NEXT. I can not replicate the working state, I must have done something wrong last night. I added some more tasks with the same number in order to see if there was a problem with the first row, or with the count. I now have the following counts: task count(*) 180 2 280 2 380 4 480 3 580 2 I attempted to select multiple tasks at the same time, representing a case where a worker might select multiple tasks. SELECT * FROM queue ORDER BY task DESC FETCH FIRST 3 ROWS WITH TIES FOR UPDATE SKIP LOCKED; Here I get three rows back (580), (580), (480) If I run SELECT * FROM queue ORDER BY task DESC FETCH FIRST 3 ROWS WITH TIES; I get back 5 rows (580), (580), (480), (480), (480) as expected. /Emil On 2021-08-11 23:39, David Christensen wrote: > > Alvaro Herrera writes: > >> On 2021-Aug-11, PG Bug reporting form wrote: >> >>> BEGIN; >>> SELECT * FROM queue >>> ORDER BY task DESC >>> FETCH FIRST 1 ROWS WITH TIES >>> FOR UPDATE SKIP LOCKED; >>> /* Some work to be done here */ >>> COMMIT; >>> >>> select version(); >>> PostgreSQL 13.3, compiled by Visual C++ build 1914, 64-bit >>> >>> Expected result Worker 1: (580), (580), Actual result Worker 1: (580), (580) >>> Expected result Worker 2: (480), (480), Actual result Worker 2: (480) >> >> Ouch, we already saw this actually: >> https://postgr.es/m/16676-fd62c3c835880da6@postgresql.org >> The problem is that the first worker locks the first (480) row (even >> though it does not return it), so the second worker skips it due to SKIP >> LOCKED. >> >> I have this on my list of things to look at, but it's not at the top >> yet sadly ... > > Yeah, I'd looked at this when I found it, and short of detecting the situation "WITH TIES FOR UPDATE > SKIP LOCKED" and erroring out, it seems like it would require adding in infrastructure that we don't > support (AFAIK) with unlocking an already locked row inside a transaction or reworking the order of > LockRows and Limit such that Limit comes first (and itself handles the WITH TIES) before handing to > LockRows. Either way (other than the error), it seems to be a fairly invasive change. > > If someone has another idea on how to handle this, I could take a stab at things. Detecting the > situation and erroring seems like the easiest way to handle so you're at least not getting back bad > results, though I agree that the functionality would be useful if we *could* support it somehow. > > David >