PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 55000: currval of sequence is not yet defined in this session

currval or lastval was called in a session that has not run nextval on that sequence yet.

PostgreSQL 17.12
55000: currval of sequence "orders_id_seq" is not yet defined in this session

Jump to a cause

  1. 01Getting the new id with currval in a different session than the INSERT
  2. 02lastval() before any nextval in the session (IDENTITY column)
  3. 03Reading currval to see the latest number

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
Setuprun as lab_a
CREATE TABLE orders (id serial PRIMARY KEY, item text);
INSERT INTO orders (item) VALUES ('pen');
OUTPUT
CREATE TABLE
INSERT 0 1
Reproducerun as lab_a
SELECT currval('orders_id_seq');
✕ ERROR
ERROR:  55000: currval of sequence "orders_id_seq" is not yet defined in this session
Checkrun as lab_a
SELECT pg_get_serial_sequence('orders', 'id') AS seq, last_value FROM orders_id_seq;
OUTPUT
         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.

After the fixrun as lab_a
INSERT INTO orders (item) VALUES ('book') RETURNING id;
✓ SUCCESS
 id
----
  2
(1 row)

INSERT 0 1

02lastval() 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
Setuprun as lab_a
CREATE TABLE logs (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, msg text);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
SELECT lastval();
✕ ERROR
ERROR:  55000: lastval is not yet defined in this session
Checkrun as lab_a
SELECT pg_get_serial_sequence('logs', 'id') AS seq;
OUTPUT
        seq
-------------------
 lab_a.logs_id_seq
(1 row)
After the fixrun as lab_a
INSERT INTO logs (msg) VALUES ('start');
SELECT lastval();
✓ SUCCESS
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
Setuprun as lab_a
CREATE SEQUENCE invoice_no_seq START 1000;
SELECT nextval('invoice_no_seq');
OUTPUT
CREATE SEQUENCE
 nextval
---------
    1000
(1 row)
Reproducerun as lab_a
SELECT currval('invoice_no_seq');
✕ ERROR
ERROR:  55000: currval of sequence "invoice_no_seq" is not yet defined in this session
Checkrun as lab_a
SELECT last_value, is_called FROM invoice_no_seq;
OUTPUT
 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.

After the fixrun as lab_a
SELECT nextval('invoice_no_seq');
SELECT currval('invoice_no_seq');
✓ SUCCESS
 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