// PostgreSQL error reference

PostgreSQL 55000: object not in prerequisite state

The object isn't in the state the command needs, for example currval before nextval in a session, or a replication slot or table that isn't ready.

SQLSTATE
55000
Name
object_not_in_prerequisite_state
Category
Object Not In Prerequisite State
Error message
currval of sequence "orders_id_seq" is not yet defined in this session

What it means

55000 (object_not_in_prerequisite_state) means the command is valid but the object it acts on isn't ready for it. The most searched case is currval: it returns the value most recently produced by nextval in the current session, so calling it before any nextval (or on a pooled connection that didn't run the insert) fails.

Other messages with this code include lastval is not yet defined in this session and errors about replication slots, prepared transactions or extensions that are in the wrong state for the operation.

Common causes

  • Calling currval or lastval in a session that hasn't called nextval, often because a connection pool sent the INSERT and the SELECT currval(...) on different connections.
  • Reading the id of an inserted row with a separate query instead of RETURNING.
  • Operating on a replication slot that is in use, or a feature that needs a setting such as wal_level = logical.

How to fix it

  1. Get generated ids from the INSERT itself with RETURNING; it works on any connection and with any number of rows.

    INSERT INTO orders (customer_id) VALUES (42) RETURNING id;
  2. If you must use currval, call it on the same connection and in the same transaction as the insert.

  3. For other messages, read the full text: it names the state that is missing, such as a setting to change or a slot to release.

Reproduce it safely

CREATE TEMP SEQUENCE s;
SELECT currval('s');  -- ERROR: currval of sequence "s" is not yet defined in this session

Related errors

More object not in prerequisite state errors

The code and message text are from the PostgreSQL documentation, Appendix A: PostgreSQL Error Codes. Browse the full PostgreSQL error list or search every database error.

// fewer errors, faster fixes

Work on PostgreSQL in SQLly

A native SQL client for macOS, Windows and Linux with schema-aware IntelliSense, guardrails for risky statements and clear error messages, for PostgreSQL and the other databases you use.