Skip to content

Consider an opt-in behavior to truncate high-precision PG numeric types to read them as decimal #6125

Description

@roji

Over the years, we've received many reports of people trying to read numeric/decimal values from PostgreSQL which have too much precision for .NET decimal (see #448). There are several workarounds for specific use-cases:

  • BigInteger can be used for integer-only values.
  • Values can be fetched as text for display purposes.
  • Data can be truncated in the database to remove the extra precision which causes reading to fail.

However, in some cases the above are insufficient, and we've received requests to allow these values to be truncated as part of the reading process. This would be an explicit opt-in, as Npgsql does not truncate or lose information by default; but it could make sense for some use-cases.

Activity

  1. added this to the Backlog milestone on May 31, 2025
  2. added theissue type on Jun 14, 2025
  3. johnfedak commented on Jul 18, 2025

    @johnfedak

    There are 2 types of overflow here which might need to be handled separately. There is simply exceeding the range of allowed .Net values (i.e. numerics < Decimal.MinValue or > Decimal.MaxValue)

    This is not our primary concern.

    The scenario we are hitting is the precision of the PostgreSQL numeric is exceeding the precision allowed by System.Decimal. And in this case we would simply like the PostgreSQL value rounded/truncated to the precision allowed by .Net

    The full precision of the source numeric in our cases is almost always generated by floating point division and is immaterial to the application

  4. roji commented on Jul 19, 2025

    @roji
    MemberAuthor

    Yes, this issue is indeed about precision, not range - I don't think we'd ever truncate e.g. a numeric that's beyond Decimal.MaxValue to Decimal.MaxValue, since that would return completely different data. I'll update the issue title to make that clear.

    Beyond that, note that you should already be able to perform the truncate database-side, in SQL.

  5. changed the title [-]Consider an opt-in behavior to truncate out-of-range PG numeric types to read them as decimal[/-] [+]Consider an opt-in behavior to truncate high-precision PG numeric types to read them as decimal[/+] on Jul 19, 2025
  6. klinki commented on Feb 21, 2026

    @klinki

    I can't wait for this one to be implemented, this is causing me a lot of trouble.

    I'm in the process of migration from MySQL to PostgreSQL and now I'm fighting with that.

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

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions