Re: How can I prevent duplicate inserts by multiple concurrent threads ?

From: Hursh Jain <hurshjain(at)beesell(dot)com>
To: "pgsql-novice(at)postgresql(dot)org" <pgsql-novice(at)postgresql(dot)org>
Subject: Re: How can I prevent duplicate inserts by multiple concurrent threads ?
Date: 2015-01-07 20:01:42
Message-ID: 54AD90A6.9090408@beesell.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Sean Davis wrote:
>
> Any suggestions, tips ? Am I at least thinking along the right lines ?
> Should I be getting a table lock instead ? What is the recommended way
> to prevent duplicate inserts in a concurrent situation like this ?
>
>
> Is there a reason that you cannot use a UNIQUE constraint?
>

Right, maybe that's what I should be doing. The reason I didn't do that
initially was because creating new data can be *very* expensive. We have
about a 100 different chunks and if say 10 threads hit the database and
each thread re-creates 100 chunks, we will end up trying to save 100*10
(1000) chunks, 900 of which will be rightly rejected because of the
uniqueness constraint, so that time spent in creating those chunks would
have been wasted to the annoyance of the clients.

It would be nice to see if that data already exists before we try to
re-insert it....but how ? the "select for update" doesn't seem to be
working as I hoped..

Best,
--j

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Daniel Staal 2015-01-07 20:31:20 Re: How can I prevent duplicate inserts by multiple concurrent threads ?
Previous Message Sean Davis 2015-01-07 19:35:24 Re: How can I prevent duplicate inserts by multiple concurrent threads ?