From: Michael Fuhr <mike@fuhr.org>
To: Robert Perry <rlperry@lodestonetechnologies.com>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: Check before insert
Date: Tue, 15 Mar 2005 15:23:29 -0700
Message-ID: <20050315222329.GA67743@winnie.fuhr.org> (raw)
In-Reply-To: <55762d9369dc9fd5b85b8662ccdf8557@lodestonetechnologies.com>
References: <1110920863.42374e9f75aa1@sms.ed.ac.uk>
<55762d9369dc9fd5b85b8662ccdf8557@lodestonetechnologies.com>
On Tue, Mar 15, 2005 at 04:51:20PM -0500, Robert Perry wrote:
> Insert (protein_id, name)
> select 'P04667', 'Albumin'
> where
> not exists(select * from protein_table_name where protein_id =
> 'P04667')
This should work if concurrency isn't an issue. But if two concurrent
transactions execute the same statement, then they might both find
no existing row and thus both attempt the insert. In that case,
in the presence of a unique index, one of the inserts will succeed
and the other transaction will block pending the first transaction's
completion. If the first transaction rolls back then the second's
insert will succeed, but if the first transaction commits then the
second will fail with a duplicate key violation. A program should
therefore be prepared to handle this situation. In 8.0 and later
you could use a savepoint or a PL/pgSQL exception handler to recover
from the error without aborting the entire transaction.
--
Michael Fuhr
http://www.fuhr.org/~mfuhr/
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-interfaces@postgresql.org
Cc: mike@fuhr.org, rlperry@lodestonetechnologies.com
Subject: Re: Check before insert
In-Reply-To: <20050315222329.GA67743@winnie.fuhr.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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