fix: decode PG NUMERIC directly to avoid rust_decimal panics#139
Open
fix: decode PG NUMERIC directly to avoid rust_decimal panics#139
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces
rust_decimal::Decimalwith a customPgNumerictype that decodes the PostgreSQL NUMERIC binary wire format directly into aString, eliminating panics on large values.Motivation
abi_uint()on uint256 columns produces 78-digit NUMERIC values.rust_decimal::Decimalhas a 96-bit mantissa (~28 digits) and panics infrom_i128_with_scale()when values exceed that limit. The existingcatch_unwindcatches the panic but stderr output still floods the logs — ~244 panics/day in prd across 5 pods.Changes
src/service/mod.rs: AddedPgNumericstruct implementingFromSqlto decode PG NUMERIC binary format (base-10000 digit groups) directly to stringsrc/service/mod.rs: Replacedcatch_unwind(try_get::<_, Decimal>)withtry_get::<_, PgNumeric>Cargo.toml: Removedrust_decimaldependency (no longer used)weight < -1), digit validationTesting
13 unit tests covering: zero, small/large integers, negatives, decimals, deep fractions, uint256 max (78 digits), NaN, ±Infinity, invalid digits, invalid signs.
Co-Authored-By: Kamil Szczygieł 2961450+kamsz@users.noreply.github.com
Prompted by: kamil