Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 82B81B5DBEA for ; Tue, 6 Sep 2011 16:05:55 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 08182-04 for ; Tue, 6 Sep 2011 19:05:48 +0000 (UTC) X-Greylist: delayed 00:15:01.499828 by SQLgrey-1.7.6 Received: from vsmtp3.jaring.my (vsmtp3A.jaring.my [192.228.250.249]) by mail.postgresql.org (Postfix) with ESMTP id 433E8B5DBD1 for ; Tue, 6 Sep 2011 16:05:47 -0300 (ADT) DKIM-Signature: v=1; a=rsa-sha256; d=pop.jaring.my; s=mail; c=relaxed/simple; q=dns/txt; i=@pop.jaring.my; t=1315334941; x=1346870941; h=From:Sender:Reply-To:Subject:Date:To:Cc:Mime-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To: Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=GX3r5Q3L0wpBrvHLG7Wt8fWCSb7WiAcEOIOLCZJXA2k=; b=KOtYnVNC/ZASpVL4y/LeHwDw3MPnP/WT7AEn6ohYrficg4FUjejTu/ijYzfJf+T5 iBReuFBGPGIpn6tBz723FfoD4D5ZOKH4LJ0AJTgBRQ5Rz67iIuH3Lwv5OQIp8f1+ aq95RqCOFKB0v+KSdKBkKAkjELICeQTBvy2ABQyn2j4=; X-AuditID: c0e4faf8-b7b16ae000004da9-f6-4e666b1c4eda Received: from core.localhost. ( [60.50.247.155]) by vsmtp3.jaring.my (JARING SMTP READY) with SMTP id 8B.0D.19881.C1B666E4; Wed, 7 Sep 2011 02:49:00 +0800 (MYT) X-Mailer: QUALCOMM Windows Eudora Version 7.1.0.9 Date: Wed, 07 Sep 2011 02:50:29 +0800 To: jhondius@rem.nl,Pau Marc =?iso-8859-1?Q?Mu=F1oz?= Torres , pgsql-general@postgresql.org From: Lincoln Yeoh Subject: Re: conditional insert In-Reply-To: <4E64AC45.6030704@rem.nl> References: <4E64A79E.7030503@compulab.co.il> <4E64AC45.6030704@rem.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrIIsWRmVeSWpSXmKPExsViY/R9tq5MdpqfwfTXShZP/wpbXG18x24x /e5WNgdmj52z7rJ7XNg3jd3jZNsi5gDmKC6blNSczLLUIn27BK6MrfvXsRVM5q3ouhjUwDiD q4uRk0NCwETi2qlZbBC2mMSFe+uBbC4OIYEljBJnF7czdjFyACUMJbYvYAGpYRFQlTj3Zg8T SFhEoFji8Yw8kDCbgIbE6lvXwcYIC2hKzJ33ihHE5hRQk9hw9jIjxMj9TBIv589jA+nlFRCU +LtDGMRkFrCWWP8oawIjzyyExCyExAJGplWMAmXFuSUFxnpZiUWZeel6uZWbGIGhceDJrx87 GOd26h5iFOBgVOLhnRCY5ifEmlhWXJl7iFGSg0lJlPdOFlCILyk/pTIjsTgjvqg0J7X4EKME B7OSCK8WP1CONyWxsiq1KB8mJc3BoiTOy12Q6ickkJ5YkpqdmlqQWgSTleHgUJLgXQUyVLAo NT21Ii0zpwQhzcTBCTKcB2j4PZAa3uKCxNzizHSI/ClGXY6Nd5YcZxRiycvPS5US510NUiQA UpRRmgc3BxbTrxjFgd4S5n0AUsUDTAdwk14BLWECWnIoPxVkSUkiQkqqgbHcxHjOPN/i9Vev 7Lh+0rNz2+cPAgqLZ+66u0Ms58LXwmit3wzdBt1M3d+Zn55a6vJfTTHJdM2z1dW3RALaA5pb /z70DyjsDtv5wXHqoZ8nFxbwbnaVW2rTLGq40SRIuPTzqkczakWT/EzzOGcV6rB6X5HdtWtZ 78cVLiYlrKYfWB0WWp5nX6vEUpyRaKjFXFScCABQq5jkxAIAAA== Message-Id: <20110906190548.433E8B5DBD1@mail.postgresql.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.404 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/155 X-Sequence-Number: 179513 At 07:02 PM 9/5/2011, J. Hondius wrote: >I agree that there are better ways to do this. >But for me this works. (legacy driven situation) > >INSERT INTO tbinitialisatie (col1, col2) > SELECT 'x', 'y' > FROM tbinitialisatie > WHERE not exists (select * from tbinitialisatie where col1 = 'x' > and col2 = 'y') > LIMIT 1 Hi, That does not work 100%. Try it with two psql instances. Do: *** psql #1 begin; INSERT INTO tbinitialisatie (col1, col2) SELECT 'x', 'y' FROM tbinitialisatie WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2 = 'y') LIMIT 1 ; *** psql #2 begin; INSERT INTO tbinitialisatie (col1, col2) SELECT 'x', 'y' FROM tbinitialisatie WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2 = 'y') LIMIT 1 ; commit; *** psql #1 commit; You should find duplicate inserts. In most cases the "begin" and "commit" are very close together so you won't notice the problem. But one day you might get unlucky. Your options are: a) put a unique constraint and handle the insert errors when they occur b) lock the entire table first (affects performance: blocks all selects on that table) c) use a lock elsewhere (but this requires all applications using the database to cooperate and use the lock). d) wait for SQL MERGE to be implemented ( but from what I see the current proposal seems to require a) or b) anyway: http://wiki.postgresql.org/wiki/SQL_MERGE ) You could do both a) and b) too. Or both a) and c) (if you don't want insert errors in the cooperating apps and want to allow other selects during the transaction). Regards, Link.