Skip to content

Decimal decoding error when summing in the database (but not client-side) #5848

Description

@TheYoxy

Hi,

While using the postgres adapter for efcore, I'm facing a weird issue where, when aggregating data with a sum, I get an OverflowException on the deserialization of the returned response to the user.

System.OverflowException: Value was either too large or too small for a Decimal.
   at System.Number.ThrowOverflowException(String message)
   at System.Decimal.DecCalc.ScaleResult(Buf24* bufRes, UInt32 hiRes, Int32 scale)
   at System.Decimal.DecCalc.VarDecMul(DecCalc& d1, DecCalc& d2)
   at System.Decimal.op_Multiply(Decimal d1, Decimal d2)
   at Npgsql.Internal.Converters.PgNumeric.Builder.ToDecimal(Int16 scale, Int16 weight, UInt16 sign, Span`1 digits)
   at Npgsql.Internal.Converters.PgNumeric.Builder.ToDecimal()
   at Npgsql.Internal.Converters.DecimalNumericConverter`1.ConvertTo(Builder& numeric)
   at Npgsql.Internal.Converters.DecimalNumericConverter`1.ReadCore(PgReader reader)
   at Npgsql.Internal.PgBufferedConverter`1.Read(PgReader reader)
   at Npgsql.NpgsqlDataReader.GetFieldValueCore[T](Int32 ordinal)
   at Npgsql.NpgsqlDataReader.GetDecimal(Int32 ordinal)
   at lambda_method88(Closure, QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator)
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in /npgsql-decimal-issue/Program.cs:line 49
   at Program.<Main>(String[] args)

Reproduction

Here is a reproduction repository already setup with a case where the exception is thrown and some case where it doesn't.

From the few test I did until now, it seems that the value is too big to be handled by the dotnet runtime, but while fetching all the data directly on the application side instead of doing the calculation directly at the database side, I don't get any errors, neither from overflow or anything else.

Versions

Npgsql.EntityFrameworkCore.PostgreSQL: 7.0.0 and onwards.

$ dotnet --info

.NET SDK:
 Version:           9.0.100-rc.1.24452.12
 Commit:            81a714c6d3
 Workload version:  9.0.100-manifests.a7bf2b8f
 MSBuild version:   17.12.0-preview-24422-09+d17ec720d

Runtime Environment:
 OS Name:     nixos
 OS Version:  24.05
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /nix/store/1k6xg0qsjm49z35zdk94q2rfrgr2hlma-dotnet-sdk-9.0.100-rc.1.24452.12/sdk/9.0.100-rc.1.24452.12/

.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.

Host:
  Version:      9.0.0-rc.1.24431.7
  Architecture: x64
  Commit:       static

.NET SDKs installed:
  8.0.401 [/nix/store/pmsmcwd1spflhnwqmnkqwp9ps2dgca1c-dotnet-core-combined/sdk]
  9.0.100-rc.1.24452.12 [/nix/store/pmsmcwd1spflhnwqmnkqwp9ps2dgca1c-dotnet-core-combined/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.8 [/nix/store/pmsmcwd1spflhnwqmnkqwp9ps2dgca1c-dotnet-core-combined/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0-rc.1.24452.1 [/nix/store/pmsmcwd1spflhnwqmnkqwp9ps2dgca1c-dotnet-core-combined/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.8 [/nix/store/pmsmcwd1spflhnwqmnkqwp9ps2dgca1c-dotnet-core-combined/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0-rc.1.24431.7 [/nix/store/pmsmcwd1spflhnwqmnkqwp9ps2dgca1c-dotnet-core-combined/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  DOTNET_ROOT       [/nix/store/pmsmcwd1spflhnwqmnkqwp9ps2dgca1c-dotnet-core-combined]

global.json file:
  Not found

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions