Skip site navigation (1) Skip section navigation (2)

Re: Locking rows

From: "scott(dot)marlowe" <scott(dot)marlowe(at)ihs(dot)com>
To: Jean-Christian Imbeault <jc(at)mega-bucks(dot)co(dot)jp>
Cc: PostgreSQL-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Locking rows
Date: 2003-02-28 20:30:26
Message-ID: Pine.LNX.4.33.0302281328070.22448-100000@css120.ihs.com (view raw or flat)
Thread:
Lists: pgsql-general
On Fri, 28 Feb 2003, Jean-Christian Imbeault wrote:

> I have written an application that prints out data from a database. One 
> problem I am encountering is in the time it takes to generate the print 
> file the data may have changed.
> 
> I was thinking that a way around this would be to lock the rows that are 
> used to generate the print file. However I am not quite sure how to best 
> go about this.
> 
> Is locking the rows the best (or simplest) solution?
> 
> If locking is a good idea how could I lock and then unlock the rows 
> returned by this (somewhat ugly) query?
> 
> select prod_id, sum(quantity), case when (select stock_qty+cancelled_qty 
> from stock where pid=prod_id) is null then 0 else (select 
> stock_qty+cancelled_qty from stock where pid=prod_id) end as stock_qty 
> from invoice_li, invoices where invoices.id=invoice_id AND dist_id=1 AND 
> NOT invoices.cancelled AND NOT invoice_li.cancelled group by prod_id;
> 
> I will also be doing some other select queries between this one and the 
> printing (I need to get more information on each prod_id returned).
> 
> I've read the iDocs and Bruce's online manual but can seem to find any 
> info on the syntax for locking other that for SELECT FOR UPDATE. And 
> even then the docs don't state how to remove the lock created by a 
> SELECT FOR UPDATE.

No need.  Take a look here:

http://www.postgresql.org/docs/view.php?version=7.3&idoc=0&file=transaction-iso.html

And read up on serializable, which should do exactly what you want.  I.e. 
Even if people are changing the database underneath you, you'll see the 
database wholly as if it never changed since your begin; was issued.


In response to

  • Locking rows at 2003-02-28 10:26:55 from Jean-Christian Imbeault

pgsql-general by date

Next:From: scott.marloweDate: 2003-02-28 20:33:53
Subject: Re: Recreating a primary key
Previous:From: IvanDate: 2003-02-28 20:27:34
Subject: Re: Few Simple Questions

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group