PostgreSQL 17.12Lab-verified · 2026-11-30

PostgreSQL 26000: prepared statement does not exist

EXECUTE or DEALLOCATE named a prepared statement that this session does not have.

PostgreSQL 17.12
26000: prepared statement "get_order" does not exist

Jump to a cause

  1. 01PREPARE ran in a different session
  2. 02DEALLOCATE ALL removed it
  3. 03Wrong name in DEALLOCATE or EXECUTE

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

Prepared statements belong to one database session. They are not shared between connections and disappear when the session ends or when DEALLOCATE removes them. pg_prepared_statements shows only the current session's statements, so it is the quickest check.

Below are three cases reproduced on PostgreSQL 17 with the fixes that were actually run.

01PREPARE ran in a different session

The setup session ran PREPARE get_order (int) AS SELECT ... and pg_prepared_statements listed it. The next session (same user lab_a) saw count = 0 in pg_prepared_statements, and EXECUTE get_order(1) failed with 26000.

Running PREPARE and EXECUTE in the same session returned order 1 (NEW).

A new session checking pg_prepared_statements saw 0 rows: nothing is kept between sessions.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id int PRIMARY KEY, status text);
INSERT INTO orders VALUES (1, 'NEW'), (2, 'PAID');
PREPARE get_order (int) AS SELECT order_id, status FROM orders WHERE order_id = $1;
SELECT name, statement FROM pg_prepared_statements;
OUTPUT
CREATE TABLE
INSERT 0 2
PREPARE
   name    |                                      statement
-----------+-------------------------------------------------------------------------------------
 get_order | PREPARE get_order (int) AS SELECT order_id, status FROM orders WHERE order_id = $1;
(1 row)
Reproducerun as lab_a
SELECT count(*) FROM pg_prepared_statements;
EXECUTE get_order(1);
✕ ERROR
 count
-------
     0
(1 row)

ERROR:  26000: prepared statement "get_order" does not exist
Checkrun as lab_a
SELECT name, statement FROM pg_prepared_statements;
OUTPUT
 name | statement
------+-----------
(0 rows)

The same thing happens when a connection pool hands each statement to a different server connection: PREPARE and EXECUTE must use the same connection.

After the fixrun as lab_a
PREPARE get_order (int) AS SELECT order_id, status FROM orders WHERE order_id = $1;
EXECUTE get_order(1);
✓ SUCCESS
PREPARE
 order_id | status
----------+--------
        1 | NEW
(1 row)

02DEALLOCATE ALL removed it

EXECUTE get_order(1) worked, then DEALLOCATE ALL ran, and EXECUTE get_order(2) failed with 26000. DEALLOCATE ALL drops every prepared statement in the session.

With both EXECUTEs first and DEALLOCATE get_order at the end, orders 1 and 2 were returned.

A new session checking pg_prepared_statements saw 0 rows: nothing is kept between sessions.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id int PRIMARY KEY, status text);
INSERT INTO orders VALUES (1, 'NEW'), (2, 'PAID');
OUTPUT
CREATE TABLE
INSERT 0 2
Reproducerun as lab_a
PREPARE get_order (int) AS SELECT order_id, status FROM orders WHERE order_id = $1;
EXECUTE get_order(1);
DEALLOCATE ALL;
EXECUTE get_order(2);
✕ ERROR
PREPARE
 order_id | status
----------+--------
        1 | NEW
(1 row)

DEALLOCATE ALL
ERROR:  26000: prepared statement "get_order" does not exist
Checkrun as lab_a
SELECT name, statement FROM pg_prepared_statements;
OUTPUT
 name | statement
------+-----------
(0 rows)
After the fixrun as lab_a
PREPARE get_order (int) AS SELECT order_id, status FROM orders WHERE order_id = $1;
EXECUTE get_order(1);
EXECUTE get_order(2);
DEALLOCATE get_order;
✓ SUCCESS
PREPARE
 order_id | status
----------+--------
        1 | NEW
(1 row)

 order_id | status
----------+--------
        2 | PAID
(1 row)

DEALLOCATE

03Wrong name in DEALLOCATE or EXECUTE

The statement was prepared as get_order, but DEALLOCATE get_orders was called and failed with 26000 naming get_orders. SELECT name FROM pg_prepared_statements showed the real name get_order, and DEALLOCATE get_order succeeded.

A new session checking pg_prepared_statements saw 0 rows: nothing is kept between sessions.

Setup
Setuprun as lab_a
CREATE TABLE orders (order_id int PRIMARY KEY, status text);
OUTPUT
CREATE TABLE
Reproducerun as lab_a
PREPARE get_order (int) AS SELECT order_id, status FROM orders WHERE order_id = $1;
DEALLOCATE get_orders;
✕ ERROR
PREPARE
ERROR:  26000: prepared statement "get_orders" does not exist
Checkrun as lab_a
SELECT name, statement FROM pg_prepared_statements;
OUTPUT
 name | statement
------+-----------
(0 rows)
After the fixrun as lab_a
PREPARE get_order (int) AS SELECT order_id, status FROM orders WHERE order_id = $1;
SELECT name FROM pg_prepared_statements;
DEALLOCATE get_order;
✓ SUCCESS
PREPARE
   name
-----------
 get_order
(1 row)

DEALLOCATE

FAQ

Why does PostgreSQL say prepared statement does not exist right after PREPARE?

PREPARE and EXECUTE ran on different sessions. Here the other session's pg_prepared_statements count was 0. Run both on the same connection.

How do I list prepared statements in PostgreSQL?

SELECT name, statement FROM pg_prepared_statements; It shows only the current session's statements.

What does DEALLOCATE ALL do?

It removes every prepared statement in the current session. Any EXECUTE after it fails with 26000 until you PREPARE again.

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