jackc/pgx · error

invalid length for bool

Error message

invalid length for bool: %v

What it means

Binary bool values are exactly one byte on the wire; this guard fires when the src slice has any other length, i.e. a malformed binary bool payload.

Solutions

  1. Fail the scan; the value is not a valid binary bool
  2. Check for corruption or a non-PostgreSQL source of binary data
  3. Ensure custom encoders emit exactly one byte per bool
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/bool.go:252 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/5468a97c812fb12a. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/bool.go:252

	}

	var b bool
	err := codecScan(c, m, oid, format, src, &b)
	if err != nil {
		return nil, err
	}
	return b, nil
}

type scanPlanBinaryBoolToBool struct{}

func (scanPlanBinaryBoolToBool) Scan(src []byte, dst any) error {
	if src == nil {
		return fmt.Errorf("cannot scan NULL into %T", dst)
	}

	if len(src) != 1 {
		return fmt.Errorf("invalid length for bool: %v", len(src))
	}

	p, ok := (dst).(*bool)
	if !ok {
		return ErrScanTargetTypeChanged
	}

	*p = src[0] == 1

	return nil
}

type scanPlanTextAnyToBool struct{}

func (scanPlanTextAnyToBool) Scan(src []byte, dst any) error {
	if src == nil {
		return fmt.Errorf("cannot scan NULL into %T", dst)
	}

View on GitHub (pinned to ec1a0befd2)