jackc/pgx · error

is less than minimum value for Int2

Error message

%d is less than minimum value for Int2

What it means

ScanInt64 on zero-null Int2 fires when the incoming int64 is below math.MinInt16 and would silently wrap if stored in the int16-based type. The input at fault is the database value, which falls outside the range the Go type (and PostgreSQL smallint) can represent.

Solutions

  1. Switch the Go type to zero-null Int4/Int8 or plain int64 to match the column's real range
  2. Alter the column to integer or bigint
  3. Clamp or reject out-of-range values in the query or with a CHECK constraint
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/zeronull/int.go:26 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/078b11b8be9e7d8a. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/zeronull/int.go:26

	"math"

	"github.com/jackc/pgx/v5/pgtype"
)

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.

View on GitHub (pinned to ec1a0befd2)