JuliusBrussee/caveman · error

JSON number exceeds PostgreSQL numeric limits

Error message

JSON number exceeds PostgreSQL numeric limits

What it means

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.

Solutions

  1. Clamp or round the number to fit PostgreSQL numeric limits before appending the run.
  2. Emit the value as a JSON string (or scientific notation string) in detail instead of a bare JSON number.
  3. Split the value into mantissa/exponent fields you control with bounded digits.
  4. Pre-validate literals with a digit-count check at your API boundary.

Example fix

// before
detail := []byte(`{"x": 123456789012345678901234567890.98765432198765432198765}`)
// after
detail := []byte(`{"x": "1.23456789012345678901e+29"}`)
Defensive patterns

Strategy: validation

Validate before calling

func fitsNumericLimits(lit string) bool {
	neg := strings.HasPrefix(lit, "-")
	body := strings.TrimPrefix(lit, "-")
	if i := strings.IndexAny(body, "eE"); i >= 0 { body = body[:i] }
	digits := strings.Replace(body, ".", "", 1)
	digits = strings.TrimLeft(digits, "0")
	return len(digits) <= 131072+16383
}

Try / catch

canon, err := canonicalizeJSONNumber(lit)
if err != nil {
	lit = clampToStringField(lit) // emit as string in detail
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/1cdf13092ee23fc9. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/proposalrun/proposalrun.go:259

		scale--
	}

	var out string
	switch {
	case scale <= 0:
		zeros := -scale
		if zeros > maxJSONBNumericIntegerDigits-len(digits) {
			return "", fmt.Errorf("JSON number exceeds PostgreSQL integer-digit limit")
		}
		out = digits + strings.Repeat("0", zeros)
	case scale >= len(digits):
		if scale > maxJSONBNumericFractionDigits {
			return "", fmt.Errorf("JSON number exceeds PostgreSQL fraction-digit limit")
		}
		out = "0." + strings.Repeat("0", scale-len(digits)) + digits
	default:
		if scale > maxJSONBNumericFractionDigits || len(digits)-scale > maxJSONBNumericIntegerDigits {
			return "", fmt.Errorf("JSON number exceeds PostgreSQL numeric limits")
		}
		point := len(digits) - scale
		out = digits[:point] + "." + digits[point:]
	}
	if negative {
		return "-" + out, nil
	}
	return out, nil
}

func parseJSONExponent(raw string) (int, error) {
	if len(raw) == 0 {
		return 0, fmt.Errorf("invalid JSON number exponent")
	}
	negative := false
	if raw[0] == '+' || raw[0] == '-' {
		negative = raw[0] == '-'
		raw = raw[1:]

View on GitHub (pinned to 3ee70a1026)