PREPARE a PostgreSQL statement, then EXECUTE it with parameters
Answer: PREPARE creates a prepared statement, a server-side object that can be used to optimize performance. When PREPARE is executed, the specified statement is parsed, analyzed, and rewritten. When EXECUTE is subsequently issued, the prepared statement is planned and executed.
Parameters
$1first parameter, by position$2second parameter, and so onunknowna type inferred from the context when possibleEXECUTEspecify the actual values for these parameters
Prepared statements can take parameters: values substituted into the statement when it is executed. A corresponding list of parameter data types can optionally be specified.
Storage in the backend
The same module also implements storage of prepared statements accessed via the extended FE/BE query protocol. The hash table in which prepared queries are stored is per-backend: query plans are not shared between backends. The keys are the arguments to PREPARE and EXECUTE (statement names). Empty-string statement names conflict with the protocol-level unnamed statement. EXECUTE also supports CREATE TABLE ... AS EXECUTE.
See also
Claims
- id: C1 claim: PREPARE parses, analyzes, and rewrites a statement; EXECUTE later plans and runs it. source: S1 evidence: "When the PREPARE statement is executed, the specified statement is parsed"
- id: C2 claim: Parameters are referenced by position as $1, $2, and so on. source: S1 evidence: "refer to parameters by position, using $1 , $2 , etc."
- id: C3 claim: Stored prepared queries live in a per-backend hash table and are not shared across backends. source: S2 evidence: "query plans are not shared between backends."
Sources
- [S1] PREPARE. PostgreSQL Global Development Group. https://www.postgresql.org/docs/current/sql-prepare.html . License: PostgreSQL License. Accessed 2026-11-20.
- [S2] prepare.c. PostgreSQL Global Development Group. https://raw.githubusercontent.com/postgres/postgres/master/src/backend/commands/prepare.c . License: PostgreSQL License. Accessed 2026-11-20.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PREPARE; PostgreSQL Global Development Group, prepare.c. PostgreSQL License; PostgreSQL License. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PREPARE; PostgreSQL Global Development Group, prepare.c. PostgreSQL License; PostgreSQL License.
Cite this
iinventedlight. 2026. "PREPARE a PostgreSQL statement, then EXECUTE it with parameters." iinventedlight Library, iil:note:k93v8xtb, version of 2026-11-20. https://iinventedlight.com/library/notes/postgres-prepared-statements/. License: CC-BY-4.0.
@misc{iil_note_k93v8xtb,
author = {{iinventedlight}},
title = {{PREPARE a PostgreSQL statement, then EXECUTE it with parameters}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-20},
url = {https://iinventedlight.com/library/notes/postgres-prepared-statements/},
note = {Permanent ID iil:note:k93v8xtb. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- postgres-prepared-statements.table.json (table; CC-BY-4.0)
- postgres-prepared-statements.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/postgres-prepared-statements.md.