postgresqlpostgresql-9.5postgresql-9.6

What's the correct OID for an array of composite type in PostgreSQL?


Under PostgreSQL 9.5, in a C function I wrote, ARR_ELEMTYPE(PG_GETARG_ARRAYTYPE_P(0)) where (0) is an array of composite type (i.e. defined by CREATE TYPE) returned 28642010. This number doesn't appear in the source code and is undocumented.

Today, I upgraded to PG 9.6 and my C function threw an error. The elemtype is now reported as 16396. This also does not appear in the source code.

I searched both numbers in decimal and hex in both the wiki and the source.

I can't see how a) the OID isn't defined in the source code and b) the number changes with a change of PG.

Am I missing something here?


Solution

  • The Oid of any object you create in a database, including a type, is assigned by the system and will change if you dump the database and restore it to a different one.

    To figure out the Oid of a type in a C function from its name and the Oid of its schema, you can do something like this:

    typoid = GetSysCacheOid2(TYPENAMENSP,
                             CStringGetDatum(typeName),
                             ObjectIdGetDatum(typeNamespace));