Re: psql sequence question

From: Bruno Wolff III <bruno(at)wolff(dot)to>
To: Jodi Kanter <jkanter(at)virginia(dot)edu>
Cc: Postgres Admin List <pgsql-admin(at)postgresql(dot)org>
Subject: Re: psql sequence question
Date: 2003-06-16 16:04:10
Message-ID: 20030616160410.GA29057@wolff.to
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin

On Mon, Jun 16, 2003 at 11:24:28 -0400,
Jodi Kanter <jkanter(at)virginia(dot)edu> wrote:
> If I'm using transactions (not autocommit), are sequences atomic?

Yes.

> In other words, after inserting a record to a table that
> uses sequence A, am I guaranteed that select last_value on
> sequence A is atomic, and cannot be interfered with by other
> transactions using that same sequence? Sequence A is used by
> several tables.

Probably not in the way you mean. Every transaction is going to see
a consistant view of the sequence table. However transactions proceeding
in parallel may seem the same value for the last value. To make this
work you would need to use serializable mode to do any updates based
on the value of the last value or lock the table exclusively to prevent
concurrent updates. This defeats the function of sequences providing
unique values using light weight locking.

The right way to use sequences is to use nextval to get new values
and use currval to reuse the value you got from the latest call to
nextval in the same session.

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message Stephan Szabo 2003-06-16 17:14:36 Re: Error: Relation "pg_rel_check" does not exist
Previous Message Markus Bertheau 2003-06-16 15:55:03 how do I get rid of huge sorttemp files?