IamIC
IamIC

Reputation: 18249

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?

Upvotes: 1

Views: 1174

Answers (2)

Serpent7776
Serpent7776

Reputation: 92

Laurenz's answer is correct, but no longer compiles on Postgres 14. GetSysCacheOid* family of functions requires additional argument.

To get OID of a type it should now be:

typeOid = GetSysCacheOid2(TYPENAMENSP,
                          Anum_pg_type_oid,
                          CStringGetDatum(typeName),
                          ObjectIdGetDatum(typeNamespace));

To get OID of a namespace:

namespaceOid = GetSysCacheOid1(NAMESPACENAME,
                               Anum_pg_namespace_oid,
                               CStringGetDatum(namespaceName));

Upvotes: 1

Laurenz Albe
Laurenz Albe

Reputation: 246848

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));

Upvotes: 2

Related Questions