jackc/pgx · error

cannot losslessly convert

Error message

cannot losslessly convert %v to int64

What it means

Returned by the binary float4-to-Int64Scanner plan when the decoded float32 has a fractional part or magnitude that cannot round-trip through int64. The conversion is deliberately lossless-only: if float32(int64(f)) != f the scan fails.

Solutions

  1. Scan into a float target (float32/float64/Float8) instead of an integer one
  2. Round the value in SQL (e.g. round()) before scanning
  3. Use pgtype.Numeric for exact decimal handling
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/float4.go:258 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/156bd58052dd09cd. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/float4.go:258

type scanPlanBinaryFloat4ToInt64Scanner struct{}

func (scanPlanBinaryFloat4ToInt64Scanner) Scan(src []byte, dst any) error {
	s := (dst).(Int64Scanner)

	if src == nil {
		return s.ScanInt64(Int8{})
	}

	raw, err := pgio.Uint32Exact(src)
	if err != nil {
		return fmt.Errorf("float4: %w", err)
	}

	ui32 := int32(raw)
	f32 := math.Float32frombits(uint32(ui32))
	i64 := int64(f32)
	if f32 != float32(i64) {
		return fmt.Errorf("cannot losslessly convert %v to int64", f32)
	}

	return s.ScanInt64(Int8{Int64: i64, Valid: true})
}

type scanPlanBinaryFloat4ToTextScanner struct{}

func (scanPlanBinaryFloat4ToTextScanner) Scan(src []byte, dst any) error {
	s := (dst).(TextScanner)

	if src == nil {
		return s.ScanText(Text{})
	}

	raw, err := pgio.Uint32Exact(src)
	if err != nil {
		return fmt.Errorf("float4: %w", err)
	}

View on GitHub (pinned to ec1a0befd2)