42P05 — Duplicate prepared statement
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.
English manual coverage begins with PostgreSQL 8.1. Source history and runtime observations have their own recorded version boundaries.
Reading PG 18·Select a recorded version
- aliases
- Unknown
- class name
- Syntax Error or Access Rule Violation
- condition name
- duplicate_prepared_statement
- lang
- en
- sqlstate
- 42P05
Definition PG 18
42P05 — Duplicate prepared statement
At a glance {#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 {#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 {#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 {#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 {#messages}
The confirmed 18.6 variant is explicit ERROR: prepared statement "%s" already exists. %s is the dynamic server-side name.
Versions {#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.
Related {#related}
Sources {#sources}
-
src.errcodes.42P05.18.6—src/backend/utils/errcodes.txtline 381, fixed at724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-2566e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba(source). -
src.call.42P05.b490336894ec11628c1766f2—src/backend/commands/prepare.clines 412-415, fixed at724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956(source). -
src.call.42P05.lifecycle—src/backend/commands/prepare.clines 401-423 and 504-511, fixed at724edf9bde9d356724ad384a2e196edc3c9f80f7; SHA-256e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956(source). -
src.calls.REL_18_6.42P05— resolved core call groups; SHA-2569ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf. -
authored evidence— source claims, message roles, and runtime boundary.
Compare versions
SQLSTATE evidence and recorded runtime versions
claims · identity.class-and-condition
{
"claim_id": "identity.class-and-condition",
"limits": "Identity does not enumerate all source branches or client failures.",
"method": "Read fixed REL_18_6 errcodes.txt and locked facts.",
"position": 0,
"runtime": [],
"sources": [
"src.errcodes.42P05.18.6"
],
"statement": "42P05 is the duplicate_prepared_statement condition in Class 42."
}claims · mechanism.selected-source-path
{
"claim_id": "mechanism.selected-source-path",
"limits": "Representative confirmed core paths only; not exhaustive historical or extension coverage.",
"method": "Read complete resolved REL_18_6 report groups and message roles.",
"position": 1,
"runtime": [],
"sources": [
"src.call.42P05.b490336894ec11628c1766f2",
"src.call.42P05.lifecycle"
],
"statement": "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."
}claims · versions.catalogue-boundary
{
"claim_id": "versions.catalogue-boundary",
"limits": "First observed release is a lower bound, not precise behavioral introduction.",
"method": "Read locked catalogue facts and definition snapshots.",
"position": 2,
"runtime": [],
"sources": [
"src.errcodes.42P05.18.6"
],
"statement": "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."
}messages · message.variant-1
{
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"message_id": "message.variant-1",
"path": "",
"position": 0,
"raw": {
"detail_templates": [],
"hint_templates": [],
"id": "message.variant-1",
"limits": "Placeholders remain dynamic source fields; no concrete runtime value is claimed.",
"primary_template": "prepared statement \"%s\" already exists",
"roles": [
{
"role": "errmsg",
"template": "prepared statement \"%s\" already exists"
}
],
"severity_source": "explicit ERROR",
"sources": [
"src.call.42P05.b490336894ec11628c1766f2"
],
"sqlstate": "42P05"
},
"severity": "explicit ERROR",
"sources": [
"src.call.42P05.b490336894ec11628c1766f2"
],
"templates": [
{
"kind": "primary",
"literal": "prepared statement \" \" already exists",
"position": 0,
"role": "",
"template": "prepared statement \"%s\" already exists"
}
]
}sources · src.errcodes.42P05.18.6
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "line 381",
"path": "src/backend/utils/errcodes.txt",
"position": 0,
"sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
"source_id": "src.errcodes.42P05.18.6",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L381"
}sources · src.call.42P05.b490336894ec11628c1766f2
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 412-415",
"path": "src/backend/commands/prepare.c",
"position": 1,
"sha256": "e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956",
"source_id": "src.call.42P05.b490336894ec11628c1766f2",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/prepare.c#L412-L415"
}sources · src.call.42P05.lifecycle
{
"commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
"docs_url": "",
"kind": "upstream_source",
"location": "lines 401-423 and 504-511",
"path": "src/backend/commands/prepare.c",
"position": 2,
"sha256": "e37fbd5f7618e5554561d9293d8c3af7cf3190c62c5c6bbceb3fe8b97be17956",
"source_id": "src.call.42P05.lifecycle",
"tag": "REL_18_6",
"url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/prepare.c#L401-L423"
}sources · src.calls.REL_18_6.42P05
{
"commit": "",
"docs_url": "",
"kind": "local_artifact",
"location": "",
"path": "raw/calls/REL_18_6.jsonl",
"position": 3,
"sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
"source_id": "src.calls.REL_18_6.42P05",
"tag": "",
"url": ""
}Source references
Full definition and evidence JSON
Definition sources
center · PostgreSQL 18 · english-manuals:e37cbe48dbc0578a88d3141e5290771b240b35383619148dc71ec82c7cbebd85
Text language: en · english-manuals:e37cbe48dbc0578a88d3141e5290771b240b35383619148dc71ec82c7cbebd85