Skip to content

Failure during preparation leads to bad state #2665

Closed

Description

@inelisoni

Steps to reproduce

string connectionString = GetConnectionString();

try
{
    Insert(connectionString);
}
catch (PostgresException)
{
    // Table does not exist, it's expected
}

// Create table
{
    using var connection = new NpgsqlConnection(connectionString);
    connection.Open();
    const string query = "create table test_table(id integer)";
    using var command = new NpgsqlCommand(query, connection);
    command.ExecuteNonQuery();
}

// Retry insert
try
{
    Insert(connectionString);
}
catch (PostgresException)
{
    // prepared statement does not exist, unexpected behaviour
}

static string GetConnectionString()
{
    return new NpgsqlConnectionStringBuilder
        {
            Host = "localhost",
            Port = 5434,
            Username = "postgres",
            Password = "postgres",
            Database = "postgres",
            Pooling = true,
            MinPoolSize = 1,
            MaxPoolSize = 1,
        }.
        ConnectionString;
}

static void Insert(string connectionString)
{
    using var connection = new NpgsqlConnection(connectionString);
    connection.Open();
    
    const string query = "insert into test_table(id) values (@id)";
    using var command = new NpgsqlCommand(query, connection);
    var param = new NpgsqlParameter<int>("id", 1);
    command.Parameters.Add(param);
    command.Prepare();
    command.ExecuteNonQuery();
}

The issue

Cannot insert into the table if the previous insert failed on the same connection.
Workaround is to add UnprepareAll before connection disposing.

prepared statement "_p1" does not exist
   
at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 953
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 973
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 416
   at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 298
   at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1177
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 977
   at Npgsql.NpgsqlCommand.ExecuteNonQuery() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 959

Further technical details

Npgsql version: 4.1.1
PostgreSQL version: 11
Operating system: Windows 10 1903 x64

Activity

  1. self-assigned this
    on Oct 14, 2019
  2. added this to the 4.1.2 milestone on Oct 14, 2019
  3. modified the milestones: 4.1.2, 4.1.3 on Nov 14, 2019
  4. changed the title [-]Prepared statement does not exist[/-] [+]Failure during preparation leads to bad state[/+] on Jan 24, 2020
  5. added 3 commits that reference this issue on Jan 24, 2020
    041d8d7
    9a70439
  6. added 2 commits that reference this issue on Jan 31, 2020
    afe2ac7
    e096a8b
  7. 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

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions