Re: Referencing serial col's sequence for insert - Mailing list pgsql-general

From David G Johnston
Subject Re: Referencing serial col's sequence for insert
Date
Msg-id 1406039082942-5812382.post@n5.nabble.com
Whole thread Raw
In response to Re: Referencing serial col's sequence for insert  (Albe Laurenz <laurenz.albe@wien.gv.at>)
Responses Re: Referencing serial col's sequence for insert  (Albe Laurenz <laurenz.albe@wien.gv.at>)
List pgsql-general
Albe Laurenz *EXTERN* 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).

David J.



--
View this message in context:
http://postgresql.1045698.n5.nabble.com/Referencing-serial-col-s-sequence-for-insert-tp5812225p5812382.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Adrian Klaver
Date:
Subject: Re: Need r_constraint_name
Next
From: rob stone
Date:
Subject: Re: Referencing serial col's sequence for insert