jackc/pgx · error

array element OID not registered

Error message

array element OID not registered

What it means

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.

Source

Thrown at conn.go:1319

	var typtype string
	var typbasetype uint32

	err = c.QueryRow(ctx, "select typtype::text, typbasetype from pg_type where oid=$1", oid).Scan(&typtype, &typbasetype)
	if err != nil {
		return nil, err
	}

	switch typtype {
	case "b": // array
		elementOID, err := c.getArrayElementOID(ctx, oid)
		if err != nil {
			return nil, err
		}

		dt, ok := c.TypeMap().TypeForOID(elementOID)
		if !ok {
			return nil, errors.New("array element OID not registered")
		}

		return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.ArrayCodec{ElementType: dt}}, nil
	case "c": // composite
		fields, err := c.getCompositeFields(ctx, oid)
		if err != nil {
			return nil, err
		}

		return &pgtype.Type{Name: typeName, OID: oid, Codec: &pgtype.CompositeCodec{Fields: fields}}, nil
	case "d": // domain
		dt, ok := c.TypeMap().TypeForOID(typbasetype)
		if !ok {
			return nil, errors.New("domain base type OID not registered")
		}

		return &pgtype.Type{Name: typeName, OID: oid, Codec: dt.Codec}, nil
	case "e": // enum

View on GitHub (pinned to ec1a0befd2)

Solutions

  1. LoadType the element type first, register it on conn.TypeMap(), then LoadType the array type.
  2. Order LoadType calls so dependencies precede dependents (scalar → array, element → range/multirange, base → domain).
  3. Register the element type via pgtype.Map.RegisterType if you have a hand-built codec.
  4. If the element is itself custom, recurse until you reach a built-in type.

Example fix

// before
arrT, err := conn.LoadType(ctx, "_mood") // element 'mood' not registered
// -> "array element OID not registered"

// after
baseT, err := conn.LoadType(ctx, "mood")
if err != nil { return err }
conn.TypeMap().RegisterType(baseT)
arrT, err := conn.LoadType(ctx, "_mood")
Defensive patterns

Strategy: validation

Validate before calling

// ensure element type is known before loading the array
elemName := strings.TrimPrefix(arrayTypeName, "_")
if _, ok := conn.TypeMap().TypeForName(elemName); !ok {
    base, err := conn.LoadType(ctx, elemName)
    if err != nil { return err }
    conn.TypeMap().RegisterType(base)
}
arrT, err := conn.LoadType(ctx, arrayTypeName)

Type guard

func elementRegistered(m *pgtype.Map, elemOID uint32) bool { _, ok := m.TypeForOID(elemOID); return ok }

Try / catch

if t, err := conn.LoadType(ctx, name); err != nil {
    if strings.Contains(err.Error(), "array element OID not registered") {
        // load + register element type, then retry
    }
    return nil, err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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