You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto preparation broken when executing multi-statement command containing error #3002
Npgsql.PostgresException (0x80004005): 26000: prepared statement "_auto6" does not exist
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
This can be reproed fastest with auto prepare min usages at 1 and max prepare on anything but default by doing something like:
// dapper api
await conn.ExecuteAsync("SELECT1; SELECT @missingparam;"); // note we're not passing params
First few times this will return Npgsql.PostgresException (0x80004005): 42703: column "missing" does not exist
Then it starts returning prepared statement "_auto#" does not exist
changed the title [-]Auto preparation broken when executing command outside running transaction[/-][+]Auto preparation broken when executing multi-statement command containing error[/+]on Jun 10, 2020
I've seen this happen today again, same underlying issue. I suspect we're just missing some bookkeeping here to remove the auto preparation key if the db statement failed.
@NinoFloris when you have a moment, can you provide a full repro? The below seems to work fine for me (maybe the bug was fixed since you filed it):
awaitusingvarconn=newNpgsqlConnection("Host=localhost;Username=test;Password=test;Auto Prepare Min Usages=1;Max Auto Prepare=10");awaitconn.OpenAsync();for(vari=0;i<100;i++){try{awaitconn.ExecuteAsync("SELECT 1; SELECT @missingparam;");}catch(PostgresExceptione)when(e.SqlState=="42703"&&e.Message.Contains("missingparam")){// ignore}}
awaitusingvarconn=newNpgsqlConnection("Host=localhost;Username=test;Password=test;Auto Prepare Min Usages=1;Max Auto Prepare=10");for(vari=0;i<100;i++){try{awaitconn.ExecuteAsync("SELECT 1; SELECT @missingparam;");}catch(PostgresExceptione){Console.WriteLine(e.Message);// ignore}}
Right after that we get the PG error for the bad SQL, and abort the whole thing.
The prepared statement slot remains in our state. We can attempt to replace it again - we weren't checking its state or anything. This can cause the global prepared statements counter to go to 0.
If we close the connection at this point, Npgsql will send DISCARD ALL since it thinks there are no prepared statements based on that counter. But PreparedStatementManager may still other prepared statements.
Activity
[-]Auto preparation broken when executing command outside running transaction[/-][+]Auto preparation broken when executing multi-statement command containing error[/+]NinoFloris commentedon Jun 18, 2020
I've seen this happen today again, same underlying issue. I suspect we're just missing some bookkeeping here to remove the auto preparation key if the db statement failed.
roji commentedon Jan 17, 2021
@NinoFloris putting this on the backlog for you to try to repro at some point.
NinoFloris commentedon Jan 17, 2021
I see I wrote it in a somewhat exploratory way but the repro is there already, will reword in a bit
roji commentedon May 17, 2021
@NinoFloris when you have a moment, can you provide a full repro? The below seems to work fine for me (maybe the bug was fixed since you filed it):
NinoFloris commentedon May 17, 2021
roji commentedon May 18, 2021
This turned out to be unrelated to batching, but a bug when trying to replace a prepared statement with a bad new statement:
npgsql/src/Npgsql/NpgsqlDataReader.cs
Line 426 in 05e9553
npgsql/src/Npgsql/PreparedStatement.cs
Line 116 in d42cc68
Fix prepared statement logic for failing statement
Fix prepared statement logic for failing statement (#3750)
Fix prepared statement logic for failing statement (#3750)