currval()

Synopsis

Use the currval( sequence_name ) function to return the last value returned by the nextval( sequence_name ) function for the specified sequence in the current session.

Semantics

sequence_name

Specify the name of the sequence.

  • An error is raised if nextval( sequence_name ) has not been called for the specified sequence in the current session.

Examples

Create a sequence

yugabyte=# CREATE SEQUENCE s;
CREATE SEQUENCE

Call nextval().

yugabyte=# SELECT nextval('s');
 nextval
---------
       1
(1 row)
yugabyte=# SELECT currval('s');
 currval
---------
       1
(1 row)

Call currval() before nextval() is called.

yugabyte=# CREATE SEQUENCE s2;
CREATE SEQUENCE
SELECT currval('s2');
ERROR:  currval of sequence "s2" is not yet defined in this session

See also