pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Győző Papp <gyozo.papp@meltwater.com>
Cc: pgsql-general@postgresql.org
Subject: Re: race condition when checking uniqueness between two tables
Date: Thu, 28 Feb 2019 17:50:01 -0500
Message-ID: <1954.1551394201@sss.pgh.pa.us> (raw)
In-Reply-To: <CABxafcEVwb5kmYTH0Kf8pMtUQypk=ixMudt9r8_VN-XKd-i8vg@mail.gmail.com>
References: <CABxafcEVwb5kmYTH0Kf8pMtUQypk=ixMudt9r8_VN-XKd-i8vg@mail.gmail.com>

=?UTF-8?B?R3nFkXrFkSBQYXBw?= <gyozo.papp@meltwater.com> 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




view thread (2+ messages)

Message-ID: <1954.1551394201@sss.pgh.pa.us>
Permalink:  ../1954.1551394201@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1954.1551394201@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, gyozo.papp@meltwater.com
  Subject: Re: race condition when checking uniqueness between two tables
  In-Reply-To: <1954.1551394201@sss.pgh.pa.us>
  References: <CABxafcEVwb5kmYTH0Kf8pMtUQypk=ixMudt9r8_VN-XKd-i8vg@mail.gmail.com> <1954.1551394201@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox