Npgsql with EF5: Received unexpected backend message ParseComplete

Hi all

I’m using CrateDB via EntityFramework 5 (5.0.13) and Npgsql 5.0.10.

I want to add/update multiple entities at once.

    var newItems = new List<Item>();
    // code to fill list
    await context.AddRangeAsync(newItems);
    await context.SaveChangesAsync();

But this results in the following error:

      Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
       ---> System.Exception: Received unexpected backend message ParseComplete. Please file a bug.
         at Npgsql.NpgsqlConnector.UnexpectedMessageReceived(BackendMessageCode received)
         at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
         at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
         at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
         at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
         --- End of inner exception stack trace ---
         at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
         at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
         at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

Despite the error, the data is correctly inserted into Crate.

When saving the items one by one, the error is not thrown, but perfomance is way worse as the insert statements are not batched anymore.

foreach (Item item in newItems)
{
    await context.AddAsync(item);
    await context.SaveChangesAsync();
}

Does someone know the source of this Exception? And more importantly, how it can be prevented?

Thanks in advance!

Hi @Lukas,

I opened an issue with crate/crate to verify if this requires a change in CrateDB or in npgsql (as noted by exception “Please file a bug”).

Because inserts work despite the exception, I would suggest to ignore this exception until this bug is fixed.

2 Likes

Which CrateDB version are you using?

Does this happen with the latest version?

We are running version 4.6.6

Could you share a standalone minimal snippet to reproduce the issue?

We have some rudimentary tests for npgsql in crate-qa/tests/client_tests/stock_npgsql at master · crate/crate-qa · GitHub

We’ve had issues in the past related to message ordering that only surfaced when using npgsql , so I wouldn’t rule out a bug in CrateDB as opposed to npgsql. Once we have a reproduction case we can have a closer look

2 Likes

Here is a minimal code example.

It references a table with only one column (timestamptz) as primary key.

I tested it with using these verions

  • EntityFrameworkCore v5.0.13
  • Npgsql v5.0.10
    class Program
    {
        static void Main(string[] args)
        {
            CrateContext.ConnectionString =
                "Server=****;Port=****;User Id=****;Password=****;";
            
            using CrateContext context = new();

            // works fine
            for (int i = 0; i < 10; i++)
            {
                context.Test.Add(new TestEntity());
                context.SaveChanges();
            }

            // raises exception show in the first post when calling saveChanges
            for (int i = 0; i < 10; i++)
            {
                context.Test.Add(new TestEntity());
            }
            context.SaveChanges();
        }
    }

    public class TestEntity
    {
        public DateTimeOffset DateTime{ get; set; } = DateTimeOffset.UtcNow;
    }
    
    public class CrateContext : DbContext
    {
        public virtual DbSet<TestEntity> Test { get; set; }
        public static string ConnectionString { get; set; }

        public CrateContext() { }

        public CrateContext(DbContextOptions<CrateContext> options)
            : base(options) { }


        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseNpgsql(ConnectionString);
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TestEntity>(entity =>
            {
                entity.HasKey(e => e.DateTime);
                entity.ToTable("test", "test");
                entity.Property(e => e.DateTime).HasColumnName("datetime").HasColumnType("timestamp with time zone");
            });
        }
    }

DDL

CREATE TABLE test.test (
	datetime timestamp with time zone primary key
);
1 Like

Could you also share the CrateContext definition?

You just need to scroll down a bit in the code snippet :wink:

:man_facepalming:

Scrollbar indicators wouldn’t hurt :stuck_out_tongue:

2 Likes

This will be fixed with the next 4.6 hotfix release (Ensure pg clients receive response messages in order on inserts by mfussenegger · Pull Request #12044 · crate/crate · GitHub)

5 Likes