jackc/pgx · error

mismatched param and argument count

Error message

mismatched param and argument count

What it means

ExtendedQueryBuilder.Build was given a statement description whose ParamOIDs count differs from the number of arguments supplied. The prepared statement expects a fixed parameter count that the call did not match.

Solutions

  1. Match the number of args to the statement's parameter count
  2. Re-prepare or re-describe the statement if its signature changed
  3. Check for cached statement collisions when reusing SQL strings
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at extended_query_builder.go:36 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/f03a6639c724aaee. Report an issue: GitHub.

Appendix: source

Thrown at extended_query_builder.go:36

// Build sets ParamValues, ParamFormats, and ResultFormats for use with *PgConn.ExecParams or *PgConn.ExecPrepared. If
// sd is nil then QueryExecModeExec behavior will be used.
func (eqb *ExtendedQueryBuilder) Build(m *pgtype.Map, sd *pgconn.StatementDescription, args []any) error {
	eqb.reset()

	if sd == nil {
		for i := range args {
			err := eqb.appendParam(m, 0, pgtype.TextFormatCode, args[i])
			if err != nil {
				err = fmt.Errorf("failed to encode args[%d]: %w", i, err)
				return err
			}
		}
		return nil
	}

	if len(sd.ParamOIDs) != len(args) {
		return fmt.Errorf("mismatched param and argument count")
	}

	for i := range args {
		err := eqb.appendParam(m, sd.ParamOIDs[i], -1, args[i])
		if err != nil {
			err = fmt.Errorf("failed to encode args[%d]: %w", i, err)
			return err
		}
	}

	for i := range sd.Fields {
		eqb.appendResultFormat(m.FormatCodeForOID(sd.Fields[i].DataTypeOID))
	}

	return nil
}

// appendParam appends a parameter to the query. format may be -1 to automatically choose the format. If arg is nil it

View on GitHub (pinned to ec1a0befd2)