cpostgresqlcomposite-types

In PostgreSQL, How can I identify a type is composite based on its type Oid in C function?


I am writing a trigger with C in PostgreSQL that would need to identify whether the type is composite or not based on its Oid in pg_type.

It is one of the few information that is not included in FormData_pg_attribute structure.

Can anyone help? Thanks a lot.


Solution

  • You could proceed like this (untested):

    #include "access/htup.h"
    #include "catalog/pg_type.h"
    #include "utils/syscache.h"
    
    bool is_composite(Oid typoid)
    {
        HeapTuple   tup;
        Form_pg_type typtup;
        bool result;
    
        tup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typoid));
        if (!HeapTupleIsValid(tup))
            elog(ERROR, "cache lookup failed for type %u", basetypoid);
        typtup = (Form_pg_type) GETSTRUCT(tup);
    
        result = (typtup->typtype == TYPTYPE_COMPOSITE);
    
        ReleaseSysCache(tup);
    
        return result;
    }