{"id":"432ca20e7c3cb646","repo":"jackc/pgx","slug":"too-many-result-format-codes","errorCode":null,"errorMessage":"too many result format codes","messagePattern":"too many result format codes","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgproto3/bind.go","lineNumber":144,"sourceCode":"\t\tdst = pgio.AppendInt16(dst, fc)\n\t}\n\n\tif len(src.Parameters) > math.MaxUint16 {\n\t\treturn nil, errors.New(\"too many parameters\")\n\t}\n\tdst = pgio.AppendUint16(dst, uint16(len(src.Parameters)))\n\tfor _, p := range src.Parameters {\n\t\tif p == nil {\n\t\t\tdst = pgio.AppendInt32(dst, -1)\n\t\t\tcontinue\n\t\t}\n\n\t\tdst = pgio.AppendInt32(dst, int32(len(p)))\n\t\tdst = append(dst, p...)\n\t}\n\n\tif len(src.ResultFormatCodes) > math.MaxUint16 {\n\t\treturn nil, errors.New(\"too many result format codes\")\n\t}\n\tdst = pgio.AppendUint16(dst, uint16(len(src.ResultFormatCodes)))\n\tfor _, fc := range src.ResultFormatCodes {\n\t\tdst = pgio.AppendInt16(dst, fc)\n\t}\n\n\treturn finishMessage(dst, sp)\n}\n\n// MarshalJSON implements encoding/json.Marshaler.\nfunc (src Bind) MarshalJSON() ([]byte, error) {\n\tformattedParameters := make([]map[string]string, len(src.Parameters))\n\tfor i, p := range src.Parameters {\n\t\tif p == nil {\n\t\t\tcontinue\n\t\t}\n\n\t\ttextFormat := true","sourceCodeStart":126,"sourceCodeEnd":162,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgproto3/bind.go#L126-L162","documentation":"Returned by Bind.Encode in pgproto3/bind.go:144 when len(src.ResultFormatCodes) exceeds 65535 (math.MaxUint16). The wire protocol encodes the result-format-code count as a uint16, so more than 65535 codes cannot be serialized. Fired client-side during Encode, before any network I/O. Since result format codes map to result columns, this also implies the query selects more than 65535 columns - itself far above PostgreSQL's real column limit.","triggerScenarios":"Constructing a Bind with an explicit ResultFormatCodes slice longer than 65535, or a SELECT whose column count exceeds the limit. Almost always an application bug (pgx normally emits one code per result column or a single default code).","commonSituations":"Selecting a huge generated column set; copying a result column count into ResultFormatCodes in a loop without a cap; building a custom query tool that mirrors an oversized schema.","solutions":["Cap ResultFormatCodes at 65535; for uniform result format use a single code (one code applies to all columns).","Reduce the selected column count to what the application actually needs (SELECT explicit columns, not a generated superset).","Validate column count against the protocol limit before issuing the query."],"exampleFix":"// before: one result format code per column, exceeds 65535\nresultFormats := make([]int16, numCols) // numCols > 65535 -> \"too many result format codes\"\n\n// after: a single code applies to all result columns\nresultFormats := []int16{1} // binary format for every column","handlingStrategy":"validation","validationCode":"const maxResultFormatCodes = 65535 // math.MaxUint16\nif len(resultFormatCodes) > maxResultFormatCodes {\n    // Use a single code: the protocol applies one code to ALL result columns.\n    resultFormatCodes = []int16{resultFormatCodes[0]}\n}","typeGuard":"func bindResultFormatCodesOK(codes []int16) bool { return len(codes) <= 65535 }","tryCatchPattern":"if _, err := bindMsg.Encode(dst); err != nil {\n    if strings.Contains(err.Error(), \"too many result format codes\") {\n        bindMsg.ResultFormatCodes = []int16{bindMsg.ResultFormatCodes[0]}\n    }\n}","preventionTips":["Use at most one ResultFormatCode when all columns share a format (protocol applies it to all columns).","Select only the columns you need instead of a generated superset.","Validate selected column count against the 65535 limit before issuing the query."],"tags":["bind","protocol","encode","parameter-limit","pgproto3"],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}