Re: Conditional INSERT: if not exists

From: Stuart Bishop <stuart(at)stuartbishop(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: Conditional INSERT: if not exists
Date: 2006-10-05 07:07:05
Message-ID: 4524AF19.2040206@stuartbishop.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Don Morrison wrote:

> My problem: if the insert fails because the value already exists, then
> this starts a rollback of my entire transaction. The solution I'm
> trying is to create a nested transaction with a savepoint right before
> the insert, thus catching the rollback with the nested
> transaction...I'm not sure the nested transaction is necessary...maybe
> just the savepoint. Example:

You need to:

SAVEPOINT insert_may_fail
try:
INSERT INTO ...
RELEASE SAVEPOINT
except:
ROLLBACK TO SAVEPOINT
UPDATE ...

You can catch the exception either in Python, or if you are using a ZSQL
method, using <dtml-try>.

--
Stuart Bishop <stuart(at)stuartbishop(dot)net>
http://www.stuartbishop.net/

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Andreas Andreakis 2006-10-05 08:13:34 problems with left outer join
Previous Message Tom Lane 2006-10-05 04:11:29 Re: Interface of the R-tree in order to work with postgresql