对PostgreSQL源代码中的 ObjectClass的初步理解

[作者:技术者高健@博客园  mail: [email protected] ]

depency.h 中:

/*                            

 * This enum covers all system catalogs whose OIDs can appear in                            

 * pg_depend.classId or pg_shdepend.classId.                            

 */                            

typedef enum ObjectClass                            

{                            

    OCLASS_CLASS,            /* pg_class */            

    OCLASS_PROC,            /* pg_proc */            

    OCLASS_TYPE,            /* pg_type */            

    OCLASS_CAST,            /* pg_cast */            

    OCLASS_COLLATION,            /* pg_collation */            

    OCLASS_CONSTRAINT,            /* pg_constraint */            

    OCLASS_CONVERSION,            /* pg_conversion */            

    OCLASS_DEFAULT,            /* pg_attrdef */            

    OCLASS_LANGUAGE,            /* pg_language */            

    OCLASS_LARGEOBJECT,            /* pg_largeobject */            

    OCLASS_OPERATOR,            /* pg_operator */            

    OCLASS_OPCLASS,            /* pg_opclass */            

    OCLASS_OPFAMILY,            /* pg_opfamily */            

    OCLASS_AMOP,            /* pg_amop */            

    OCLASS_AMPROC,            /* pg_amproc */            

    OCLASS_REWRITE,            /* pg_rewrite */            

    OCLASS_TRIGGER,            /* pg_trigger */            

    OCLASS_SCHEMA,            /* pg_namespace */            

    OCLASS_TSPARSER,            /* pg_ts_parser */            

    OCLASS_TSDICT,            /* pg_ts_dict */            

    OCLASS_TSTEMPLATE,            /* pg_ts_template */            

    OCLASS_TSCONFIG,            /* pg_ts_config */            

    OCLASS_ROLE,            /* pg_authid */            

    OCLASS_DATABASE,            /* pg_database */            

    OCLASS_TBLSPACE,            /* pg_tablespace */            

    OCLASS_FDW,            /* pg_foreign_data_wrapper */            

    OCLASS_FOREIGN_SERVER,            /* pg_foreign_server */            

    OCLASS_USER_MAPPING,            /* pg_user_mapping */            

    OCLASS_DEFACL,            /* pg_default_acl */            

    OCLASS_EXTENSION,            /* pg_extension */            

    MAX_OCLASS            /* MUST BE LAST */            

} ObjectClass;                            

而dependency.c 中又有如此的代码,看下:

[作者:技术者高健@博客园  mail: [email protected] ]

/*                    

 * This constant table maps ObjectClasses to the corresponding catalog OIDs.                    

 * See also getObjectClass().                    

 */                    

static const Oid object_classes[MAX_OCLASS] = {                    

    RelationRelationId,                /* OCLASS_CLASS */

    ProcedureRelationId,                /* OCLASS_PROC */

    TypeRelationId,                /* OCLASS_TYPE */

    CastRelationId,                /* OCLASS_CAST */

    CollationRelationId,                /* OCLASS_COLLATION */

    ConstraintRelationId,                /* OCLASS_CONSTRAINT */

    ConversionRelationId,                /* OCLASS_CONVERSION */

    AttrDefaultRelationId,                /* OCLASS_DEFAULT */

    LanguageRelationId,                /* OCLASS_LANGUAGE */

    LargeObjectRelationId,                /* OCLASS_LARGEOBJECT */

    OperatorRelationId,                /* OCLASS_OPERATOR */

    OperatorClassRelationId,                /* OCLASS_OPCLASS */

    OperatorFamilyRelationId,                /* OCLASS_OPFAMILY */

    AccessMethodOperatorRelationId,                /* OCLASS_AMOP */

    AccessMethodProcedureRelationId,                /* OCLASS_AMPROC */

    RewriteRelationId,                /* OCLASS_REWRITE */

    TriggerRelationId,                /* OCLASS_TRIGGER */

    NamespaceRelationId,                /* OCLASS_SCHEMA */

    TSParserRelationId,                /* OCLASS_TSPARSER */

    TSDictionaryRelationId,                /* OCLASS_TSDICT */

    TSTemplateRelationId,                /* OCLASS_TSTEMPLATE */

    TSConfigRelationId,                /* OCLASS_TSCONFIG */

    AuthIdRelationId,                /* OCLASS_ROLE */

    DatabaseRelationId,                /* OCLASS_DATABASE */

    TableSpaceRelationId,                /* OCLASS_TBLSPACE */

    ForeignDataWrapperRelationId,                /* OCLASS_FDW */

    ForeignServerRelationId,                /* OCLASS_FOREIGN_SERVER */

    UserMappingRelationId,                /* OCLASS_USER_MAPPING */

    DefaultAclRelationId,                /* OCLASS_DEFACL */

    ExtensionRelationId                /* OCLASS_EXTENSION */

}                    

可以看到上述两段代码,几乎是一行一行对应的。对于其目的,再看如下的代码:

/*                                    

 * Add an entry to an ObjectAddresses array.                                    

 *                                    

 * It is convenient to specify the class by ObjectClass rather than directly                                    

 * by catalog OID.                                    

 */                                    

static void                                    

add_object_address(ObjectClass oclass, Oid objectId, int32 subId,                                    

                   ObjectAddresses *addrs)                    

{                                    

    ObjectAddress *item;                                

                                    

    /* enlarge array if needed */                                

    if (addrs->numrefs >= addrs->maxrefs)                                

    {                                

        addrs->maxrefs *= 2;                            

        addrs->refs = (ObjectAddress *)                            

            repalloc(addrs->refs, addrs->maxrefs * sizeof(ObjectAddress));                        

        Assert(!addrs->extras);                            

    }                                

    /* record this item */                                

    item = addrs->refs + addrs->numrefs;                                

    item->classId = object_classes[oclass];                                

    item->objectId = objectId;                                

    item->objectSubId = subId;                                

    addrs->numrefs++;                                

}                                    

比如,当我用 的oclass 为 OCLASS_SCHEMA 的时候,我通过

item->classId = object_classes[oclass]; 得到的 classId 就是  NamespaceRelationId

再者,当我用 的oclass 为 OCLASS_TRIGGER 的时候,我通过

item->classId = object_classes[oclass]; 得到的 classId 就是  TriggerRelationId

[作者:技术者高健@博客园  mail: [email protected] ]

你可能感兴趣的:(PostgreSQL)