Npgsql: ExecuteScalar throws exception with message 'Unknown message code: 22/35' when trying to return a large decimal

Created on 3 Apr 2020  路  4Comments  路  Source: npgsql/npgsql

Steps to reproduce

var connectionString = "<an amazing connection string>";

using (var conn = new NpgsqlConnection(connectionString)) {
    conn.Open();

    using (var trans = conn.BeginTransaction()) {
        using(var cmd = new NpgsqlCommand(@"SELECT 1000000000000000000000.123456789;", conn, trans)) {
            Console.Out.WriteLine(cmd.ExecuteScalar());
        }
    }
}

The issue

ExecuteScalar() is unable to return large decimal numbers.

Exception message:

  • 1000000000000000000000.123456789 gives Unknown message code: 22
  • 100000000000000000000.123456789 gives Unknown message code: 35

Stack trace:

The stack trace is the same for both cases, only the message changes.

Npgsql.NpgsqlException
  HResult=0x80004005
  Message=Unknown message code: XX
  Source=Npgsql
  StackTrace:
   at Npgsql.Util.PGUtil.ValidateBackendMessageCode(BackendMessageCode code)
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlDataReader.<NextResult>d__44.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlDataReader.<Consume>d__64.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlDataReader.<Close>d__69.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlDataReader.Close()
   at Npgsql.NpgsqlCommand.<ExecuteScalar>d__95.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Npgsql.NpgsqlCommand.ExecuteScalar()
   at Tests.Program.Main(String[] args) 

Further technical details

Npgsql version: 4.0.10 & 4.1.3.1
PostgreSQL version: 11.4 & 12
Operating system: Windows 10

duplicate

Most helpful comment

Will take a look.

All 4 comments

Will take a look.

@YohDeadfall pinging as a reminder :)

The expected exception from ExecuteScalar is "hidden" behind an unexpected exception from Dispose as already seen in #2621.
The exception should be here an OverflowException("Numeric value does not fit in a System.Decimal").

Thanks @manandre! That code actually catches OverflowException, but it's also reading within that try/catch and probably doesn't make sure everything has been consumed before throwing NpgsqlSafeReadException. Am going to close this as a duplicate of #2621.

Was this page helpful?
0 / 5 - 0 ratings