pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mario Splivalo <mario.splivalo@mobart.hr>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-sql@postgresql.org
Subject: Re: 'locking' the SELECTs based on indices...
Date: Thu, 23 Feb 2006 11:13:37 +0100
Message-ID: <1140689618.5002.9.camel@localhost.localdomain> (raw)
In-Reply-To: <14024.1140634704@sss.pgh.pa.us>
References: <1140631350.14229.8.camel@localhost.localdomain>
	<14024.1140634704@sss.pgh.pa.us>

On Wed, 2006-02-22 at 13:58 -0500, Tom Lane wrote:
> Mario Splivalo <mario.splivalo@mobart.hr> 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




view thread (7+ messages)  latest in thread

Message-ID: <1140689618.5002.9.camel@localhost.localdomain>
Permalink:  ../1140689618.5002.9.camel@localhost.localdomain/
Also on:    postgresql.org/message-id/1140689618.5002.9.camel@localhost.localdomain

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: mario.splivalo@mobart.hr, tgl@sss.pgh.pa.us
  Subject: Re: 'locking' the SELECTs based on indices...
  In-Reply-To: <1140689618.5002.9.camel@localhost.localdomain>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox