Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 9B094B5DBDA for ; Thu, 8 Sep 2011 11:14:23 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 84346-07 for ; Thu, 8 Sep 2011 14:14:17 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from vsmtp1.jaring.my (vsmtp1.jaring.my [192.228.250.213]) by mail.postgresql.org (Postfix) with ESMTP id 9BBA9B5DBCB for ; Thu, 8 Sep 2011 11:14:16 -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=1315491550; x=1347027550; 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=7xVfVUNivMZGcEa1YJ8KAYxcMCFhNCo/RmKX/XpSNmY=; b=fqSdw7GjN4bTtHdiFIWMO1mSLRF6+mUalquQDqUoUhv3xGQfydPKu3onEOfkJxEE BYE0+LJDCKdKGH9K8JcsauMhfPk6u5uArPXLccLHFMkCfu9nI46w1ilraX/jNi/P 1T6UngBWbZdv+Fpn3P2jRhw9EF8sd3e988PIOsHria0=; X-AuditID: c0e4fad5-b7b97ae000001094-60-4e68ceddb533 Received: from core.localhost. ( [60.50.247.155]) by vsmtp1.jaring.my (JARING SMTP READY) with SMTP id 59.94.04244.DDEC86E4; Thu, 8 Sep 2011 22:19:10 +0800 (MYT) X-Mailer: QUALCOMM Windows Eudora Version 7.1.0.9 Date: Thu, 08 Sep 2011 22:14:02 +0800 To: Merlin Moncure From: Lincoln Yeoh Subject: Re: conditional insert Cc: jhondius@rem.nl,Pau Marc =?iso-8859-1?Q?Mu=F1oz?= Torres , pgsql-general@postgresql.org In-Reply-To: References: <4E64A79E.7030503@compulab.co.il> <4E64AC45.6030704@rem.nl> <20110906190548.433E8B5DBD1@mail.postgresql.org> <4e679195.12848e0a.019e.ffff918eSMTPIN_ADDED@mx.google.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii"; format=flowed X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrMIsWRmVeSWpSXmKPExsViY/R9tu69cxl+Br9m81s8/StscafzI6PF 1cZ37BbT725lc2Dx2DnrLrvHhX3T2D1Oti1iDmCO4rJJSc3JLEst0rdL4MroXj+NtWA3V8WD TxNZGxjXcnQxcnJICJhIdP79yAhhi0lcuLeerYuRi0NIYAmjxLlpW1i6GDmAEoYS2xewgNSw CKhKXH98mhUkLCKgLtF5OA4kzCagIbH61nU2EFtYQFNi7rxXYCOZBYolPi1eyQxicwqESJxr PMAIMb6VVeLilAlsIHN4BQQl/u4QBjGZBawl1j/KmsDIOwshMQshsYCRaRWjQFlxbkmBoV5W YlFmXrpebuUmRmD4HHjy6+oOxlUNKocYBTgYlXh4N8xM9xNiTSwrrsw9xCjJwaQkynvnRIaf EF9SfkplRmJxRnxRaU5q8SFGCQ5mJRHeYJAcb0piZVVqUT5MSpqDRUmc9877ND8hgfTEktTs 1NSC1CKYrAwHh5IEr+xZoEbBotT01Iq0zJwShDQTByfIcB6g4VwgNbzFBYm5xZnpEPlTjLoc G+8sOc4oxJKXn5cqJc775AxQkQBIUUZpHtwcWNy/YhQHekuYlwdkFA8wZcBNegW0hAloyaH8 VJAlJYkIKakGRt7Hqf931d65m/Nai++a5bXvGkp7H/eemt0ydc7169tC1Lm3mqyWK2FymWkQ eXZRzG+fT2cqLlec5xNN2Wv+8NcFk+IzPL4nSpvuCfHVrmaVPCJdMpWHP59n98SoMI5Xly8G qL4ruVh9diVjV5ZDzjd225YP0tNPl2UwvXj+YKPor8ZQv1O9qkosxRmJhlrMRcWJAK44KgXW AgAA Message-Id: <20110908141416.9BBA9B5DBCB@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/226 X-Sequence-Number: 179584 At 03:51 AM 9/8/2011, Merlin Moncure wrote: > > Don't you have to block SELECTs so that the SELECTs get serialized? > > Otherwise concurrent SELECTs can occur at the same time, find no existing > > rows, then "all" the inserts proceed and you get errors (or dupes). > > > > That's how Postgresql still works right? I haven't really been keeping up. > >yeah -- but you only need to block selects if you are selecting in the >inserting transaction (this is not a full upsert). if both writers >are doing: >begin; >lock table foo exclusive; >insert into foo select ... where ...; >commit; > >is good enough. btw even if you are doing upsert pattern >(lock...select for update...insert/update), you'd be fine with >straight exclusive locks because the 'for update' lock takes a higher >lock that is blocked by exclusive. A basic rule of thumb is to try >and not fully block readers unless absolutely necessary...basically >maintenance operations. Oh wait, now I think I get it. "lock table foo exclusive" will block the inserts too, so I wouldn't get dupe errors even if other transactions "blindly" insert dupes at the same time. The other transactions might get the dupe errors, but mine won't as long as it selects first and only inserts if there are no rows at that point. Is that correct? Link.