Re: Using curr_val Wisely - Mailing list pgsql-general

From Tom Lane
Subject Re: Using curr_val Wisely
Date
Msg-id 23599.963422577@sss.pgh.pa.us
Whole thread Raw
In response to RE: Using curr_val Wisely  ("Andrew Snow" <als@fl.net.au>)
List pgsql-general
"Andrew Snow" <als@fl.net.au> writes:
>> I'm using curr_val('blah_id_seq') in a transaction like so: (in
>> pseudo SQL)

> curr_val() returns the current value of the sequence, in the current
> transaction.

More precisely, currval() returns the same value returned by the most
recent nextval() on that sequence in the current backend --- transaction
boundaries don't matter AFAIR.

If you try currval() without ever having executed nextval() in the
current session, you will see this is so.

(I believe a setval() also causes currval() to become set, but that's
a special case that seldom matters.)

So you need not worry about other backends when using currval().  If you
have rules or triggers that can cause the sequence to get advanced, then
you could get confusing results if you forget these rules/triggers are
being fired.

            regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: [NOVICE] newbie problem on creating table
Next
From: Jeff Waugh
Date:
Subject: Re: Re: [INTERFACES] Re: Link to postgesql components