jackc/pgx · error

invalid length for Polygon

Error message

invalid length for Polygon: %v

What it means

The text-form polygon source is shorter than the minimum 7 bytes required for even a single-point polygon "((x,y))". The guard fires before any parsing, so the input is structurally too short to be a valid PostgreSQL polygon literal.

Solutions

  1. Check the source string for truncation before scanning
  2. Ensure the database column actually contains polygon values and not empty or unrelated strings
  3. Log the offending input to find where the bad value originates
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

Thrown at pgtype/polygon.go:200

	}

	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 {
		return fmt.Errorf("invalid length for Polygon: %v", len(src))
	}

	points := make([]Vec2, 0)

	// Expected format: ((x1,y1),...,(xn,yn))
	str := string(src[1 : len(src)-1])

	for {
		if len(str) == 0 || str[0] != '(' {
			return fmt.Errorf("invalid format for Polygon")
		}
		body, rest, found := strings.Cut(str[1:], ")")
		if !found {
			return fmt.Errorf("invalid format for Polygon")
		}

		sx, sy, found := strings.Cut(body, ",")
		if !found {

View on GitHub (pinned to ec1a0befd2)