jackc/pgx · error

too many arguments

Error message

too many arguments

What it means

Returned by FunctionCall.Encode when Arguments has more than 65535 entries. The argument count is a uint16 on the wire, so MaxUint16 is the hard ceiling; the guard avoids a truncated count. FunctionCall is the legacy 'F' message for the v3 function-call protocol.

Source

Thrown at pgproto3/function_call.go:111

	dst.ResultFormatCode = resultFormatCode
	return nil
}

// Encode encodes src into dst. dst will include the 1 byte message type identifier and the 4 byte message length.
func (src *FunctionCall) Encode(dst []byte) ([]byte, error) {
	dst, sp := beginMessage(dst, 'F')
	dst = pgio.AppendUint32(dst, src.Function)

	if len(src.ArgFormatCodes) > math.MaxUint16 {
		return nil, errors.New("too many arg format codes")
	}
	dst = pgio.AppendUint16(dst, uint16(len(src.ArgFormatCodes)))
	for _, argFormatCode := range src.ArgFormatCodes {
		dst = pgio.AppendUint16(dst, argFormatCode)
	}

	if len(src.Arguments) > math.MaxUint16 {
		return nil, errors.New("too many arguments")
	}
	dst = pgio.AppendUint16(dst, uint16(len(src.Arguments)))
	for _, argument := range src.Arguments {
		if argument == nil {
			dst = pgio.AppendInt32(dst, -1)
		} else {
			dst = pgio.AppendInt32(dst, int32(len(argument)))
			dst = append(dst, argument...)
		}
	}
	dst = pgio.AppendUint16(dst, src.ResultFormatCode)
	return finishMessage(dst, sp)
}

View on GitHub (pinned to ec1a0befd2)

Solutions

  1. Cap Arguments at 65535; PostgreSQL function signatures cannot exceed this anyway.
  2. Batch large argument sets into multiple calls or use array parameters instead of positional args.
  3. Audit the loop building Arguments for an unbounded or duplicated append.
  4. Add a pre-encode length check returning a clear error to the caller.

Example fix

// before
fc := &pgproto3.FunctionCall{
    Function:  oid,
    Arguments: hugeArgs, // > 65535
}
_, err := fc.Encode(nil)

// after
if len(hugeArgs) > 65535 {
    return fmt.Errorf("too many function args: %d (max 65535)", len(hugeArgs))
}
fc := &pgproto3.FunctionCall{Function: oid, Arguments: hugeArgs}
_, err := fc.Encode(nil)
Defensive patterns

Strategy: validation

Validate before calling

func validateFunctionCallArgs(fc *pgproto3.FunctionCall) error {
	if len(fc.Arguments) > math.MaxUint16 {
		return fmt.Errorf("too many arguments: %d (max %d)", len(fc.Arguments), math.MaxUint16)
	}
	return nil
}

Type guard

null

Try / catch

null

Prevention

When it happens

Trigger: Calling `(*FunctionCall).Encode(dst)` with `len(Arguments) > 65535`. Surfaces in synthetic code or a caller invoking a function with an enormous argument list.

Common situations: A caller bulk-converts a slice/map into FunctionCall arguments without bounding it, or a test harness stress-tests the encoder. Real PostgreSQL functions cannot accept >64k args, so this almost always indicates a construction bug.

Related errors


AI-assisted analysis of jackc/pgx@ec1a0befd2 (2026-08-04). Data as JSON: /data/errors/d8695fc2577549bf.json. Report an issue: GitHub.