Re: Locking tables

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Allan Berger <alb2(at)cornell(dot)edu>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: Locking tables
Date: 2003-07-21 17:49:12
Message-ID: 20030721174912.GA13093@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

On Mon, Jul 21, 2003 at 12:00:45 -0500,
Allan Berger <alb2(at)cornell(dot)edu> wrote:
> Hi all,
>
> I have a genuine novice question. What's the best "postgres way" to
> lock tables in the following work flow circumstances:
>
> A)
> 1) Begin work;
> 2) select max(Id) from table;
> 3) insert into table record with Id=(max+1);
> 4) commit;
>
> I want to be absolutely certain no other user can run this identical
> query concurrently (read the same max(Id)) causing two identical
> records to be built with the same Id=(max+1) between steps 2 and 4.
> This would require locking the entire table with a "Lock table"
> statement between steps 1 and 2, yes? Best syntax?

If you just need uniqueness (e.g. there can be gaps in the ids), you
can use sequences to do this more efficiently.

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Stephan Szabo 2003-07-21 17:52:39 Re: Locking tables
Previous Message Allan Berger 2003-07-21 17:00:45 Locking tables