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 1mDtse-0006Ab-MY for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Aug 2021 19:27:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mDtsc-0008Et-3z for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Aug 2021 19:27:10 +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 1mDs2U-0002Dr-F2 for pgsql-bugs@lists.postgresql.org; Wed, 11 Aug 2021 17:29:14 +0000 Received: from mail-gateway-shared12.cyon.net ([194.126.200.65]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mDs2O-0000BO-Fc for pgsql-bugs@lists.postgresql.org; Wed, 11 Aug 2021 17:29:14 +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 1mDs2L-0007Tu-Lt for pgsql-bugs@lists.postgresql.org; Wed, 11 Aug 2021 19:29:07 +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:To:Subject:Sender:Reply-To:Cc: Content-ID:Content-Description; bh=qHsThXOO+zAgqjOU8asVyfmV4C6iN4gAK39Wd+qBtVo=; b=gQJB62rpDt285zxMMcqtG9QLrR whb6xAs6fk9U7pbo8yVQDC8H91yycxkTKZtOGYYLhPYiSbhNvad3MDDYA7KC3Cze7ppivX/aujsC3 19IxrsEAGU24BUhZMe18wPjysYRXysaYkVswxfSg3i+gExJuYUm1xZJuQA5IbmNhSDwbdrcWkjXpW vl5oB56eP1x6MgZbTQz2XDbfznxrMhMPtb5Si3hgp/PR5nxSSvd6wvQ3uQjNH/MfC18QNtzYXq584 AtMr8b3k08s/ReZWpiuXCieb30Uzs8KeAA6onaRlE9fXSDID2oLYMVdz0Pk3pXvJNv0lBG3NqUHFX HgjTa0Tw==; Received: from [10.20.10.231] (port=38788 helo=mail.cyon.ch) by s065.cyon.net with esmtpa (Exim 4.94.2) (envelope-from ) id 1mDs2K-00C003-Fz for pgsql-bugs@lists.postgresql.org; Wed, 11 Aug 2021 19:29:04 +0200 Subject: Re: BUG #17141: SELECT LIMIT WITH TIES FOR UPDATE SKIP LOCKED returns wrong number of rows To: pgsql-bugs@lists.postgresql.org References: <17141-913d78b9675aac8e@postgresql.org> From: Emil Iggland Message-ID: <88d7b60d-045e-27c5-a2f4-c669371128c5@iggland.com> Date: Wed, 11 Aug 2021 19:29:02 +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: <17141-913d78b9675aac8e@postgresql.org> 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 As does SELECT * FROM queue ORDER BY task DESC FETCH NEXT 1 ROWS WITH TIES FOR UPDATE SKIP LOCKED; Note the use of NEXT instead of FIRST On 2021-08-11 18:38, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 17141 > Logged by: Emil Iggland > Email address: emil@iggland.com > PostgreSQL version: 13.3 > Operating system: Windows > Description: > > I am trying to create a queue which should assign multiple tasks to a > worker. I use row locking with FOR UPDATE SKIP LOCKED, but the number of > rows returned are inconsistent with what I expect. > > Minimum example: > > CREATE TABLE queue (task INTEGER); > INSERT INTO queue (task) > VALUES (180),(280),(380),(480),(580),(180),(280),(380),(480),(580); > > 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) >