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: Sun, 15 Nov 2026 05:39:08 +0000
Message-ID: <gh-pgbouncer-pgbouncer-1706-c6889861418@github.com> (raw)
In-Reply-To: <gh-pgbouncer-pgbouncer-1706@github.com>
References: <gh-pgbouncer-pgbouncer-1706@github.com>
Fair point - the Python repro does read like hand-crafted protocol fuzzing. Here is the same bug from a plain Npgsql program:
```csharp
// Life-like repro: pooled Npgsql connection, reset pipelined with next command.
// Usage: dotnet run -- "<connection-string>"
using Npgsql;
var cs = args[0];
// 1. Normal app usage: open a pooled connection, change session state, query, return it.
await using (var c1 = new NpgsqlConnection(cs))
{
await c1.OpenAsync();
await using (var cmd = new NpgsqlCommand("SET application_name = 'demo'", c1))
await cmd.ExecuteNonQueryAsync();
await using (var cmd = new NpgsqlCommand("SELECT 1", c1))
await cmd.ExecuteScalarAsync();
} // returned to pool: Npgsql queues a fire-and-forget DISCARD ALL in its write buffer
// 2. Next open gets the same pooled connector: the pending DISCARD ALL is
// pipelined in the same write with the next command. Prepare + run.
await using (var c2 = new NpgsqlConnection(cs))
{
await c2.OpenAsync();
await using var cmd = new NpgsqlCommand("SELECT @p::int + 1", c2);
cmd.Parameters.AddWithValue("p", 1);
await cmd.PrepareAsync(); // Parse goes out after the DISCARD ALL
var v = await cmd.ExecuteScalarAsync(); // Bind: direct PG ok, pgbouncer kills the session
Console.WriteLine($"OK value={v}");
}
```
On PgBouncer 1.26.1 (`pool_mode=transaction`, `max_prepared_statements=10000`, Npgsql 9.0.4) it reproduces every time: `ExecuteScalarAsync()` fails with
```
Npgsql.PostgresException (0x80004005): 08P01: prepared statement did not exist
```
and `pgbouncer.log` records `prepared statement '_p1' not found` -> `closing because: prepared statement did not exist`.
The same wire batch works fine through other poolers with server-side prepared-statement tracking: odyssey 1.7.0-rc1 and pg_doorman 3.11.2 both serve the programm. That suggests the batch itself is legitimate and the difference is in the statement-cache handling.
Apologies for the confusion with the Python file: I generated it with an AI to replay the sequence inside pgbouncer's pytest suite, and I should have presented it differently. Message for message it is the same batch the program above sends, plus one control with a `Sync` boundary between the reset and the next `Parse`; that control passes, which is what pins the shared flush as the trigger.
The `DISCARD ALL` comes from Npgsql itself: a connector returned to the pool dirty (changed session state, no prepared statements) gets a bare `DISCARD ALL` as its reset, prepended into the connector's write buffer (`Reset()` -> `PrependInternalMessage(PregeneratedMessages.DiscardAll, 2)` in `src/Npgsql/Internal/NpgsqlConnector.cs`), so it leaves the socket in the same flush, ahead of the first command the connector's next owner writes.