Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id DD05CB5DBCB for ; Thu, 8 Sep 2011 11:07:52 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 57358-10 for ; Thu, 8 Sep 2011 14:07:46 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from vsmtp2.jaring.my (vsmtp2.jaring.my [192.228.250.215]) by mail.postgresql.org (Postfix) with ESMTP id 8B0CCB5DBE0 for ; Thu, 8 Sep 2011 11:07:44 -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=1315490850; x=1347026850; 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=sR+OGWX902gEfF69WKvHE5uYfp5SNDKtS42qL4om9iU=; b=kcHxYd9rxxRQ82aYGqPsfeW/Wx19cyqZ4w0msF/gbbI96XPsnQqvAalbtdDziKuz NInoYSybT8IP/I/SIPIyTNVflkYvKemKCBGfXadmYx9fSNipTImbifwklq/eUxEz gQOTYGePdqYMYWLvsmI+OCU0xHfbr3nj3vCu7l/DG0E=; X-AuditID: c0e4fad7-b7ce3ae000004d01-c1-4e68cc2007a5 Received: from core.localhost. ( [60.50.247.155]) by vsmtp2.jaring.my (JARING SMTP READY) with SMTP id 0C.69.19713.02CC86E4; Thu, 8 Sep 2011 22:07:28 +0800 (MYT) X-Mailer: QUALCOMM Windows Eudora Version 7.1.0.9 Date: Thu, 08 Sep 2011 22:07:20 +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+NgFjrIIsWRmVeSWpSXmKPExsViY/R9tq7imQw/g2vHzC2e/hW2uNP5kdHi auM7dovpd7eyObB47Jx1l93jwr5p7B4n2xYxBzBHcdmkpOZklqUW6dslcGX82nmKueAnV0Xj HrMGxgccXYycHBICJhJbrq5jhbDFJC7cW88GYgsJLGGUePwuoouRAyhuKLF9AQtImEVAVeL8 nB+sIGERAXWJzsNxIGE2AQ2J1beug3UKC2hKzJ33ihHEZhYolvi0eCUziM0pECJxrvEAUJwL aHorq8TFKRPYQObwCghK/N0hDGIyC1hLrH+UNYGRdxZCYhZCYgEj0ypGgbLi3JICI72sxKLM vHS93MpNjMDQOfDk1/UdjC0H1Q4xCnAwKvHwRkxP9xNiTSwrrsw9xCjJwaQkynvnRIafEF9S fkplRmJxRnxRaU5q8SFGCQ5mJRHeYJAcb0piZVVqUT5MSpqDRUmcd8+7ND8hgfTEktTs1NSC 1CKYrAwHh5IEb8lpoEbBotT01Iq0zJwShDQTByfIcB6g4TogNbzFBYm5xZnpEPlTjLocG+8s Oc4oxJKXn5cqJc7rClIkAFKUUZoHNwcW868YxYHeEuZNB6niAaYLuEmvgJYwAS05lJ8KsqQk ESEl1cCYMHfZg22uNnOe3T0jIKfMtP3vvOsXlfp1n1zbeaZNgL/89FyRe7uXuB82fZMW0dEk HqvxWXXf54UrPCseTuM1XvX4YpijsZbi7IUsMX/C+Sxep/Wo3RDhaxTX14xvSkoW/XHhlPJc 17PqV9z3XFo6Z233zD5zngILzZrLa+KKohu7W7mTNHmVWIozEg21mIuKEwH15HVs1AIAAA== Message-Id: <20110908140745.8B0CCB5DBE0@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/225 X-Sequence-Number: 179583 At 03:51 AM 9/8/2011, Merlin Moncure wrote: >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. Yeah it works if all the inserters do the lock table (or select for update), and provides better performance. But if you're paranoid and lazy - a full lock will ensure that your code won't get dupe errors even if someone else's code or manual control doesn't do the lock table (they might get the dupe errors[1], but that's their problem ;) ). So your code can safely assume that any DB errors that occur are those that deserve a full rollback of everything (which is what Postgresql "likes" by default). This means fewer scenarios to handle so you don't need to write as much code, nor document and support as much code ;). Link. [1] I'm assuming a unique constraint is present- the locking is to simplify things.