jackc/pgx · error

invalid format for Polygon

Error message

invalid format for Polygon

What it means

The text polygon parser expected an opening '(' at the current position or a closing ')' to terminate a point pair, but found neither — the input does not follow the "((x1,y1),...,(xn,yn))" grammar after the outer parentheses were stripped.

Solutions

  1. Validate the string matches PostgreSQL polygon output syntax before scanning
  2. Re-serialize the value through the database (e.g. SELECT polygon::text) to normalize it
  3. Reject user-supplied polygon literals that were not produced by PostgreSQL
Defensive patterns

Strategy: validation

When it happens

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

Appendix: source

Thrown at pgtype/polygon.go:210

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 {
			return fmt.Errorf("invalid format for Polygon")
		}
		x, err := strconv.ParseFloat(sx, 64)
		if err != nil {
			return err
		}
		y, err := strconv.ParseFloat(sy, 64)
		if err != nil {
			return err
		}

View on GitHub (pinned to ec1a0befd2)