Re: Re: Re: Rules, views, sequences and returned values

From: Doug McNaught <doug(at)wireboard(dot)com>
To: will trillich <will(at)serensoft(dot)com>
Cc: PostgreSQL-General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Re: Re: Rules, views, sequences and returned values
Date: 2001-03-23 22:19:18
Message-ID: m3y9tw163d.fsf@belphigor.mcnaught.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

will trillich <will(at)serensoft(dot)com> writes:

> On Fri, Mar 23, 2001 at 12:23:35PM -0500, Gregory Wood wrote:
> > > i'm sure there's a reason for this--
> > >
> > > psql=> select currval('mytable_afield_seq') ;
> > > ERROR: mytable_afield_seq.currval is not yet defined in this session
> > >
> > > but i'll be darned if i can figure out what it is. (after one
> > > call to "nextval()" currval() works fine, of course.) someone hit
> > > me with a clue stick!
> >
> > currval() is the value last used by the backend, not by the database.
> > Meaning that you have to actually use the sequence before the backend has a
> > value to retrieve. That means either doing a nextval() or doing an INSERT
> > (which implicitly performs the nextval() ).
>
> <dense mode=on>
> seems like the backend (server?) would know the current value of
> a sequence, since it has to get it, then add one to it, to get
> the next one in line. if george inserts a record, i'd think that
> ringo would be able to see the current counter afterwards,
> independent of session...

Remember there are (potentially) multiple backends, one per client
connection. Because of transaction isolation, there may be multiple
outstanding values of 'currval'. There's no way to know the "real"
current value in your transaction unless you do 'nextval' (which
atomically gets the next free value of the sequence).

Does this make sense? I probably didn't explain it very well.

-Doug

In response to

Browse pgsql-general by date

  From Date Subject
Next Message newsreader 2001-03-23 22:25:03 Re: HOWTO for pg 7.1 installation from cvs
Previous Message Matt Friedman 2001-03-23 22:18:48 ~* OR LIKE?