currval()

A function returning the current value in a sequence

currval() is a system function returning the current value in a sequence.

currval() was added in PostgreSQL 6.1.

Usage

currval ( regclass ) → bigint

currval() returns the value most recently obtained by nextval() for this sequence in the current session.

An error will be raised if nextval() has not yet been executed for this sequence in the current session.

Change history

Examples

Basic usage example for currval():

postgres=# CREATE SEQUENCE foo_seq;
CREATE SEQUENCE

postgres=# SELECT nextval('foo_seq');
 nextval
---------
       1
(1 row)

postgres=# SELECT currval('foo_seq');
 currval
---------
       1
(1 row)

If nextval() was not called in the current session, an error will be raised:

postgres=# SELECT currval('foo_seq');
ERROR:  currval of sequence "foo_seq" is not yet defined in this session

postgres=# SELECT nextval('foo_seq');
 nextval
---------
       2
(1 row)

postgres=# SELECT currval('foo_seq');
 currval
---------
       2
(1 row)

Categories

Sequences, System function

See also

lastval(), nextval(), setval()