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 1mDvwP-0000vW-Ql for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Aug 2021 21:39:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mDvwO-0006oO-En for pgsql-bugs@arkaria.postgresql.org; Wed, 11 Aug 2021 21:39:12 +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 1mDvwO-0006oG-6e for pgsql-bugs@lists.postgresql.org; Wed, 11 Aug 2021 21:39:12 +0000 Received: from mail-oi1-x22b.google.com ([2607:f8b0:4864:20::22b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mDvwJ-0002I6-Eb for pgsql-bugs@lists.postgresql.org; Wed, 11 Aug 2021 21:39:11 +0000 Received: by mail-oi1-x22b.google.com with SMTP id y18so6835253oiv.3 for ; Wed, 11 Aug 2021 14:39:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crunchydata.com; s=google; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=hkiqQYvcZjhTrPZQhfsUXC0oRmkWDVptCelh5mT99kQ=; b=WLJ/yo2A0hZep5jLkAO2gFuQ9CZXZOPtxsftUYkcOpfx2NN2vM9SGhWcd8RD/eSxAO +D1ZqL5voiAM0qcCocu5tPhARv7m0zlwNgJ0JjW/5yeHjbUceMbPPkcQ/Do0KcGBveig HHRFFpOvtNINz464tZOBIubvW0R4pHDJclqAcNoCWODRU1uBlO94RsZJyw0jrRCZ1Fja ZHZ1wFJh2owph6e0rZC4EmIy1oH25EAErgUHAJ9xWQ1xNLSTBWOUhqIYCF+47bCZQKw5 PX3Tn5wlGoIXbW6dClBMtbIyGpV10yNGY9D6F5gJRdZJ5Xw1rduM5YwDsmB061VvuNx9 2lfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=hkiqQYvcZjhTrPZQhfsUXC0oRmkWDVptCelh5mT99kQ=; b=NKaXUFnuSdkhAnwWuGviyrwACrMBr9bp59NmCHCsZ7WgvGUQiDIlLmjp0ukbzRGuOo S8DOQjLAJvsiklRT8G0LqrTMx8iy//xMJaq60eMsrN/ByuCut3yw9hGwbv7gVjnfUtHu XP7uwRIg4M7/qF7r1DohH92FSTvUPiQYOp9GqQdEXJU/LxbzYrDBba5TaQ2B5gpxcK4I EGSWXcbwkGBMqEavq7Yuxm/Veq6fM1qthwL2fDf82Fy3t7pRa2OfglUjMyKHC9ohPZrr s8gdCzlegAFNTWuWmntVBMq9waSJG5KzSYweYka1YFzmJDItqioPghdNMlVN6lcYW8Az Gxlw== X-Gm-Message-State: AOAM5322leS/4zqRBRIKVvx51Ry/P20yDlENXAbT1mtFLXaURXwVYbUV AW1vJQr7SFTPh0rjb6dR2IyBVA== X-Google-Smtp-Source: ABdhPJwblYKUG18ukTtUu3/99fWfRfLRKgwe+Sk/oKmKgTdbwIluMZNegUl61C7FCGDxQS287rOKNg== X-Received: by 2002:aca:5301:: with SMTP id h1mr814020oib.70.1628717945453; Wed, 11 Aug 2021 14:39:05 -0700 (PDT) Received: from veeddrois.local (108-241-52-134.lightspeed.mssnks.sbcglobal.net. [108.241.52.134]) by smtp.gmail.com with ESMTPSA id v48sm107061ooj.3.2021.08.11.14.39.04 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 11 Aug 2021 14:39:05 -0700 (PDT) References: <17141-913d78b9675aac8e@postgresql.org> <202108112007.evylbmzueapg@alvherre.pgsql> User-agent: mu4e 1.4.15; emacs 27.1 From: David Christensen To: Alvaro Herrera Cc: emil@iggland.com, pgsql-bugs@lists.postgresql.org, Surafel Temesgen Subject: Re: BUG #17141: SELECT LIMIT WITH TIES FOR UPDATE SKIP LOCKED returns wrong number of rows In-reply-to: <202108112007.evylbmzueapg@alvherre.pgsql> Date: Wed, 11 Aug 2021 16:39:00 -0500 Message-ID: MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 --