From: vadv (@vadv) <noreply+vadv@github.com>
To: pgbouncer/pgbouncer <noreply+pgbouncer-pgbouncer@github.com>
Subject: Re: [pgbouncer/pgbouncer] issue #1706: `DISCARD ALL` reset pipelined with the next Parse wipes the client's prepared statement map
Date: Sat, 14 Nov 2026 19:02:41 +0000
Message-ID: <gh-pgbouncer-pgbouncer-1706-c6882674459@github.com> (raw)
In-Reply-To: <gh-pgbouncer-pgbouncer-1706@github.com>
References: <gh-pgbouncer-pgbouncer-1706@github.com>
Thanks @cosgroveb for taking a look. My reproducer was incomplete: it prepared `DISCARD ALL` but never executed it, because the call to `pg.send_query_prepared(b"", ())` was missing — sorry about that.
I restored that call and reran the test with the upstream `bouncer` fixture, without my external harness, on PgBouncer 1.26.1 (PostgreSQL 16.15, psycopg 3.3.6, libpq 18.6).
With `DISCARD ALL` executed and the new Parse for `s0` sent in the same pipeline batch, I still reproduce the issue: the reset completes, the new Parse is acknowledged, and `PIPELINE_SYNC` is received, but the subsequent Bind/Execute of `s0` fails with `08P01`: `prepared statement did not exist`.
As a control, if I complete the reset in a separate pipeline batch before sending the new Parse, executing `s0` succeeds and returns `1`. The original Parse-only snippet also passes, but does not execute a reset.
There is no explicit `BEGIN`/`COMMIT` block in this test; it uses transaction pooling mode.
The corrected test and both controls are below. Could you take another look?
<details>
<summary>Reproducer using the upstream bouncer fixture (three cases)</summary>
Save as `test/test_issue1706_review.py` in the upstream checkout and run with its normal pytest setup. On 1.26.1, the middle case fails and the other two pass.
```python
import select
import time
import pytest
from psycopg import pq
def finish_batch(pg, label):
pg.pipeline_sync()
pg.flush()
results = []
deadline = time.monotonic() + 5
while True:
remaining = deadline - time.monotonic()
assert remaining > 0, (label, "no PIPELINE_SYNC within five seconds")
pg.consume_input()
if pg.is_busy():
select.select([pg.socket], [], [], remaining)
continue
result = pg.get_result()
if result is None:
assert pg.status != pq.ConnStatus.BAD, pg.error_message
continue
results.append(result)
print(label, pq.ExecStatus(result.status).name, result.command_status,
result.error_field(pq.DiagnosticField.SQLSTATE), result.error_message,
flush=True)
assert result.status not in (pq.ExecStatus.FATAL_ERROR, pq.ExecStatus.PIPELINE_ABORTED), (
label, result.error_field(pq.DiagnosticField.SQLSTATE), result.error_message
)
if result.status == pq.ExecStatus.PIPELINE_SYNC:
assert pg.get_result() is None
return results
@pytest.mark.parametrize(
"execute_reset,separate_sync",
[(False, False), (True, False), (True, True)],
ids=["posted-parse-only", "execute-reset-pipelined", "execute-reset-then-sync"],
)
def test_issue1706(bouncer, execute_reset, separate_sync):
bouncer.admin("set pool_mode=transaction")
bouncer.admin("set max_prepared_statements=10000")
with bouncer.conn(autocommit=True) as conn:
pg = conn.pgconn
pg.enter_pipeline_mode()
pg.send_prepare(b"", b"DISCARD ALL")
if execute_reset:
pg.send_query_prepared(b"", ())
if separate_sync:
reset_results = finish_batch(pg, "reset")
assert any(r.command_status == b"DISCARD ALL" for r in reset_results)
pg.send_prepare(b"s0", b"SELECT 1")
prepared = finish_batch(pg, "prepare")
if not separate_sync:
assert any(r.command_status == b"DISCARD ALL" for r in prepared) == execute_reset
pg.send_query_prepared(b"s0", ())
executed = finish_batch(pg, "execute-s0")
rows = [r for r in executed if r.status == pq.ExecStatus.TUPLES_OK]
assert len(rows) == 1 and rows[0].get_value(0, 0) == b"1"
pg.exit_pipeline_mode()
```
</details>
I checked the max_prepared_statements docs (https://www.pgbouncer.org/config.html#max_prepared_statements): they only cover SQL-level DEALLOCATE ALL / DISCARD ALL handling and say nothing about batching order. The reproducer shows the cache wipe is not scoped to statements prepared before the DISCARD ALL — a Parse registered after it was sent, but before its CommandComplete is processed, is wiped too, which the docs don't mention.
thank you!