Re: Referencing serial col's sequence for insert

From: Albe Laurenz <laurenz(dot)albe(at)wien(dot)gv(dot)at>
To: "David G Johnston *EXTERN*" <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Referencing serial col's sequence for insert
Date: 2014-07-22 14:53:16
Message-ID: A737B7A37273E048B164557ADEF4A58B17D1EFB8@ntex2010i.host.magwien.gv.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2014-07-22 14:53:50 Re: Fwd: Need r_constraint_name
Previous Message David G Johnston 2014-07-22 14:49:42 Re: Referencing serial col's sequence for insert