Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id A71D62E0033 for ; Tue, 10 Jun 2008 17:46:42 -0300 (ADT) Received: from developer.postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 98684-01 for ; Tue, 10 Jun 2008 17:46:36 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mongonet.net (mailgate.mongonet.net [64.157.94.58]) by developer.postgresql.org (Postfix) with ESMTP id 63C882E002E for ; Tue, 10 Jun 2008 17:46:39 -0300 (ADT) Received: from [192.168.1.121] (account dbhandary [192.168.1.121] verified) by mongonet.net (CommuniGate Pro SMTP 5.1.12) with ESMTPSA id 3857706; Tue, 10 Jun 2008 13:46:36 -0700 Message-ID: <484EE821.6090700@mongonet.net> Date: Tue, 10 Jun 2008 13:46:25 -0700 From: Dinesh User-Agent: Thunderbird 2.0.0.14 (Windows/20080421) MIME-Version: 1.0 To: Scott Marlowe CC: pgsql-admin@postgresql.org Subject: Re: error handling unique key constraint in pgsql References: <484EB14B.4010003@mongonet.net> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/83 X-Sequence-Number: 29421 Hi Scott, Thanks for your reply. Is there a built in exception in pl/pgsql ( similar to oracle's |DUP_VAL_ON_INDEX ) that I can use? I ran into an unique situation a couple of days ago. Procedure that inserts a new value into unique index column was called at the same time. I even check if the value exist before inserting, but one of the instance of the function gave me an unique constraint violation error. My code looks like this: select into var * from table; if not found then insert into table values (a); end if; | I would imagine the postgres db would inherently handle situation like this, but it did not. Thanks. Dinesh Scott Marlowe wrote: > On Tue, Jun 10, 2008 at 10:52 AM, Dinesh wrote: > >> Hi All, >> >> I was wondering if there is an easy way to handle unique key constraint in >> pgsql. Right now my entire transaction aborts if it tries to insert >> duplicate keys, but I would like to handle this >> scenario in such a way that the transaction will not break. >> >> Please let me know if you have done something like this. I greatly >> appreciate your help. >> > > There are a couple of approaches: > > process the inserts in a pl/pgsql function and use the exception > handling in there to catch them. > > Use savepoints. > > Both of these use the same underlying mechanisms, and both tend to be > expensive in terms of overhead. > >