Skip to content

Auto preparation broken when executing multi-statement command containing error #3002

Closed

Description

@NinoFloris

Auto preparation throws a PG exception:

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

Activity

  1. 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
  2. NinoFloris commented on Jun 18, 2020

    @NinoFloris
    MemberAuthor

    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.

  3. added this to the Backlog milestone on Jan 17, 2021
  4. roji commented on Jan 17, 2021

    @roji
    Member

    @NinoFloris putting this on the backlog for you to try to repro at some point.

  5. NinoFloris commented on Jan 17, 2021

    @NinoFloris
    MemberAuthor

    I see I wrote it in a somewhat exploratory way but the repro is there already, will reword in a bit

  6. roji commented on May 17, 2021

    @roji
    Member

    @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):

    await using var conn = new NpgsqlConnection("Host=localhost;Username=test;Password=test;Auto Prepare Min Usages=1;Max Auto Prepare=10");
    await conn.OpenAsync();
    
    for (var i = 0; i < 100; i++)
    {
        try
        {
            await conn.ExecuteAsync("SELECT 1; SELECT @missingparam;");
        }
        catch (PostgresException e) when (e.SqlState == "42703" && e.Message.Contains("missingparam"))
        {
            // ignore
        }
    }
  7. removed this from the Backlog milestone on May 17, 2021
  8. NinoFloris commented on May 17, 2021

    @NinoFloris
    MemberAuthor
    await using var conn = new NpgsqlConnection("Host=localhost;Username=test;Password=test;Auto Prepare Min Usages=1;Max Auto Prepare=10");
    
    for (var i = 0; i < 100; i++)
    {
    	try
    	{
    		await conn.ExecuteAsync("SELECT 1; SELECT @missingparam;");
    	}
    	catch (PostgresException e)
    	{
    		Console.WriteLine(e.Message);
    		// ignore
    	}
    }
  9. roji commented on May 18, 2021

    @roji
    Member

    This turned out to be unrelated to batching, but a bug when trying to replace a prepared statement with a bad new statement:

    • A prepared statement already exists (SELECT 1)
    • We try to replace it with a new bad one (SELECTBAD)
    • We receive the PG CloseCompleted message from PG (
      Expect<CloseCompletedMessage>(await Connector.ReadMessage(async), Connector);
      ), and update our internal state accordingly. Crucially, we decrement the global prepared statements counter for (
      _manager.NumPrepared--;
      ).
    • 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.
  10. added a commit that references this issue on May 18, 2021
    b1ef795
  11. added 2 commits that reference this issue on May 18, 2021
    32f5056
    2e74d32
  12. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions