X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (mx2.hub.org [200.46.204.254]) by postgresql.org (Postfix) with ESMTP id 4698611F6C25 for ; Wed, 19 Apr 2006 05:20:50 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: -2.599 X-Spam-Level: X-Spam-Status: No, score=-2.599 required=5 tests=[BAYES_00=-2.599] Received: from postgresql.org ([200.46.204.71]) by localhost (mx2.hub.org [200.46.204.254]) (amavisd-new, port 10024) with ESMTP id cxs1YJ8lQgwC for ; Wed, 19 Apr 2006 05:20:48 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from pjetlic.mobart.hr (pjetlic.mobart.hr [80.80.51.10]) by postgresql.org (Postfix) with ESMTP id 9695111F6B6E for ; Wed, 19 Apr 2006 05:20:46 -0300 (ADT) Received: from localhost (localhost.localdomain [127.0.0.1]) by pjetlic.mobart.hr (Postfix) with ESMTP id DA86617A67 for ; Wed, 19 Apr 2006 10:20:45 +0200 (CEST) Received: from pjetlic.mobart.hr ([127.0.0.1]) by localhost (pjetlic [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 02792-04-5 for ; Wed, 19 Apr 2006 10:20:45 +0200 (CEST) Received: from mike.mobart.hr (mike.mobart.hr [192.168.10.94]) (using SSLv3 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by pjetlic.mobart.hr (Postfix) with ESMTP id C31A117A3C for ; Wed, 19 Apr 2006 10:20:45 +0200 (CEST) Subject: Re: SELECT FOR UPDATE performance is bad From: Mario Splivalo Reply-To: mario.splivalo@mobart.hr To: Pgsql performance In-Reply-To: <13609.1145374386@sss.pgh.pa.us> References: <1145365720.18469.35.camel@localhost.localdomain> <13037.1145370658@sss.pgh.pa.us> <1145371662.31555.10.camel@localhost.localdomain> <13609.1145374386@sss.pgh.pa.us> Content-Type: text/plain Organization: Mob-Art Date: Wed, 19 Apr 2006 10:20:43 +0200 Message-Id: <1145434844.4458.10.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by OpenSource AV+AS Mail Server X-Archive-Number: 200604/422 X-Sequence-Number: 18506 On Tue, 2006-04-18 at 11:33 -0400, Tom Lane wrote: > Mario Splivalo writes: > >> If there is concurrent locking, > >> you're also running a big risk of deadlock because two processes might > >> try to lock the same rows in different orders. > > > I think there is no risk of a deadlock, since that particular function > > is called from the middleware (functions are used as interface to the > > database), and the lock order is always the same. > > No, you don't even know what the order is, let alone that it's always > the same. You got me confused here! :) If I have just only one function that acts as a interface to the middleware, and all the operations on the database are done trough that one function, and I carefuly design that function so that I first grab the lock, and then do the stuff, aint I pretty sure that I won't be having any deadlocks? > > > Now, I just need to have serialization, I need to have clients 'line up' > > in order to perform something in the database. Actually, users are > > sending codes from the newspaper, beer-cans, Cola-cans, and stuff, and > > database needs to check has the code allready been played. Since the > > system is designed so that it could run multiple code-games (and then > > there similair code could exists for coke-game and beer-game), I'm using > > messages table to see what code-game (i.e. service) that particular code > > belongs. > > I'd suggest using a table that has exactly one row per "code-game", and > doing a SELECT FOR UPDATE on that row to establish the lock you need. > This need not have anything to do with the tables/rows you are actually > intending to update --- although obviously such a convention is pretty > fragile if you have updates coming from a variety of code. I think it's > reasonably safe when you're funneling all the operations through a bit > of middleware. I tend to design my applications so I don't have "flying SQL" in my java/python/c#/php/whereever code, all the database stuff is done trough the functions which are designed as interfaces. Those functions are also designed so they don't stop each other. So, since I need the serialization, I'll do as you suggested, using a lock-table with exactley one row per "code-game". Just one more question here, it has to do with postgres internals, but still I'd like to know why is postgres doing such huge i/o (in my log file I see a lot of messages that say "LOG: archived transaction log file" when performing that big FOR UPDATE. Mario -- Mario Splivalo Mob-Art mario.splivalo@mobart.hr "I can do it quick, I can do it cheap, I can do it well. Pick any two."