Options to retrieve the current (on a moment of running query) sequence value

后端 未结 2 1380
夕颜
夕颜 2021-02-01 01:00

How is it possible to get the current sequence value in postgresql 8.4?

Note: I need the value for the some sort of statistics, just retrieve and store. Nothing related

2条回答
  •  无人共我
    2021-02-01 01:26

    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).

提交回复
热议问题