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
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
currvalorlastvalin a session that hasn't callednextval, often because a connection pool sent theINSERTand theSELECT 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
-
Get generated ids from the
INSERTitself withRETURNING; it works on any connection and with any number of rows.INSERT INTO orders (customer_id) VALUES (42) RETURNING id;
-
If you must use
currval, call it on the same connection and in the same transaction as the insert. -
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.
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.