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

后端 未结 2 1289
夕颜
夕颜 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:31

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

提交回复
热议问题