{"record":{"id":"1cdf13092ee23fc9","repo":"JuliusBrussee/caveman","slug":"json-number-exceeds-postgresql-numeric-limits","errorCode":null,"errorMessage":"JSON number exceeds PostgreSQL numeric limits","messagePattern":"JSON number exceeds PostgreSQL numeric limits","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shared/platform/proposalrun/proposalrun.go","lineNumber":259,"sourceCode":"\t\tscale--\n\t}\n\n\tvar out string\n\tswitch {\n\tcase scale <= 0:\n\t\tzeros := -scale\n\t\tif zeros > maxJSONBNumericIntegerDigits-len(digits) {\n\t\t\treturn \"\", fmt.Errorf(\"JSON number exceeds PostgreSQL integer-digit limit\")\n\t\t}\n\t\tout = digits + strings.Repeat(\"0\", zeros)\n\tcase scale >= len(digits):\n\t\tif scale > maxJSONBNumericFractionDigits {\n\t\t\treturn \"\", fmt.Errorf(\"JSON number exceeds PostgreSQL fraction-digit limit\")\n\t\t}\n\t\tout = \"0.\" + strings.Repeat(\"0\", scale-len(digits)) + digits\n\tdefault:\n\t\tif scale > maxJSONBNumericFractionDigits || len(digits)-scale > maxJSONBNumericIntegerDigits {\n\t\t\treturn \"\", fmt.Errorf(\"JSON number exceeds PostgreSQL numeric limits\")\n\t\t}\n\t\tpoint := len(digits) - scale\n\t\tout = digits[:point] + \".\" + digits[point:]\n\t}\n\tif negative {\n\t\treturn \"-\" + out, nil\n\t}\n\treturn out, nil\n}\n\nfunc parseJSONExponent(raw string) (int, error) {\n\tif len(raw) == 0 {\n\t\treturn 0, fmt.Errorf(\"invalid JSON number exponent\")\n\t}\n\tnegative := false\n\tif raw[0] == '+' || raw[0] == '-' {\n\t\tnegative = raw[0] == '-'\n\t\traw = raw[1:]","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/shared/platform/proposalrun/proposalrun.go#L241-L277","documentation":"This is the mixed/integer-plus-fraction branch of normalizeJSONNumber: it throws when the number's fractional scale exceeds maxJSONBNumericFractionDigits OR its integer digit count exceeds maxJSONBNumericIntegerDigits, either of which makes the value unrepresentable in a PostgreSQL numeric (max 131072 integer digits, 16383 fraction digits). Throwing here keeps oversized values out of the database.","triggerScenarios":"canonicalizeJSONNumber -> normalizeJSONNumber given a literal like 123...(>131072 integer digits).456 or a number whose trailing-zero-stripped scale still exceeds 16383 while it has both integer and fraction parts.","commonSituations":"Writing huge computed values (factorials, big-integer arithmetic dumped as JSON numbers) into detail JSON, or importing JSON generated with unbounded precision from other systems.","solutions":["Clamp or round the number to fit PostgreSQL numeric limits before appending the run.","Emit the value as a JSON string (or scientific notation string) in detail instead of a bare JSON number.","Split the value into mantissa/exponent fields you control with bounded digits.","Pre-validate literals with a digit-count check at your API boundary."],"exampleFix":"// before\ndetail := []byte(`{\"x\": 123456789012345678901234567890.98765432198765432198765}`)\n// after\ndetail := []byte(`{\"x\": \"1.23456789012345678901e+29\"}`)","handlingStrategy":"validation","validationCode":"func fitsNumericLimits(lit string) bool {\n\tneg := strings.HasPrefix(lit, \"-\")\n\tbody := strings.TrimPrefix(lit, \"-\")\n\tif i := strings.IndexAny(body, \"eE\"); i >= 0 { body = body[:i] }\n\tdigits := strings.Replace(body, \".\", \"\", 1)\n\tdigits = strings.TrimLeft(digits, \"0\")\n\treturn len(digits) <= 131072+16383\n}","typeGuard":null,"tryCatchPattern":"canon, err := canonicalizeJSONNumber(lit)\nif err != nil {\n\tlit = clampToStringField(lit) // emit as string in detail\n}","preventionTips":["Never dump arbitrary-precision results directly into detail JSON","Represent huge values as strings or mantissa/exponent pairs","Validate literals at the API boundary before persistence"],"tags":["postgresql","numeric-precision","jsonb","validation"],"backgroundTag":"value-out-of-range","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}