Skip to content

fix: decode PG NUMERIC directly to avoid rust_decimal panics#139

Open
decofe wants to merge 2 commits intomainfrom
kamil/fix-pg-numeric-panic
Open

fix: decode PG NUMERIC directly to avoid rust_decimal panics#139
decofe wants to merge 2 commits intomainfrom
kamil/fix-pg-numeric-panic

Conversation

@decofe
Copy link
Copy Markdown
Member

@decofe decofe commented Mar 20, 2026

Summary

Replaces rust_decimal::Decimal with a custom PgNumeric type that decodes the PostgreSQL NUMERIC binary wire format directly into a String, eliminating panics on large values.

Motivation

abi_uint() on uint256 columns produces 78-digit NUMERIC values. rust_decimal::Decimal has a 96-bit mantissa (~28 digits) and panics in from_i128_with_scale() when values exceed that limit. The existing catch_unwind catches the panic but stderr output still floods the logs — ~244 panics/day in prd across 5 pods.

Changes

  • src/service/mod.rs: Added PgNumeric struct implementing FromSql to decode PG NUMERIC binary format (base-10000 digit groups) directly to string
  • src/service/mod.rs: Replaced catch_unwind(try_get::<_, Decimal>) with try_get::<_, PgNumeric>
  • Cargo.toml: Removed rust_decimal dependency (no longer used)
  • Handles edge cases: NaN, ±Infinity, negative values, deep fractions (weight < -1), digit validation

Testing

13 unit tests covering: zero, small/large integers, negatives, decimals, deep fractions, uint256 max (78 digits), NaN, ±Infinity, invalid digits, invalid signs.

cargo test -- pg_numeric  # 13 passed
cargo check               # clean

Co-Authored-By: Kamil Szczygieł 2961450+kamsz@users.noreply.github.com

Prompted by: kamil

decofe and others added 2 commits March 20, 2026 22:09
Replace rust_decimal::Decimal (which panics on values exceeding its
96-bit mantissa ~28 digits) with a custom PgNumeric type that decodes
the PostgreSQL NUMERIC binary wire format directly into a String.

This fixes ~244 panics/day in prd caused by abi_uint() producing
78-digit uint256 values that overflow rust_decimal.

Co-Authored-By: Kamil Szczygieł <2961450+kamsz@users.noreply.github.com>
Co-Authored-By: Kamil Szczygieł <2961450+kamsz@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants