Search Postgresql Archives

Re: Referencing serial col's sequence for insert

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am a bit confused -which one comes first?

1) the 'data'||currval('id01_col1_seq') is parsed first : which means it takes the current session's currval
2) then the insert is attempted which causes a sequence.nextval to be performed which means that 'data'||currval('id01_col1_seq')will be different from the sequence's value

or

1) an insert is attempted which causes a sequence.nextval to be performed and then
2) the cols are parsed for the insert so the 'data'||currval('id01_col1_seq') has the correct value

I observe the latter on my single session notebook instance of postgres.


Regards,
AK


On Tue, Jul 22, 2014 at 10:53 PM, Albe Laurenz <laurenz.albe@xxxxxxxxxx> wrote:
David G Johnston wrote:
>> Also, I think that your method is vulnerable to race conditions:
>> If somebody else increments the sequence between the INSERT and
>> "SELECT lastval()" you'd get a wrong value.
>
> Uh, no.  It returns that last value issued in the same session - which is
> race-proof.
>
> http://www.postgresql.org/docs/9.3/static/functions-sequence.html
>
> Both of them are useful and in the case of inserting multiple rows you have
> to use RETURNING.  Beyond though it is largely personal preference and
> ease-of-use (dealing with a set when you know a single value is all that is
> necessary can be annoying).

You are right, I mixed it up with "currval".

Yours,
Laurenz Albe

--
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux