select open change scope Open full search

PG.CENTER connects PostgreSQL documentation, reference, and ecosystem knowledge. Maintained by Pigsty.

SQLSTATE / CLASS 42 · SYNTAX ERROR OR ACCESS RULE VIOLATION

duplicate_prepared_statement

SQLSTATE
42P05
Condition name
duplicate_prepared_statement
Class
Syntax Error or Access Rule Violation
Source macro
ERRCODE_DUPLICATE_PSTATEMENT
Evidence
Source path confirmed
Analysis & operational context

English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗

<h1>42P05 — Duplicate prepared statement</h1>

At a glance

42P05 (duplicate_prepared_statement) means the current backend/session already has a named prepared statement with that name. Pool reuse can expose this state from an earlier request, while a different connection can have a separate statement of the same name.

Meaning

StorePreparedStatement keeps named statements in the backend's prepared_queries hash. HASH_ENTER reports ERROR prepared statement "%s" already exists when the name is already present, before the new plan is saved. DEALLOCATE name removes one entry and DEALLOCATE ALL removes the current backend's entries; these operations do not remove a statement owned by another connection.

Diagnosis

Check the physical connection/backend identity, pg_prepared_statements, pool checkout history, and the PREPARE path. Distinguish a named server statement from the unnamed protocol statement and from a driver-local cache key. If two requests used the same pool connection, inspect the exact name and parameter types before changing the SQL.

Response

Reuse the existing statement only when its SQL and parameter types match. Otherwise coordinate DEALLOCATE name with the owner of that connection, use a unique deterministic name, or reset the pool session under an explicit lifecycle policy. Do not issue DEALLOCATE on an unrelated connection and assume it cleared the collision. Recover an explicit transaction after this ERROR with a savepoint rollback or transaction rollback; reconnecting is a controlled reset, not a universal fix.

Messages

The confirmed 18.6 variant is explicit ERROR: prepared statement "%s" already exists. %s is the dynamic server-side name.

Versions

From 7.4 through 18.6 and 19beta3; that catalogue range is not a precise introduction claim. The selected 18.6 source covers insertion and current-backend deallocation, and runtime was not run.

Sources

  • src.errcodes.42P05.18.6src/backend/utils/errcodes.txt line 381, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba (source).

  • src.call.42P05.b490336894ec11628c1766f2src/backend/commands/prepare.c lines 412-415, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956 (source).

  • src.call.42P05.lifecyclesrc/backend/commands/prepare.c lines 401-423 and 504-511, fixed at 724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256 e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956 (source).
  • src.calls.REL_18_6.42P05 — resolved core call groups; SHA-256 9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf.
  • authored evidence — source claims, message roles, and runtime boundary.

Source evidence

Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.

42P05 is the duplicate_prepared_statement condition in Class 42.

Method: Read fixed REL_18_6 errcodes.txt and locked facts.

Limits: Identity does not enumerate all source branches or client failures.

src.errcodes.42P05.18.6

StorePreparedStatement inserts named statements into the current backend's prepared_queries hash and reports ERROR `prepared statement "%s" already exists` when HASH_ENTER finds the name. DEALLOCATE removes entries from that backend's hash; another connection has independent state.

Method: Read complete resolved REL_18_6 report groups and message roles.

Limits: Representative confirmed core paths only; not exhaustive historical or extension coverage.

src.call.42P05.b490336894ec11628c1766f2 src.call.42P05.lifecycle

From 7.4 through 18.6 and 19beta3; this catalogue range is not a precise introduction claim. The selected 18.6 source covers insertion and current-backend deallocation, and runtime was not run.

Method: Read locked catalogue facts and definition snapshots.

Limits: First observed release is a lower bound, not precise behavioral introduction.

src.errcodes.42P05.18.6

Message templates

explicit ERROR · message.variant-1

Primary

prepared statement "%s" already exists

Placeholders remain dynamic source fields; no concrete runtime value is claimed.

Reproduction & repair cases

No reproduction case is attached to this condition.

Recorded runtime evidence

No runtime observation is attached to this entry.

Definition snapshot: english-manuals:f1e61c1d28b8bfd0308750d15d6… · English manual source