X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 8859A11F602E for ; Tue, 18 Apr 2006 12:33:16 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0.127 X-Spam-Level: X-Spam-Status: No, score=0.127 required=5 tests=[AWL=0.127] Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 7b4BlWlUXtrX for ; Tue, 18 Apr 2006 12:33:11 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id 4EEBA11F605A for ; Tue, 18 Apr 2006 12:33:10 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k3IFX658013610; Tue, 18 Apr 2006 11:33:06 -0400 (EDT) To: mario.splivalo@mobart.hr cc: pgsql-performance@postgreSQL.org Subject: Re: SELECT FOR UPDATE performance is bad In-reply-to: <1145371662.31555.10.camel@localhost.localdomain> References: <1145365720.18469.35.camel@localhost.localdomain> <13037.1145370658@sss.pgh.pa.us> <1145371662.31555.10.camel@localhost.localdomain> Comments: In-reply-to Mario Splivalo message dated "Tue, 18 Apr 2006 16:47:42 +0200" Date: Tue, 18 Apr 2006 11:33:06 -0400 Message-ID: <13609.1145374386@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200604/376 X-Sequence-Number: 18460 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. > 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. regards, tom lane