Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ihaKA-00074s-RP for pgsql-general@arkaria.postgresql.org; Wed, 18 Dec 2019 14:29:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ihaK9-0004C7-FG for pgsql-general@arkaria.postgresql.org; Wed, 18 Dec 2019 14:29:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ihaK9-0004C0-1i for pgsql-general@lists.postgresql.org; Wed, 18 Dec 2019 14:29:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ihaK6-0000ld-Qb for pgsql-general@lists.postgresql.org; Wed, 18 Dec 2019 14:29:11 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id xBIET4hB030296; Wed, 18 Dec 2019 09:29:04 -0500 From: Tom Lane To: Steven Winfield cc: Thomas Munro , "pgsql-general@lists.postgresql.org" Subject: Re: Row locks, SKIP LOCKED, and transactions In-reply-to: References: Comments: In-reply-to Steven Winfield message dated "Wed, 18 Dec 2019 11:33:07 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <30294.1576679344.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 18 Dec 2019 09:29:04 -0500 Message-ID: <30295.1576679344@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Steven Winfield writes: >> There are various ways you could deal with this, but I'd probably go fo= r a >> simple scheme where you only have to consult a single row to know if yo= u >> can claim it. You could still put the results into a separate table, b= ut >> use job.state to find work, and set it to DONE when you insert the resu= lt. >> It may also be possible to add no new columns but do a dummy update to = the >> job row to get the join qual rechecked, but I'm not sure if that'd work= . >> Another reason to add a state column to the job table is so that you ca= n >> put a conditional index on it so you can find jobs to be done very >> quickly, if you're not planning to remove the ones that are done. > Thanks. I rejected the idea of doing a dummy update to the locked row as= I wanted to avoid too much extra WAL - the real table originally had quit= e a few more columns than the toy example, but it's much slimmer now so th= is could be a viable option. Yeah ... the fundamental reason why this isn't working for you is that the FOR UPDATE will only lock/check conflicts in the "job" table. You could add a FOR UPDATE in the sub-select to lock the "result" table, but that will still only lock rows it read, not rows it didn't read because they weren't there yet :-(. Updating the state of the job row to show that it's claimed is much the most reliable way to fix this. (Or you could use serializable mode, but that feels like using a hammer to swat a fly.) regards, tom lane