jackc/pgx · error
unable to encode
Error message
unable to encode %v
What it means
When encoding a multirange to text, the encoder reflects over each element and re-plans the element's encode step whenever the element's Go type changes. This guard fires when no encode plan can be found for an element — the element's type is not encodable to the multirange's element type OID (e.g. a Go type not registered for the underlying range subtype).
Solutions
- Ensure all elements share a Go type registered for the multirange's element type via RegisterDefaultPgType
- Scan into a typed Multirange[Range[T]] instead of a mixed-type slice
- Remove unsupported element types from the slice before encoding
Defensive patterns
Strategy: type-guard
When it happens
Trigger: Thrown at pgtype/multirange.go:113 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/ae2c92efafada2e4.
Report an issue: GitHub.
Appendix: source
Thrown at pgtype/multirange.go:113
buf = append(buf, '{')
var encodePlan EncodePlan
var lastElemType reflect.Type
inElemBuf := make([]byte, 0, 32)
for i := range elementCount {
if i > 0 {
buf = append(buf, ',')
}
elem := multirange.Index(i)
var elemBuf []byte
if elem != nil {
elemType := reflect.TypeOf(elem)
if lastElemType != elemType {
lastElemType = elemType
encodePlan = p.m.PlanEncode(p.ac.ElementType.OID, TextFormatCode, elem)
if encodePlan == nil {
return nil, fmt.Errorf("unable to encode %v", multirange.Index(i))
}
}
elemBuf, err = encodePlan.Encode(elem, inElemBuf)
if err != nil {
return nil, err
}
}
if elemBuf == nil {
return nil, fmt.Errorf("multirange cannot contain NULL element")
} else {
buf = append(buf, elemBuf...)
}
}
buf = append(buf, '}')
return buf, nilView on GitHub (pinned to ec1a0befd2)