jackc/pgx · error

invalid length for Polygon with

Error message

invalid length for Polygon with %d points: %w

What it means

The binary polygon payload contained more bytes than needed for the parsed point count: after reading pointCount*16 bytes of coordinates, r.Finish() reports trailing bytes. The message includes how many points were parsed before the mismatch was detected.

Solutions

  1. Treat the row value as corrupt and fail the scan; do not use the partially parsed points
  2. Verify no codec or transport layer is appending padding to binary values
  3. Capture the raw payload for diagnosis of the length mismatch
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/polygon.go:181 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/77c7dc1d3c6d546f. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/polygon.go:181

	}

	r := pgio.NewReader(src)

	// Each point is two float64s.
	pointCount := r.Count(16)
	if err := r.Err(); err != nil {
		return fmt.Errorf("invalid length for polygon: %w", err)
	}

	points := make([]Vec2, pointCount)
	for i := range points {
		x := r.Uint64()
		y := r.Uint64()
		points[i] = Vec2{math.Float64frombits(x), math.Float64frombits(y)}
	}

	if err := r.Finish(); err != nil {
		return fmt.Errorf("invalid length for Polygon with %d points: %w", pointCount, err)
	}

	return scanner.ScanPolygon(Polygon{
		P:     points,
		Valid: true,
	})
}

type scanPlanTextAnyToPolygonScanner struct{}

func (scanPlanTextAnyToPolygonScanner) Scan(src []byte, dst any) error {
	scanner := (dst).(PolygonScanner)

	if src == nil {
		return scanner.ScanPolygon(Polygon{})
	}

	if len(src) < 7 {

View on GitHub (pinned to ec1a0befd2)