jackc/pgx · error

cannot scan composite field

Error message

cannot scan composite field %d into CompositeFields of length %d

What it means

Returned by compositeFieldTarget when the incoming composite has more fields than the destination CompositeFields slice has elements. Indexing past the slice end would panic, so the mismatch is reported as an error instead.

Solutions

  1. Size the CompositeFields slice to match the composite type's field count
  2. Select fewer fields in the query so counts match
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at pgtype/composite.go:339 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/205945d94c1dcc43. Report an issue: GitHub.

Appendix: source

Thrown at pgtype/composite.go:339

	cfs.fieldBytes, _ = cfs.r.Value()
	if err := cfs.r.Err(); err != nil {
		cfs.err = fmt.Errorf("Record incomplete: %w", err)
		return false
	}

	return true
}

func (cfs *CompositeBinaryScanner) FieldCount() int {
	return int(cfs.fieldCount)
}

// compositeFieldTarget returns the scan target for field i. CompositeFields is
// a slice, so indexing past its end would panic. A source with more fields than
// the destination is a mismatch that must be reported as an error.
func compositeFieldTarget(targetScanner CompositeIndexScanner, i int) (any, error) {
	if cf, ok := targetScanner.(CompositeFields); ok && i >= len(cf) {
		return nil, fmt.Errorf("cannot scan composite field %d into CompositeFields of length %d", i, len(cf))
	}

	return targetScanner.ScanIndex(i), nil
}

// Bytes returns the bytes of the field most recently read by Scan().
func (cfs *CompositeBinaryScanner) Bytes() []byte {
	return cfs.fieldBytes
}

// OID returns the OID of the field most recently read by Scan().
func (cfs *CompositeBinaryScanner) OID() uint32 {
	return cfs.fieldOID
}

// Err returns any error encountered by the scanner.
func (cfs *CompositeBinaryScanner) Err() error {
	return cfs.err

View on GitHub (pinned to ec1a0befd2)