jackc/pgx · error

is greater than maximum value for Int2

Error message

%d is greater than maximum value for Int2

What it means

ScanInt64 on zero-null Int2 fires when the incoming int64 exceeds math.MaxInt16 and would silently wrap when narrowed into the int16-based type. The database value is outside the representable range of both the Go type and PostgreSQL smallint.

Solutions

  1. Switch the Go type to zero-null Int4/Int8 or plain int64
  2. Alter the column to integer or bigint
  3. Validate the value's range before scanning or constrain it in SQL
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/zeronull/int.go:29 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/4c9ae705768c269c. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/zeronull/int.go:29

)

type Int2 int16

// SkipUnderlyingTypePlan implements the [pgtype.SkipUnderlyingTypePlanner] interface.
func (Int2) SkipUnderlyingTypePlan() {}

// ScanInt64 implements the [pgtype.Int64Scanner] interface.
func (dst *Int2) ScanInt64(n pgtype.Int8) error {
	if !n.Valid {
		*dst = 0
		return nil
	}

	if n.Int64 < math.MinInt16 {
		return fmt.Errorf("%d is less than minimum value for Int2", n.Int64)
	}
	if n.Int64 > math.MaxInt16 {
		return fmt.Errorf("%d is greater than maximum value for Int2", n.Int64)
	}
	*dst = Int2(n.Int64)

	return nil
}

// Int64Value implements the [pgtype.Int64Valuer] interface.
func (src Int2) Int64Value() (pgtype.Int8, error) {
	if src == 0 {
		return pgtype.Int8{}, nil
	}
	return pgtype.Int8{Int64: int64(src), Valid: true}, nil
}

// Scan implements the [database/sql.Scanner] interface.
func (dst *Int2) Scan(src any) error {
	if src == nil {
		*dst = 0

View on GitHub (pinned to ec1a0befd2)