Contents
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
- PostgreSQL 6.1
- added (commit e8647c45)
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)
References
- PostgreSQL documentation: Sequence Functions