Re: blocking INSERTs

From: Douglas McNaught <doug(at)mcnaught(dot)org>
To: Joseph Shraibman <jks(at)selectacast(dot)net>
Cc: Postgresql-General <pgsql-general(at)postgresql(dot)org>
Subject: Re: blocking INSERTs
Date: 2005-06-08 03:48:35
Message-ID: m2r7fd1pbg.fsf@Douglas-McNaughts-Powerbook.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Joseph Shraibman <jks(at)selectacast(dot)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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Dinesh Pandey 2005-06-08 05:25:01 Postgres 8.0.1 configure failed
Previous Message Joseph Shraibman 2005-06-08 03:31:54 blocking INSERTs