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 1gzUVV-0008Qq-Hr for pgsql-general@arkaria.postgresql.org; Thu, 28 Feb 2019 22:50:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gzUVS-00030L-IP for pgsql-general@arkaria.postgresql.org; Thu, 28 Feb 2019 22:50:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gzUVS-0002sr-8K for pgsql-general@lists.postgresql.org; Thu, 28 Feb 2019 22:50:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gzUVB-0000tF-Uw for pgsql-general@postgresql.org; Thu, 28 Feb 2019 22:50:21 +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 x1SMo1nD001955; Thu, 28 Feb 2019 17:50:02 -0500 From: Tom Lane To: =?UTF-8?B?R3nFkXrFkSBQYXBw?= cc: pgsql-general@postgresql.org Subject: Re: race condition when checking uniqueness between two tables In-reply-to: References: Comments: In-reply-to =?UTF-8?B?R3nFkXrFkSBQYXBw?= message dated "Thu, 28 Feb 2019 22:45:31 +0100" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1953.1551394201.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Thu, 28 Feb 2019 17:50:01 -0500 Message-ID: <1954.1551394201@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk =?UTF-8?B?R3nFkXrFkSBQYXBw?= writes: > A periodic task moves rows form `available_jobs` to `assigned_jobs` > the jobs that has been assigned: > WITH jobs_to_assign AS ( > DELETE FROM available_jobs > USING v_assignable_jobs > WHERE available_jobs.id = v_assignable_jobs.id > RETURNING available_jobs.* > ) > INSERT > INTO assigned_jobs(id, url) > SELECT id, url > FROM jobs_to_assign > RETURNING *; > And a similar one that “unassigns” failed assigned jobs, that is, > pushes back rows from `assigned_jobs` to `available_jobs`. And it > sometimes fails with: > ERROR: duplicate key value violates unique constraint "jobs_pkey" > DETAIL: Key (id)=(1fd0626c-f953-3278-82a1-8e4320d28914) already > exists. What is v_assignable_jobs? If, as I suspect, it's a view involving these same tables, then likely your issue is that you aren't reading that view with suitable locking, so that sometimes it will return stale rows that describe no-longer-assignable jobs. DELETE doesn't have an option to apply FOR UPDATE to USING tables, AFAIR, but maybe you could fix it along this line: WITH jobs_to_assign AS ( SELECT id FROM v_assignable_jobs FOR UPDATE ), deleted_jobs AS ( DELETE FROM available_jobs USING jobs_to_assign WHERE available_jobs.id = jobs_to_assign.id RETURNING available_jobs.* ) INSERT ... about as before, but use deleted_jobs ... > * Each transaction is in READ_COMMITTED. Another line of thought is to use SERIALIZABLE mode and just retry the inevitable serialization failures. However, if there are a lot of concurrent processes doing this, you'd probably get so many serialization failures that it'd be quite inefficient. regards, tom lane