blocking INSERTs

From: Joseph Shraibman <jks(at)selectacast(dot)net>
To: Postgresql-General <pgsql-general(at)postgresql(dot)org>
Subject: blocking INSERTs
Date: 2005-06-08 03:31:54
Message-ID: 42A666AA.70608@selectacast.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Douglas McNaught 2005-06-08 03:48:35 Re: blocking INSERTs
Previous Message Steve Atkins 2005-06-08 03:29:58 Re: To SPAM or not to SPAM...