Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17
currval and lastval return the value that nextval produced in your own session. They are not a way to read the latest value in the table. If the current session never called nextval (directly or through an INSERT that used the default), PostgreSQL raises 55000 with the message currval of sequence "…" is not yet defined in this session, or lastval is not yet defined in this session.
This typically happens when an app inserts in one connection and asks for the new id in another, for example with a connection pool.
01Getting the new id with currval in a different session than the INSERT
A row was inserted into orders (id serial) in one session. A new session then ran SELECT currval('orders_id_seq') and got 55000, even though last_value of orders_id_seq was 1.
The fix is to return the id from the INSERT itself: INSERT INTO orders (item) VALUES ('book') RETURNING id returned 2.
Setup
CREATE TABLE orders (id serial PRIMARY KEY, item text);
INSERT INTO orders (item) VALUES ('pen');CREATE TABLE
INSERT 0 1SELECT currval('orders_id_seq');ERROR: 55000: currval of sequence "orders_id_seq" is not yet defined in this sessionSELECT pg_get_serial_sequence('orders', 'id') AS seq, last_value FROM orders_id_seq; seq | last_value
---------------------+------------
lab_a.orders_id_seq | 1
(1 row)RETURNING gives the id of the row you just inserted in the same statement, so it does not depend on which connection runs the next query.
INSERT INTO orders (item) VALUES ('book') RETURNING id; id
----
2
(1 row)
INSERT 0 102lastval() before any nextval in the session (IDENTITY column)
logs has an id bigint GENERATED ALWAYS AS IDENTITY column, and a new session ran SELECT lastval() before inserting anything. It failed with 55000: lastval is not yet defined in this session.
pg_get_serial_sequence('logs', 'id') showed that the identity column uses the sequence lab_a.logs_id_seq. After an INSERT INTO logs in the same session, SELECT lastval() returned 1.
Setup
CREATE TABLE logs (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, msg text);CREATE TABLESELECT lastval();ERROR: 55000: lastval is not yet defined in this sessionSELECT pg_get_serial_sequence('logs', 'id') AS seq; seq
-------------------
lab_a.logs_id_seq
(1 row)INSERT INTO logs (msg) VALUES ('start');
SELECT lastval();INSERT 0 1
lastval
---------
1
(1 row)03Reading currval to see the latest number
invoice_no_seq (START 1000) already returned 1000 from nextval in another session. A new session ran SELECT currval('invoice_no_seq') and got 55000.
Reading the sequence like a table, SELECT last_value, is_called FROM invoice_no_seq, showed 1000 and t without an error. In the session that needs currval, calling nextval first (1001) made currval return 1001.
Setup
CREATE SEQUENCE invoice_no_seq START 1000;
SELECT nextval('invoice_no_seq');CREATE SEQUENCE
nextval
---------
1000
(1 row)SELECT currval('invoice_no_seq');ERROR: 55000: currval of sequence "invoice_no_seq" is not yet defined in this sessionSELECT last_value, is_called FROM invoice_no_seq; last_value | is_called
------------+-----------
1000 | t
(1 row)To just look at the last issued value, select last_value from the sequence. currval is only for the session that called nextval.
SELECT nextval('invoice_no_seq');
SELECT currval('invoice_no_seq'); nextval
---------
1001
(1 row)
currval
---------
1001
(1 row)FAQ
Why is currval not yet defined in this session after an INSERT?
The INSERT ran in another session (connection). currval only knows the values nextval produced in the current session. In the test, currval failed in a new session even though the sequence was at 1.
How do I get the id of the inserted row in PostgreSQL?
Add RETURNING id to the INSERT. In the test, INSERT ... RETURNING id returned the new id 2 in one statement.
Does lastval work with IDENTITY columns?
Yes, after an insert in the same session. In the test, lastval() failed before any insert and returned 1 right after INSERT INTO logs.
How do I check the current value of a sequence without nextval?
Select from the sequence: SELECT last_value, is_called FROM invoice_no_seq returned 1000 and t without an error.
How we verify
Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.
lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12