From: vadv (@vadv) <noreply+vadv@github.com>
To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>
Subject: [pgbouncer/pgbouncer] issue #1706: `DISCARD ALL` reset pipelined with the next Parse wipes the client's prepared statement map
Date: Fri, 13 Nov 2026 09:25:55 +0000
Message-ID: <gh-pgbouncer-pgbouncer-1706@github.com> (raw)
### Description
In transaction pooling mode, PgBouncer appears to drop prepared statements that are prepared after a pipelined `DISCARD ALL` command. This leads to `08P01` protocol errors and session termination, as the client expects the statement to be present, while PgBouncer's internal tracking seemingly fails to associate the newly prepared statement with the current session state.
### Reproducer
The following Python script (using `psycopg` in pipeline mode) demonstrates the issue. It performs a `DISCARD ALL`, followed by a `PREPARE` statement within a transaction.
```python
from psycopg import pq
def _read_pipeline(pg, results):
# Get results until PIPELINE_SYNC
while True:
pg.consume_input()
if pg.is_busy(): continue
res = pg.get_result()
if res is None: continue
results.append(res)
if res.status == pq.ExecStatus.PIPELINE_SYNC: break
def _scenario(pg):
# Pipeline DISCARD ALL and PREPARE in one batch
pg.enter_pipeline_mode()
pg.send_prepare(b"", b"DISCARD ALL")
pg.send_prepare(b"s0", b"SELECT 1")
pg.pipeline_sync()
pg.flush()
results = []
_read_pipeline(pg, results)
pg.exit_pipeline_mode()
assert all(r.status != pq.ExecStatus.FATAL_ERROR for r in results)
# Attempt to execute the prepared statement
pg.enter_pipeline_mode()
pg.send_query_prepared(b"s0", ())
pg.pipeline_sync()
pg.flush()
results = []
_read_pipeline(pg, results)
pg.exit_pipeline_mode()
# Assert successful execution
assert any(r.status == pq.ExecStatus.TUPLES_OK for r in results)
# Test function within the PgBouncer test suite
def test_discard_all_pipelined_with_prepare(bouncer):
bouncer.admin("set pool_mode=transaction")
bouncer.admin("set max_prepared_statements=10000")
with bouncer.conn() as conn:
_scenario(conn.pgconn)
```
### Observations
The same test was executed against various connection poolers in transaction mode with prepared statement tracking enabled:
| Pooler | Result |
| :--- | :--- |
| Odyssey 1.7.0-rc1 | PASS |
| pg_doorman 3.11.2 | PASS |
| **PgBouncer 1.26.1** | **FAILED (08P01)** |
In contrast to other solutions, PgBouncer terminates the client session upon the first attempt to execute the prepared statement, reporting `prepared statement 's0' not found`.
### Impact
This behavior significantly impacts applications that utilize prepared statements within transaction pooling mode. Specifically, scenarios where frameworks perform state cleanup (`DISCARD ALL`) followed by statement preparation within the same transaction lifecycle are prone to failure. The resulting protocol error renders the session unusable, necessitating a client-side reconnection.