---
source: https://iinventedlight.com/library/notes/postgres-prepared-statements.md
title: "PREPARE a PostgreSQL statement, then EXECUTE it with parameters"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PostgreSQL License (PostgreSQL Global Development Group); PostgreSQL License (PostgreSQL Global Development Group); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: PostgreSQL Global Development Group, PREPARE; PostgreSQL Global Development Group, prepare.c. PostgreSQL License; PostgreSQL License."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-20
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-20
as_of: 2026-11-20
confidence: medium
untested: "Not reproduced (2026-11-20). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: PostgreSQL PREPARE and EXECUTE"
tags: [postgresql, sql, prepared-statements, databases]
client: Librarian
sources:
  - id: S1
    title: "PREPARE"
    publisher: PostgreSQL Global Development Group
    url: https://www.postgresql.org/docs/current/sql-prepare.html
    accessed: 2026-11-20
    license: "PostgreSQL License"
  - id: S2
    title: "prepare.c"
    publisher: PostgreSQL Global Development Group
    url: https://raw.githubusercontent.com/postgres/postgres/master/src/backend/commands/prepare.c
    accessed: 2026-11-20
    license: "PostgreSQL License"
---
# 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

- `$1` first parameter, by position
- `$2` second parameter, and so on
- `unknown` a type inferred from the context when possible
- `EXECUTE` specify 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

- [postgres-transactions](https://iinventedlight.com/library/notes/postgres-transactions/)
- [postgres-explain](https://iinventedlight.com/library/notes/postgres-explain/)
- [postgres-psql-scripts](https://iinventedlight.com/library/notes/postgres-psql-scripts/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
