42P05ERRORTier 2 — Caution✅ HIGH confidenceduplicate prepared statement
What this means
SQLSTATE 42P05 is raised when PREPARE is called with a statement name that already exists in the current session. Prepared statement names must be unique within a session.
Why it happens
- 1PREPARE uses a name that was already used by a previous PREPARE call in the same session and not yet deallocated
How to reproduce
PREPARE with a name that is already prepared.
PREPARE my_query AS SELECT * FROM orders WHERE id = $1;
PREPARE my_query AS SELECT * FROM customers WHERE id = $1; -- duplicateFix 1: DEALLOCATE the existing prepared statement before re-preparing
When reusing a prepared statement name.
DEALLOCATE my_query;
PREPARE my_query AS SELECT * FROM customers WHERE id = $1;Why this works
DEALLOCATE removes the named prepared statement, freeing the name for reuse.
Fix 2: Use unique names or DEALLOCATE ALL before re-preparing
In application code that prepares statements on each connection.
DEALLOCATE ALL; -- clear all prepared statements in the sessionWhy this works
DEALLOCATE ALL removes all prepared statements in the session, useful for connection reset in connection pools.
Sources
📚 Official docs: https://www.postgresql.org/docs/current/errcodes-appendix.html
🔧 Source ref: Class 42 — Syntax Error or Access Rule Violation (Postgres-specific)
Confidence assessment
✅ HIGH confidence
Postgres-specific. Stable across all versions.
See also
🔗 Related errors
📄 Reference pages