Re: blocking INSERTs - Mailing list pgsql-general

From Douglas McNaught
Subject Re: blocking INSERTs
Date
Msg-id m2r7fd1pbg.fsf@Douglas-McNaughts-Powerbook.local
Whole thread Raw
In response to blocking INSERTs  (Joseph Shraibman <jks@selectacast.net>)
List pgsql-general
Joseph Shraibman <jks@selectacast.net> writes:

> I want to do the following:
>
> BEGIN;
> SELECT ... FROM table WHERE a = 1 FOR UPDATE;
> UPDATE table SET ... WHERE a = 1;
> if that resturns zero then
> INSERT INTO table (...) VALUES (...);
> END;
>
> The problem is that I need to avoid race conditions.  Sometimes I get
> primary key exceptions on the INSERT.
>
> I think I need to lock the table in share mode to keep inserts from
> happening, but that blocks vacuums, and blocked vacuums block other
> things behind them.  So how do I get around this?

Ummm...  Don't lock the table for very long?  :)  If you commit
your transaction promptly then the table will be unlocked again.

-Doug

pgsql-general by date:

Previous
From: Joseph Shraibman
Date:
Subject: blocking INSERTs
Next
From: Tom Lane
Date:
Subject: Re: return two elements