Re: next integer in serial key

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: terry(at)ashtonwoodshomes(dot)com
Cc: olly(at)lfix(dot)co(dot)uk, 'Kenneth Gonsalves' <lawgon(at)thenilgiris(dot)com>, "'Postgresql Sql Group (E-mail)'" <pgsql-sql(at)postgresql(dot)org>
Subject: Re: next integer in serial key
Date: 2004-07-22 13:52:25
Message-ID: 20040722064825.Q98843@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, 22 Jul 2004 terry(at)ashtonwoodshomes(dot)com wrote:

> > > Alternatively, you can do:
> > > INSERT (accepting the default)
> > > then SELECT currval(the_sequence_object);
> > > then <extra inserts of related foreign key records>
> > >
> > > NOTE: 2nd method assumes that nobody else called nextval() on the
> > > sequence between when you did the
> > > insert and when you did the select currval(). Note that
> > being inside
> > > a transaction is NOT
> > > sufficient, you need an explicit lock on the sequence. I do not
> > > recommend the 2nd method, too much
> > > can go wrong.
> >
> > This last paragraph is wrong and irrelevant. It is a point which for
> > some reason is continually being misunderstood.
> >
> > currval() *always* returns the last value generated for the
> > sequence in
> > the *current session*. It is specifically designed to do what you are
> > suggesting without any conflict with other sessions. There is *never*
> > any risk of getting a value that nextval() returned to some
> > other user's
> > session.
>
> That statement depends on different factors. If you for example have an application server, and the
> database connection is shared across multiple application server clients (or the query results get
> cached by your application server, Ugh!), the statement IS valid: I encountered this issue 2 years
> ago with coldfusion 4.5 using the unixODBC driver against Postgres 7.1.1

If your application server will share your connection without your
explicit releasing of it, then yes, currval() is unsafe. So are
basically transactions, cursors, session variables and deferrable
constraints. That's not a valid platform to be using a database from
really.

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Oleg Konovalov 2004-07-22 13:54:16 PSQL Syntax errors running PL/SQL scripts
Previous Message terry 2004-07-22 13:38:51 Re: next integer in serial key