From roba@bml.uk.com Tue Mar 23 16:54:56 2004 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 5CDC8D1EC4B for ; Tue, 23 Mar 2004 16:54:56 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 56474-10 for ; Tue, 23 Mar 2004 12:54:53 -0400 (AST) Received: from bml.uk.com (unknown [195.60.21.19]) by svr1.postgresql.org (Postfix) with ESMTP id 48275D1EC4A for ; Tue, 23 Mar 2004 12:54:50 -0400 (AST) Received: from D4R74F0J [129.1.2.162] by bml.uk.com [127.0.0.1] with SMTP (MDaemon.v2.8.5.0.R) for ; Tue, 23 Mar 2004 16:47:39 +0000 From: "Rob Anderson" To: Subject: Testing for existence of a record before insert Date: Tue, 23 Mar 2004 16:41:40 -0000 Message-ID: MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_004E_01C410F5.B7AA74D0" X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Importance: Normal X-MDaemon-Deliver-To: pgsql-admin@postgresql.org X-Return-Path: ROBA@bml.uk.com X-Virus-Scanned: by amavisd-new at postgresql.org X-Spam-Status: No, hits=0.2 tagged_above=0.0 required=5.0 tests=BAYES_44, HTML_50_60, HTML_MESSAGE X-Spam-Level: X-Archive-Number: 200403/300 X-Sequence-Number: 12869 This is a multi-part message in MIME format. ------=_NextPart_000_004E_01C410F5.B7AA74D0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit I am trying to find a way of performing something similar to the SQL-Server SQL of IF NOT EXISTS (SELECT id FROM a_table WHERE id='akey') THEN INSERT INTO a_table (id, name) values ('akey', 'adetails'); --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.619 / Virus Database: 398 - Release Date: 10/03/2004 ------=_NextPart_000_004E_01C410F5.B7AA74D0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable
I am try= ing to find=20 a way of performing something similar to the SQL-Server SQL of=20
 
IF NOT E= XISTS=20 (SELECT id FROM a_table WHERE id=3D'akey')
THEN INS= ERT INTO=20 a_table (id, name) values ('akey', 'adetails');
 
 
------=_NextPart_000_004E_01C410F5.B7AA74D0-- From bruno@wolff.to Tue Mar 23 17:56:46 2004 X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 95430D1E91C for ; Tue, 23 Mar 2004 17:56:46 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 95611-06 for ; Tue, 23 Mar 2004 13:56:44 -0400 (AST) Received: from wolff.to (wolff.to [66.93.249.74]) by svr1.postgresql.org (Postfix) with SMTP id 1CD2FD1E414 for ; Tue, 23 Mar 2004 13:56:43 -0400 (AST) Received: (qmail 26819 invoked by uid 500); 23 Mar 2004 17:59:43 -0000 Date: Tue, 23 Mar 2004 11:59:43 -0600 From: Bruno Wolff III To: Rob Anderson Cc: pgsql-admin@postgresql.org Subject: Re: Testing for existence of a record before insert Message-ID: <20040323175943.GA26654@wolff.to> Mail-Followup-To: Rob Anderson , pgsql-admin@postgresql.org References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200403/301 X-Sequence-Number: 12870 On Tue, Mar 23, 2004 at 16:41:40 -0000, Rob Anderson wrote: > I am trying to find a way of performing something similar to the SQL-Server SQL of > > IF NOT EXISTS (SELECT id FROM a_table WHERE id='akey') > THEN INSERT INTO a_table (id, name) values ('akey', 'adetails'); INSERT INTO a_table (id, name) SELECT 'akey' as id, 'adetails' as name where not exists (SELECT id FROM a_table WHERE id='akey') It is still possible for two transactions to both try to insert the same value at the same time. You either need to be prepared to handle the occasional error or you need to get an exclusive lock on a_table.