X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 6AC5E9DC88B for ; Thu, 23 Feb 2006 06:15:34 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 13575-09 for ; Thu, 23 Feb 2006 06:15:36 -0400 (AST) Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 62B499DC8DE for ; Thu, 23 Feb 2006 06:15:32 -0400 (AST) Received: from ls405.htnet.hr (ls405.htnet.hr [195.29.150.97]) by svr4.postgresql.org (Postfix) with ESMTP id 0B3CB5AF031 for ; Thu, 23 Feb 2006 10:15:34 +0000 (GMT) Received: from ls422.t-com.hr (ls422.t-com.hr [195.29.150.237]) by ls405.htnet.hr (0.0.0/8.12.10) with ESMTP id k1NAFSdK022718; Thu, 23 Feb 2006 11:15:30 +0100 Received: from ls422.t-com.hr (localhost.localdomain [127.0.0.1]) by ls422.t-com.hr (Qmlai) with ESMTP id AEA54988047; Thu, 23 Feb 2006 11:15:28 +0100 (CET) X-Envelope-Sender: mario.splivalo@mobart.hr X-Envelope-Sender: mario.splivalo@mobart.hr Received: from ls422.t-com.hr (localhost.localdomain [127.0.0.1]) by ls422.t-com.hr (Qmlai) with ESMTP id AA6E698803F; Thu, 23 Feb 2006 11:15:28 +0100 (CET) Received: from 83-131-49-92.adsl.net.t-com.hr (83-131-49-92.adsl.net.t-com.hr [83.131.49.92]) by ls422.t-com.hr (Qmlai) with ESMTP id 5A2FE8B80C7; Thu, 23 Feb 2006 11:15:28 +0100 (CET) Subject: Re: 'locking' the SELECTs based on indices... From: Mario Splivalo Reply-To: mario.splivalo@mobart.hr To: Tom Lane Cc: pgsql-sql@postgresql.org In-Reply-To: <14024.1140634704@sss.pgh.pa.us> References: <1140631350.14229.8.camel@localhost.localdomain> <14024.1140634704@sss.pgh.pa.us> Content-Type: text/plain Organization: Mob-Art Date: Thu, 23 Feb 2006 11:13:37 +0100 Message-Id: <1140689618.5002.9.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.4.1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.15 required=5 tests=[AWL=0.150] X-Spam-Score: 0.15 X-Spam-Level: X-Archive-Number: 200602/277 X-Sequence-Number: 24287 On Wed, 2006-02-22 at 13:58 -0500, Tom Lane wrote: > Mario Splivalo writes: > > Now, when I do this from one connection (psql shell, for instance): > > > [A]BEGIN TRANSACTION; > > [A]SELECT * FROM bla WHERE code_id = 1 FOR UPDATE; > > > and then, from another psql i do: > > [B]SELECT * FROM bla WHERE code_id = 1 FOR UPDATE > > > the second SELECT will wait untill I rollback or commit first > > transaction. That is cool. > > > But, if I do second SELECT like this: > > > [C]SELECT * FROM bla WHERE code_id = 2 FOR UPDATE > > > I will get the rows. > > Well, of course. Why would you want something different? Why do you > think the table's indexes should have anything to do with it? > > If you want a full-table lock then some form of LOCK TABLE seems like > the proper answer. SELECT FOR UPDATE is designed to lock the specified > rows, no more. Hm. But, is there a way to have just a part of table locked, for instance: BEGIN; SELECT * FROM bla WHERE code_id = 1 AND code_value = 'abla' FOR UPDATE and then, in second connection: BEGIN; SELECT * FROM bla WHERE code_id = 1 AND code_value = 'eble' FOR UPDATE I'd like to have second SELECT to hold. Now I'm doing it like this: First connection: BEGIN; SELECT * FROM bla WHERE code_id = 1 FOR UPDATE -- this is just for lock SELECT * FROM bla WHERE code_id = 1 AND code_value = 'abla' FOR UPDATE ... Second connection: BEGIN; SELECT * FROM bla WHERE code_id = 1 FOR UPDATE -- here it'll wait SELECT * FROM bla WHERE code_id = 1 AND code_value = 'eble' FOR UPDATE Since I'm actually doing this inside of a function, i'll use PERFORM for the first select. The problem is that SELECT COUNT(*) FROM bla WHERE code_id = 1 will return some 10M rows (on live data, my test data has some 100k rows, and I don't see any performance impact - yet). Is this a right way to go? > > If I erase the index bla_idx1, then [C] select will wait, same as [B] > > select will wait. > > I don't think so. If it does, it's a bug; please provide a reproducible > test case. > It's not a bug. My high fever must have something to do with it. I just tried it, removed the index, and [C] isn't waiting. Mike