{"id":"5493a09e56b9f98c","repo":"jackc/pgx","slug":"array-element-oid-not-registered","errorCode":null,"errorMessage":"array element OID not registered","messagePattern":"array element OID not registered","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"conn.go","lineNumber":1319,"sourceCode":"\n\tvar typtype string\n\tvar typbasetype uint32\n\n\terr = c.QueryRow(ctx, \"select typtype::text, typbasetype from pg_type where oid=$1\", oid).Scan(&typtype, &typbasetype)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tswitch typtype {\n\tcase \"b\": // array\n\t\telementOID, err := c.getArrayElementOID(ctx, oid)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\tdt, ok := c.TypeMap().TypeForOID(elementOID)\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"array element OID not registered\")\n\t\t}\n\n\t\treturn &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.ArrayCodec{ElementType: dt}}, nil\n\tcase \"c\": // composite\n\t\tfields, err := c.getCompositeFields(ctx, oid)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\n\t\treturn &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.CompositeCodec{Fields: fields}}, nil\n\tcase \"d\": // domain\n\t\tdt, ok := c.TypeMap().TypeForOID(typbasetype)\n\t\tif !ok {\n\t\t\treturn nil, errors.New(\"domain base type OID not registered\")\n\t\t}\n\n\t\treturn &pgtype.Type{Name: typeName, OID: oid, Codec: dt.Codec}, nil\n\tcase \"e\": // enum","sourceCodeStart":1301,"sourceCodeEnd":1337,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/conn.go#L1301-L1337","documentation":"Returned by Conn.LoadType for a typtype='b' (array) type when the element type's OID is not present in the connection's TypeMap (TypeForOID returned false at conn.go:1317-1319). LoadType only assembles array/composite/domain/range/multirange codecs from already-understood components; it cannot invent the element codec. You must register the base element type first.","triggerScenarios":"Calling Conn.LoadType(ctx, \"_mytype\") (or any custom array type) before the scalar element type 'mytype' has itself been loaded and registered into conn.TypeMap().TypeMap.RegisterType.","commonSituations":"Loading types in the wrong order (array before scalar); a custom scalar type that was never LoadType'd; an enum/composite that was created but not registered before its array form was requested.","solutions":["LoadType the element type first, register it on conn.TypeMap(), then LoadType the array type.","Order LoadType calls so dependencies precede dependents (scalar → array, element → range/multirange, base → domain).","Register the element type via pgtype.Map.RegisterType if you have a hand-built codec.","If the element is itself custom, recurse until you reach a built-in type."],"exampleFix":"// before\narrT, err := conn.LoadType(ctx, \"_mood\") // element 'mood' not registered\n// -> \"array element OID not registered\"\n\n// after\nbaseT, err := conn.LoadType(ctx, \"mood\")\nif err != nil { return err }\nconn.TypeMap().RegisterType(baseT)\narrT, err := conn.LoadType(ctx, \"_mood\")","handlingStrategy":"validation","validationCode":"// ensure element type is known before loading the array\nelemName := strings.TrimPrefix(arrayTypeName, \"_\")\nif _, ok := conn.TypeMap().TypeForName(elemName); !ok {\n    base, err := conn.LoadType(ctx, elemName)\n    if err != nil { return err }\n    conn.TypeMap().RegisterType(base)\n}\narrT, err := conn.LoadType(ctx, arrayTypeName)","typeGuard":"func elementRegistered(m *pgtype.Map, elemOID uint32) bool { _, ok := m.TypeForOID(elemOID); return ok }","tryCatchPattern":"if t, err := conn.LoadType(ctx, name); err != nil {\n    if strings.Contains(err.Error(), \"array element OID not registered\") {\n        // load + register element type, then retry\n    }\n    return nil, err\n}","preventionTips":["LoadType in dependency order: scalar before array.","Maintain a sorted registration list so dependents come after dependencies.","Register custom element codecs before loading their containers."],"tags":["types","loadtype","registration","array"],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}