Re: Get last generated serial sequence and set it up when explicit value is used - Mailing list pgsql-sql

From David G. Johnston
Subject Re: Get last generated serial sequence and set it up when explicit value is used
Date
Msg-id CAKFQuwY6oxAPp+QFsK2No5q6984rNWaBy-rQ3nRQJ2gf4_etPw@mail.gmail.com
Whole thread Raw
In response to Re: Get last generated serial sequence and set it up when explicit value is used  (Sebastien FLAESCH <sf@4js.com>)
Responses Re: Get last generated serial sequence and set it up when explicit value is used  (Sebastien FLAESCH <sf@4js.com>)
List pgsql-sql
On Friday, November 20, 2020, Sebastien FLAESCH <sf@4js.com> wrote:
Is there any way to avoid the error produced by currval()?


No
 
Ideally, currval() should return zero when no serial was produced yet.

I’d accept null, zero is a valid value.
 

Is it possible to write that in a simple SQL expression so it can be used in
the RETURNING clause of my INSERTs ?

Not that I can think of.  Maybe as the docs suggest, just do an unconditional setval()?  You might be able to combine that with a non-default isolation level (guessing here) to get close-enough behavior.  You are fighting the existing design of the feature, looking for an in-between position of fast-and-concurrent (existing) and fully serialized (where this would be mostly trivial to implement).  I don’t know of such a method.

David J.

pgsql-sql by date:

Previous
From: Sebastien FLAESCH
Date:
Subject: Re: Get last generated serial sequence and set it up when explicit value is used
Next
From: Sebastien FLAESCH
Date:
Subject: Re: Get last generated serial sequence and set it up when explicit value is used