PostgreSQL Error 42P05: Duplicate Prepared Statement
In this article
PostgreSQL error 42P05 means you tried to prepare a statement with a name that already exists in the current session. The fix is to either use a unique name for each prepared statement, deallocate the existing one before re-preparing, or switch to the extended query protocol that handles naming automatically. In most cases, the error comes from application code or a driver that reuses a fixed statement name across multiple prepare calls.
What You'll Need
- Access to a PostgreSQL database with a user that can run
PREPAREandDEALLOCATE(or the ability to change application code). - A SQL client such as
psqlor any tool that lets you run ad-hoc queries. - Knowledge of the driver or ORM your application uses (e.g., JDBC, Npgsql, psycopg2) if the error appears in logs rather than in manual SQL.
Step-by-Step
- Confirm the error and its context. Run the failing statement manually in
psqlor your SQL client. If you seeERROR: prepared statement "name" already exists, note the statement name. If the error only appears in application logs, search the code forPREPAREor for driver settings that enable server-side prepared statements. - Check existing prepared statements in the session. In
psql, runSELECT name, statement FROM pg_prepared_statements;. This lists all prepared statements visible to the current session. If the name from the error appears, you have found the conflict. - Deallocate the existing statement if you no longer need it. Run
DEALLOCATE name;(replacenamewith the actual statement name). To remove all prepared statements in the session, runDEALLOCATE ALL;. This frees the name so you can prepare a new statement with the same name. - Use unique names for each prepared statement. If your code prepares multiple statements, generate a unique name per statement, for example by appending a counter or a UUID. In SQL:
PREPARE stmt_001 AS SELECT ...;. In application code, use a variable that increments or a library that handles naming. - Adjust driver or ORM settings to avoid fixed names. Many drivers use server-side prepared statements with automatically generated names. If you see the error repeatedly, check the driver documentation for options like
prepareThreshold(JDBC) orprepared_statement_cache_size(Npgsql). Setting these to 0 disables server-side prepared statements, which avoids the error but may affect performance. Alternatively, ensure your connection pool does not reuse statement names across logical sessions. - Reconnect if needed. Prepared statements are session-scoped. Closing and reopening the connection clears them. In
psql, use\cto reconnect to the same database; in application code, return the connection to the pool or open a new one. - Test the fix. Re-run the original statement or the application workflow that produced the error. Confirm that the error no longer appears and that queries return expected results.
Common Mistakes
- Assuming prepared statements are global. They are per session. A statement prepared in one connection is not visible in another, so the error only occurs when the same session tries to reuse a name.
- Forgetting to deallocate before re-preparing. If your code prepares a statement with the same name on every call without deallocating, the second call fails. Always deallocate or use unique names.
- Using
DEALLOCATE ALLin production without caution. This removes all prepared statements in the session, which can break other parts of the application that rely on them. Prefer deallocating specific names. - Confusing prepared statements with temporary tables. They are different objects; deallocating a prepared statement does not affect tables or other schema objects.
- Ignoring connection pooling behavior. Some pools reset session state, others do not. If your pool does not reset prepared statements, names can persist across checkouts and cause conflicts.
Tips
- Use a naming convention that includes a unique identifier, such as
app_<module>_<counter>, to reduce collisions. - In development, enable driver logging to see the exact SQL and statement names being sent to the server.
- If you use an ORM, check whether it has a setting to disable server-side prepared statements; this is often the quickest workaround for legacy code.
- Remember that
DEALLOCATEonly affects the current session; other sessions with the same statement name are unaffected. - When writing migration scripts, avoid
PREPAREwith fixed names that might clash with application statements.
Troubleshooting
If the error persists after deallocating or renaming, check the following:
- Verify the session. Ensure you are deallocating in the same session where the error occurs. In
psql, use\conninfoto confirm the connection. In application code, log the backend PID withSELECT pg_backend_pid();before and after the failing statement. - Check for multiple prepares in a transaction. If a transaction rolls back, prepared statements created within it may still exist. Use
SELECT * FROM pg_prepared_statements;after rollback to confirm. - Review driver documentation. Some drivers automatically prepare statements and may not expose a direct deallocate. In that case, disable automatic preparation or upgrade the driver to a version with better handling.
- Look for connection pool misconfiguration. If your pool reuses connections without resetting session state, prepared statements can accumulate. Configure the pool to reset session state on return, or set a short
idle_in_transaction_session_timeoutto clean up idle sessions. - Test with a fresh connection. Open a new connection and run the same statement. If it works, the issue is session-specific and likely due to leftover prepared statements.
If none of these steps resolve the error, capture the full error message and the output of SELECT * FROM pg_prepared_statements; from the affected session, and consult your driver's support resources.
Frequently Asked Questions
What does error 42P05 mean?
It means a prepared statement with the specified name already exists in the current session. PostgreSQL requires unique names per session for prepared statements.
How do I list all prepared statements?
Run SELECT name, statement FROM pg_prepared_statements; in the session. This shows all prepared statements visible to that session.
Can I use the same prepared statement name in different sessions?
Yes. Prepared statements are scoped to a session, so two sessions can each have a statement with the same name without conflict.
What is the difference between DEALLOCATE and DEALLOCATE ALL?
DEALLOCATE name removes a specific prepared statement. DEALLOCATE ALL removes all prepared statements in the current session.
Why does my application get this error even though I don't use PREPARE explicitly?
Many database drivers and ORMs use the extended query protocol, which prepares statements automatically. If the driver reuses a fixed name without deallocating, the error occurs. Check your driver's settings for prepared statement caching.
Does reconnecting fix the error?
Yes, because prepared statements are session-scoped. Closing and reopening the connection clears all prepared statements for that session.