Received: from localhost (unknown [200.46.204.183]) by developer.postgresql.org (Postfix) with ESMTP id CF8D62E002E for ; Tue, 10 Jun 2008 16:09:57 -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 97923-09 for ; Tue, 10 Jun 2008 16:09:52 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from qb-out-0506.google.com (qb-out-0506.google.com [72.14.204.226]) by developer.postgresql.org (Postfix) with ESMTP id 4069D2E0032 for ; Tue, 10 Jun 2008 16:09:54 -0300 (ADT) Received: by qb-out-0506.google.com with SMTP id f30so999666qba.7 for ; Tue, 10 Jun 2008 12:09:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=jEk6sXIbe8gl/FhVGeA7vtzsYUNY7hxpyYcJIwG4pKg=; b=cFqZwywwS16yvobpMaoZLVUxgLitNopPQxTEAIpztyMbRWp2dtzcdTqRBiilnnMzh5 oVlSBnG1cK+OxyAfsKv7cHmdPyTDPA824+sHiBqE7G+cNQCcUYzzxU5mUv31reDZWxQd mGph3vR82ETbFWgXB71HlyQaojafKHjS5Ro3k= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=IkkzrtubvKxdN9cSc8SmR6oHJjiVpsuVxYwC8QffD6bYFVW8TsUvYrj1Ca7sF8ghsI bunCVR8C6Wo9hOn2UFHl1eA+1lbjqk4kq9sp2X6G6LdxfNeWJW7D5ieRJrdxlu8Sk+xq RbZiU/M8rP+xaADl7vOnKb4gW63Zn+Uk2dfuo= Received: by 10.115.76.5 with SMTP id d5mr5435037wal.191.1213124992104; Tue, 10 Jun 2008 12:09:52 -0700 (PDT) Received: by 10.114.130.17 with HTTP; Tue, 10 Jun 2008 12:09:52 -0700 (PDT) Message-ID: Date: Tue, 10 Jun 2008 13:09:52 -0600 From: "Scott Marlowe" To: Dinesh Subject: Re: error handling unique key constraint in pgsql Cc: pgsql-admin@postgresql.org In-Reply-To: <484EB14B.4010003@mongonet.net> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: <484EB14B.4010003@mongonet.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200806/79 X-Sequence-Number: 29417 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.