How to fix currval is not yet defined in this session in PostgreSQL
This error occurs when you call the currval() function before calling nextval() on a sequence in the current PostgreSQL session. The currval() function returns the last value generated by nextval() within the same session, and it requires that nextval() has been called at least once first.
Quick fix
Fixes 55000
SELECT nextval('my_sequence');
SELECT currval('my_sequence');- Call nextval() before currval() in the same session
- Use RETURNING clause instead of currval() for INSERT statements
- Use lastval() as an alternative to currval()
- Ensure nextval() and currval() are in the same transaction
SELECT nextval('my_sequence');SELECT currval('my_sequence');currval is not yet defined in this session
What this error means
PostgreSQL throws the "currval is not yet defined in this session" error when you attempt to retrieve the current value of a sequence using the currval() function without having first initialized that sequence with a nextval() call in the current session. The currval() function is session-specific and session-scoped. It returns the most recent value obtained from the sequence by nextval() in the current session only. If nextval() has never been called on that sequence within the current session, currval() has no value to return, resulting in this error. This error exists to prevent developers from accidentally referencing sequence values from other sessions or mistakenly assuming a sequence has been initialized.
Typical symptoms
- Application crashes with "Object not in prerequisite state: 55000" error when trying to get the last inserted ID
- ORM or database drivers fail when using currval() immediately after a sequence operation
- Database initialization or seeding scripts fail on currval() calls in new database connections
- SQL scripts that work in one session fail in another session or transaction
- New connections or transactions fail with this error even though the sequence exists
Common causes
- Calling currval() before any nextval() call in the current session
- Assuming currval() will return values from another session or previous connection
- Using currval() with multiple sequences and only initializing one of them with nextval()
- Running a SQL script across multiple separate transactions or sessions without ensuring nextval() is called first
- ORM or application code trying to retrieve a last-inserted ID without properly calling the sequence first
- Creating a new database connection and immediately trying to use currval() from a previous connection's sequence state
How to fix "currval is not yet defined in this session"
Always initialize the sequence with nextval() before attempting to retrieve its current value with currval():
SELECT nextval('my_sequence');
SELECT currval('my_sequence');The nextval() call initializes the sequence in the current session, allowing currval() to return the generated value.
For most use cases, the RETURNING clause is more reliable and avoids this error entirely:
INSERT INTO my_table (column1, column2)
VALUES ('value1', 'value2')
RETURNING id;This directly returns the generated ID without requiring a separate currval() call, and it works in all sessions and transactions.
If you want the last sequence value from the current session without specifying a sequence name, use lastval():
SELECT nextval('my_sequence');
SELECT lastval();lastval() returns the most recent nextval() result in the current session regardless of which sequence was used. It does not require the sequence name and works across multiple sequences.
If you are using transactions, make sure both nextval() and currval() calls are in the same transaction block:
BEGIN;
SELECT nextval('my_sequence');
SELECT currval('my_sequence');
COMMIT;Keeping them in the same transaction ensures the sequence state persists throughout the operation. Avoid committing between nextval() and currval() calls.
If you are using an ORM or application framework, update your code to:
- Use RETURNING clauses for INSERT statements (Prisma, SQLAlchemy, TypeORM all support this)
- Call nextval() explicitly before currval() if you must use currval()
- Use lastval() if the sequence name is unknown at query time
- Ensure sequence calls are in the same database session
Example with Prisma:
const user = await prisma.user.create({
data: { email: 'user@example.com' },
select: { id: true }
});
// id is directly available, no need for currval()Verify that you are not opening a new database connection or session after calling nextval(). Each PostgreSQL session maintains its own sequence state, so if you:
1. Connect to database (Session A)
2. Call nextval()
3. Close connection
4. Open new connection (Session B)
5. Call currval()
You will get this error because Session B has no currval() state. Always use the same connection/session for related sequence operations.
Advanced notes
The sequence state in PostgreSQL is intentionally session-local for concurrency safety. This design prevents race conditions where one session's nextval() calls could interfere with another session's currval() retrieval.
If you need to share sequence values across sessions or store the last-inserted ID for later retrieval, store the value explicitly in a table or application variable rather than relying on currval().
For bulk inserts across transactions, consider using DEFAULT values with RETURNING to get all generated IDs at once rather than calling currval() multiple times.
In connection pooling scenarios (common in web applications), each pooled connection is a separate session. Always use RETURNING for multi-connection architectures rather than currval().
Frequently asked questions
- What does "55000" mean?
- PostgreSQL throws the "currval is not yet defined in this session" error when you attempt to retrieve the current value of a sequence using the currval() function without having first initialized that sequence with a nextval() call in the current session. The currval() function is session-specific and session-scoped. It returns the most recent value obtained from the sequence by nextval() in the current session only.
- How do I fix "55000"?
- This error occurs when you call the currval() function before calling nextval() on a sequence in the current PostgreSQL session. The currval() function returns the last value generated by nextval() within the same session, and it requires that nextval() has been called at least once first.
- What causes "55000"?
- Calling currval() before any nextval() call in the current session Assuming currval() will return values from another session or previous connection Using currval() with multiple sequences and only initializing one of them with nextval()
- Is the "55000" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐PostgreSQL Official Documentation
- ๐PostgreSQL Sequence Manipulation Functions(Official Docs)
- ๐PostgreSQL Error Codes Reference(Official Docs)