pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
race condition when checking uniqueness between two tables
2+ messages / 2 participants
[nested] [flat]

* race condition when checking uniqueness between two tables
@ 2019-02-28 21:45  Győző Papp <gyozo.papp@meltwater.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Győző Papp @ 2019-02-28 21:45 UTC (permalink / raw)
  To: pgsql-general

Hi all,

First and foremost I admit what follows is quite awkward and a bit
long. Sorry for both.

However I still would really like to understand what I am missing in
regards of transaction isolation levels and MVCC if CTE involved.

Given a sort of scheduler application with two tables:

CREATE TABLE available_jobs (id UUID PRIMARY KEY, url TEXT NOT NULL);
CREATE TABLE assigned_jobs (id UUID PRIMARY KEY, url TEXT NOT NULL);

New jobs are always inserted into `available_jobs` but only if its id
(primary key) does not exist in neither of the two tables, so for the
time being we used the INSERT below:

INSERT INTO available_jobs(id, url)
SELECT :id, :url
WHERE NOT EXISTS (
      SELECT id FROM assigned_jobs WHERE id = :id
    )
ON CONFLICT DO NOTHING

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.



^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: race condition when checking uniqueness between two tables
@ 2019-02-28 22:50  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Győző Papp <gyozo.papp@meltwater.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2019-02-28 22:50 UTC (permalink / raw)
  To: Győző Papp <gyozo.papp@meltwater.com>; +Cc: pgsql-general

=?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




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2019-02-28 22:50 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-02-28 21:45 race condition when checking uniqueness between two tables Győző Papp <gyozo.papp@meltwater.com>
2019-02-28 22:50 ` Tom Lane <tgl@sss.pgh.pa.us>

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