jackc/pgx · error

cannot convert to integer

Error message

cannot convert %v to integer

What it means

Numeric.toBigInt rejects special values: after the NaN check, this branch fires when the InfinityModifier is anything other than Finite (i.e. the value is +Infinity or -Infinity). Infinite numerics cannot be represented as integers, so conversion is refused and the modifier is printed in the message.

Solutions

  1. Check Numeric.InfinityModifier before integer conversion
  2. Exclude infinite values in the query with WHERE col::text NOT IN ('Infinity','-Infinity')
  3. Scan into pgtype.Numeric instead of an integer Go type
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at pgtype/numeric.go:154 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /api/errors/273ba9a3bd1bc4f9. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/numeric.go:154

		smallF, _ := bigF.Float64()
		src = strconv.FormatFloat(smallF, 'f', -1, 64)
	}

	num, exp, err := parseNumericString(src)
	if err != nil {
		return err
	}

	*n = Numeric{Int: num, Exp: exp, Valid: true}

	return nil
}

func (n *Numeric) toBigInt() (*big.Int, error) {
	if n.NaN {
		return nil, fmt.Errorf("cannot convert NaN to integer")
	} else if n.InfinityModifier != Finite {
		return nil, fmt.Errorf("cannot convert %v to integer", n.InfinityModifier)
	}

	if n.Int == nil {
		return big.NewInt(0), nil
	}

	if n.Exp == 0 {
		return n.Int, nil
	}

	num := &big.Int{}
	num.Set(n.Int)
	if n.Exp > 0 {
		mul := &big.Int{}
		mul.Exp(big10, big.NewInt(int64(n.Exp)), nil)
		num.Mul(num, mul)
		return num, nil
	}

View on GitHub (pinned to ec1a0befd2)