Received: from maia.hub.org (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 2559B632A43 for ; Thu, 4 Feb 2010 03:30:35 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 06822-06 for ; Thu, 4 Feb 2010 07:30:21 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from 63.mail-out.ovh.net (63.mail-out.ovh.net [91.121.185.56]) by mail.postgresql.org (Postfix) with SMTP id 293B16323C2 for ; Thu, 4 Feb 2010 03:30:23 -0400 (AST) Received: (qmail 30333 invoked by uid 503); 4 Feb 2010 07:32:00 -0000 Received: from b7.ovh.net (HELO mail178.ha.ovh.net) (213.186.33.57) by 63.mail-out.ovh.net with SMTP; 4 Feb 2010 07:31:59 -0000 Received: from b0.ovh.net (HELO queueout) (213.186.33.50) by b0.ovh.net with SMTP; 4 Feb 2010 07:30:21 -0000 Received: from par69-8-88-161-102-87.fbx.proxad.net (HELO soyouz) (88.161.102.87) by ns0.ovh.net with SMTP; 4 Feb 2010 07:30:20 -0000 Date: Thu, 04 Feb 2010 08:32:43 +0100 To: wyx6fox@sina.com, pgsql-performance@postgresql.org Subject: Re: some problems when i use postgresql 8.4.2 in my projects . From: =?utf-8?Q?Pierre_Fr=C3=A9d=C3=A9ric_Caillau?= =?utf-8?Q?d?= Content-Type: text/plain; format=flowed; delsp=yes; charset=utf-8 MIME-Version: 1.0 References: <20100203022830.2A375358C60@mail175-236.sinamail.sina.com.cn> Content-Transfer-Encoding: 7bit Message-ID: In-Reply-To: <20100203022830.2A375358C60@mail175-236.sinamail.sina.com.cn> User-Agent: Opera Mail/9.62 (Linux) X-Ovh-Tracer-Id: 8614260188297954980 X-Ovh-Remote: 88.161.102.87 (par69-8-88-161-102-87.fbx.proxad.net) X-Ovh-Local: 213.186.33.20 (ns0.ovh.net) X-Spam-Check: DONE|U 0.5/N X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.158 tagged_above=-10 required=5 tests=AWL=-1.159, BAYES_50=0.001 X-Spam-Level: X-Archive-Number: 201002/76 X-Sequence-Number: 37497 > when concurrency insert violate the unique constraints , they block each > other , i test this in oracle10g, has the same behavour. I think this > may be reasonable because the uqniue check must be  the seriazable > check . > for resolve this problem , i do the unique check in application as > possible , but in big concurrency env , this is not good way . You probably can't do that in the application. About exclusive constraints : Transaction A : begin Transaction A : insert value X Transaction A : do some work, or just wait for client ... Meanwhile : Transaction B : begin Transaction B : insert same value X Transaction B : locked because A hasn't committed yet so the exclusive constraint can't be resolved Transaction A : commit or rollback Transaction B : lock is released, constraint is either OK or violated depending on txn A rollback/rommit. As you can see, the longer the transactions are, the more problems you get. Solution 1 : change design. - Why do you need this exclusive constraint ? - How are the unique ids generated ? - What kind of data do those ids represent ? - Can you sidestep it by using a sequence or something ? - Without knowing anything about your application, impossible to answer. Solution 2 : reduce the transaction time. - Optimize your queries (post here) - Commit as soon as possible - Long transactions (waiting for user input) are generally not such a good idea - Anything that makes the txn holding the locks wait more is bad (saturated network, slow app server, etc) - Optimize your xlog to make writes & commits faster Solution 3 : reduce the lock time Instead of doing : BEGIN INSERT X ... do some stuff ... COMMIT; do : BEGIN ... do some stuff that doesn't depend on X... INSERT X ... do less stuff while holding lock ... COMMIT; Solution 4 : If you have really no control over value "X" and you need a quick reply "is X already there ?", you can use 2 transactions. One transaction will "reserve" the value of X : - SELECT WHERE col = X ensures row and index are in cache whilst taking no locks) - Set autocommit to 1 - INSERT X; inserts X and commits immediately, else cause an error. Lock will not be held for long, since autocommit means it commits ASAP. - Perform the rest of your (long) operations in another transaction. This is a bit less safe since, if the second transaction fails, insert of X is not rolled back.