jackc/pgx · error

expected values, got values

Error message

expected %d values, got %d values

What it means

The CopySource passed to CopyFrom yielded a row whose value count differs from the number of column names given to CopyFrom. Each row must supply exactly one value per target column.

Solutions

  1. Make the CopySource's Values() return exactly len(columnNames) values per row
  2. Verify the column list passed to CopyFrom matches the source data shape
  3. Check for off-by-one slicing when building each row
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at copy_from.go:230 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/28a8406ab574bb23. Report an issue: GitHub.

Appendix: source

Thrown at copy_from.go:230

	}

	return commandTag.RowsAffected(), err
}

func (ct *copyFrom) buildCopyBuf(buf []byte, sd *pgconn.StatementDescription) (bool, []byte, error) {
	const sendBufSize = 65536 - 5 // The packet has a 5-byte header
	lastBufLen := 0
	largestRowLen := 0

	for ct.rowSrc.Next() {
		lastBufLen = len(buf)

		values, err := ct.rowSrc.Values()
		if err != nil {
			return false, nil, err
		}
		if len(values) != len(ct.columnNames) {
			return false, nil, fmt.Errorf("expected %d values, got %d values", len(ct.columnNames), len(values))
		}

		buf = pgio.AppendInt16(buf, int16(len(ct.columnNames)))
		for i, val := range values {
			buf, err = encodeCopyValue(ct.conn.typeMap, buf, sd.Fields[i].DataTypeOID, val)
			if err != nil {
				return false, nil, err
			}
		}

		rowLen := len(buf) - lastBufLen
		if rowLen > largestRowLen {
			largestRowLen = rowLen
		}

		// Try not to overflow size of the buffer PgConn.CopyFrom will be reading into. If that happens then the nature of
		// io.Pipe means that the next Read will be short. This can lead to pathological send sizes such as 65531, 13, 65531
		// 13, 65531, 13, 65531, 13.

View on GitHub (pinned to ec1a0befd2)