Re: Postgresql indices (was Re: lisp mail archive software)

John DeSoi <[email protected]> Mon, 7 Jul 2003 10:11:17 -0400
Newsgroups gmane.lisp.clump
Message-ID <[email protected]>
Dan,

On Monday, July 7, 2003, at 09:53 AM, Daniel Barlow wrote:

> Isn't that potentially racey?  What happens if another user does an
> insert between when you do yours and when you call currval?
>

No race issues here. You are guaranteed a consistent value for currval 
for your connection (e.g. you can call it several times in a row and 
even if others have inserted into the sequence, you will get the same 
value back until *your* connection does nextval).

See 
http://www.postgresql.org/docs/7.3/interactive/functions-sequence.html


> In fact I'd prefer it
> if postgres would give me the primary key value back instead of the
> oid, because that's what I really wanted.  Hmm.  Perhaps I should wrap
> the insert in a function that does this atomically, if such a thing is
> possible.
>

Yes, I think this is the way to go using currval. After you insert the 
object get the primary key and attach it to your instance. Can't be 
done only using INSERT, but there should not be any consistency issues.


Best,


John DeSoi, Ph.D.