pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Dan Langille <dan@langille.org>
To: Nicolas Nolst <nnolst@hotmail.com>
Cc: pgsql-admin@postgresql.org
Cc: pgsql-general@postgresql.org
Subject: Re: [GENERAL] performance issue using DBI
Date: Thu, 6 Jun 2002 12:33:54 -0400
Message-ID: <20020606163524.D685F3F28@bast.unixathome.org> (raw)
In-Reply-To: <1023380046.26291.2977.camel@linda>
References: <Pine.BSO.4.44.0206060832210.2703-100000@kitten.greentechnologist.org>

On 6 Jun 2002 at 17:14, Oliver Elphick wrote:

> On Thu, 2002-06-06 at 14:55, Joshua b. Jore wrote:u
> >       Don't use currval since some other process might alter the
> > sequence between the time you call nextval and currval. 
> 
> This is wrong.  currval() will always return the last serial assigned in
> *the*same*session*.  It is not affected by other users at all.

Folks, here's a short test which might help.  Note the BEGIN.

$ psql testing
testing=# create sequence test;
CREATE
testing=# select currval('test');
ERROR:  test.currval is not yet defined in this session
testing=# select setval('test', 1);
 setval
--------
      1
(1 row)

testing=# select currval('test');
 currval
---------
       1
(1 row)

testing=# select currval('test');
 currval
---------
       1
(1 row)

testing=# select currval('test');
 currval
---------
       1
(1 row)

testing=# select currval('test');
 currval
---------
       1
(1 row)

testing=#

Then, in another window, I did this:

$ psql testing
# select nextval('test');
 nextval
---------
       2
(1 row)

testing=# select nextval('test');
 nextval
---------
       3
(1 row)

testing=# select nextval('test');
 nextval
---------
       4
(1 row)

testing=#

Then back to the other window:

testing=# select currval('test');
 currval
---------
       1
(1 row)

testing=# select nextval('test');
 nextval
---------
       5
(1 row)

testing=#

cheers

FWIW: I always use nextval when looking for a new ID.
-- 
Dan Langille




view thread (9+ messages)  latest in thread

Message-ID: <20020606163524.D685F3F28@bast.unixathome.org>
Permalink:  ../20020606163524.D685F3F28@bast.unixathome.org/
Also on:    postgresql.org/message-id/20020606163524.D685F3F28@bast.unixathome.org

 · 

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: dan@langille.org, nnolst@hotmail.com, pgsql-general@postgresql.org
  Subject: Re: [GENERAL] performance issue using DBI
  In-Reply-To: <20020606163524.D685F3F28@bast.unixathome.org>
  References: <Pine.BSO.4.44.0206060832210.2703-100000@kitten.greentechnologist.org> <20020606163524.D685F3F28@bast.unixathome.org>

* 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