Options to retrieve the current (on a moment of running query) sequence value
You may use:
SELECT last_value FROM sequence_name;
Update: this is documented in the CREATE SEQUENCE statement:
Although you cannot update a sequence directly, you can use a query like:
SELECT * FROM name;
to examine the parameters and current state of a sequence. In particular, the last_value field of the sequence shows the last value allocated by any session. (Of course, this value might be obsolete by the time it's printed, if other sessions are actively doing nextval calls.)
If the sequence is being used for unique ids in a table, you can simply do this:
select max(id) from mytable;
The most efficient way, although postgres specific, is:
select currval('mysequence');
although technically this returns the last value generated by the call to nextval('mysequence')
, which may not necessarily be used by the caller (and if unused would leave gaps in an auto increments id column).