PostgreSQL 17.12Lab-verified · 2026-11-29

PostgreSQL 42P05: prepared statement "get_order" already exists

A prepared statement with that name is still alive in the same session.

PostgreSQL 17.12
42P05: prepared statement "get_order" already exists

Jump to a cause

  1. 01PREPARE with the same name twice in one session
  2. 02Expecting ROLLBACK to remove a PREPARE

Lab-verifiedReproduced and fixed on PostgreSQL 17.12 · 2026-11-29 · container image postgres:17

Prepared statements live until the session ends or you remove them. Running PREPARE again with the same name in the same session gives 42P05 prepared statement "..." already exists.

SELECT name, statement FROM pg_prepared_statements; shows what the current session already has. The runs below are on PostgreSQL 17.

01PREPARE with the same name twice in one session

The first PREPARE get_order(int) AS ... succeeded; the second identical one failed with ERROR: 42P05: prepared statement "get_order" already exists.

pg_prepared_statements showed get_order already there. Running DEALLOCATE get_order; first let the second PREPARE succeed, and EXECUTE get_order(2) returned id 2, amount 250.

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 100), (2, 250);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
✕ ERROR
PREPARE
ERROR:  42P05: prepared statement "get_order" already exists
Checkrun as lab_a
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
SELECT name, statement FROM pg_prepared_statements;
OUTPUT
PREPARE
   name    |                               statement
-----------+------------------------------------------------------------------------
 get_order | PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
(1 row)
After the fixrun as lab_a
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
DEALLOCATE get_order;
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
EXECUTE get_order(2);
✓ SUCCESS
PREPARE
DEALLOCATE
PREPARE
 id | amount
----+--------
  2 |    250
(1 row)

02Expecting ROLLBACK to remove a PREPARE

PREPARE is not undone by a transaction rollback. After BEGIN; PREPARE get_order ...; ROLLBACK;, preparing get_order again still failed with 42P05, and pg_prepared_statements still listed get_order.

DISCARD ALL; cleared the session: pg_prepared_statements returned 0, then PREPARE and EXECUTE get_order(1) worked (id 1, amount 100).

Setup
Setuprun as lab_a
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
INSERT INTO orders VALUES (1, 100), (2, 250);
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
BEGIN;
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
ROLLBACK;
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
✕ ERROR
BEGIN
PREPARE
ROLLBACK
ERROR:  42P05: prepared statement "get_order" already exists
Checkrun as lab_a
BEGIN;
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
ROLLBACK;
SELECT name FROM pg_prepared_statements;
OUTPUT
BEGIN
PREPARE
ROLLBACK
   name
-----------
 get_order
(1 row)
After the fixrun as lab_a
BEGIN;
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
ROLLBACK;
DISCARD ALL;
SELECT count(*) AS prepared FROM pg_prepared_statements;
PREPARE get_order(int) AS SELECT id, amount FROM orders WHERE id = $1;
EXECUTE get_order(1);
✓ SUCCESS
BEGIN
PREPARE
ROLLBACK
DISCARD ALL
 prepared
----------
        0
(1 row)

PREPARE
 id | amount
----+--------
  1 |    100
(1 row)

FAQ

How do I list prepared statements in PostgreSQL?

SELECT name, statement FROM pg_prepared_statements; shows the ones in the current session. Other sessions' statements are not visible there.

Does ROLLBACK remove a prepared statement?

No. In the run above get_order was still in pg_prepared_statements after ROLLBACK, and preparing it again gave 42P05.

DEALLOCATE or DISCARD ALL?

DEALLOCATE name removes that one statement. DISCARD ALL resets the session, and in our run pg_prepared_statements was empty afterwards. Both fixed the error.

How we verify

Each scenario starts from fresh database users. A script runs the setup, confirms the exact error code, applies the fix and runs the statement again. The output shown is what the database printed, saved by the script.

lab_a and lab_b are ordinary roles created for the test, each owning a schema of the same name; postgres is the superuser. PostgreSQL 17.12