Code前端首页关于Code前端联系我们

了解 PostgreSQL 中每个表的 pg_RelationId

terry 2年前 (2023-09-26) 阅读数 55 #数据库

读取常规表或系统表时,会调用 heap_open 函数:

/* ----------------
 *        heap_open - open a heap relation by relation OID
 *
 *        This is essentially relation_open plus check that the relation
 *        is not an index nor a composite type.  (The caller should also
 *        check that it's not a view or foreign table before assuming it has
 *        storage.)
 * ----------------
 */
Relation
heap_open(Oid relationId, LOCKMODE lockmode)
{
    //fprintf(stderr,"++++++++++++++++++++ In heap_open start by process %d....relationId is:%d\n",
getpid(),relationId);
    Relation    r;

    r = relation_open(relationId, lockmode);

    if (r->rd_rel->relkind == RELKIND_INDEX)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("\"%s\" is an index",
                        RelationGetRelationName(r))));
    elseif (r->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
        ereport(ERROR,
                (errcode(ERRCODE_WRONG_OBJECT_TYPE),
                 errmsg("\"%s\" is a composite type",
                        RelationGetRelationName(r))));

    //fprintf(stderr,"++++++++++++++++++++ In heap_open end by process %d\n\n",getpid());

    return r;
}

对于常规表,RelationId 是文件夹下子文件夹中的文件名。

但是对于系统表来说就不同了。
例如pg_tablespace的RelationId是1213(这个已经硬编码在PostgreSQL源码中),
但是其对应的文件名是12587(对应global/122587文件)。

经过一番测试,发现对应关系如下:

pg_default_acl826
pg_pltemplate1136
_pg_table pg_type1247
pg_attribute pg_proc1255
pg_class 1259
pg_authid 1260
pg_auth_members1261
pg_database1262
pg_foreign_server1417
mapping_pg_usermapping_pg_user2328
pg_shdescription 2396
pg_aggregate2600
pg_am2601
pg_amop2602
pg_cast2605
pg_constraint2606
pg_conversion2607
pg_depend2608
pg_description2606
在她的pgpg_language2612
pg_largeobject2613
pg_namespace2615
pg_opclass2616
pg_operator I pg_rewrite 2618
pg_trigger2620
pg_opFamily2753
pg_db_role_设置2964
pg_LARGEOBJECT_METADADADTADADTADADATA_DAENSAINASION_ex 所以3596
pg_TS_DICT3600
pg_ts_parser3601
pg_ts_config3602
pg_ts_config_map❙pg进一步观察并完成上表:
/* ----------------
 *        relation_open - open any relation by relation OID
 *
 *        If lockmode is not "NoLock", the specified kind of lock is
 *        obtained on the relation.  (Generally, NoLock should only be
 *        used if the caller knows it has some appropriate lock on the
 *        relation already.)
 *
 *        An error is raised if the relation does not exist.
 *
 *        NB: a "relation" is anything with a pg_class entry.  The caller is
 *        expected to check whether the relkind is something it can handle.
 * ----------------
 */
Relation
relation_open(Oid relationId, LOCKMODE lockmode)
{

    fprintf(stderr,"___________________ In relation_open start by process %d\n",getpid());

    Relation    r;

    Assert(lockmode >= NoLock && lockmode < MAX_LOCKMODES);

    /* Get the lock before trying to open the relcache entry */if (lockmode != NoLock)
        LockRelationOid(relationId, lockmode);

    /* The relcache does all the real work... */
    r = RelationIdGetRelation(relationId);

    fprintf(stderr,"In relation_open ,the relNode is:%d....\n\n",r->);

    if (!RelationIsValid(r))
        elog(ERROR, "could not open relation with OID %u", relationId);

    /* Make note that we've accessed a temporary relation */if (RelationUsesLocalBuffers(r))
        MyXactAccessedTempRel = true;

    pgstat_initstats(r);

    fprintf(stderr,"___________________ In relation_open end by process %d\n",getpid());

    return r;
}

添加调试代码后可以看到_tablespace 的 pg RelationId 为 12 13,其对应的文件名是12587。

执行以下补充:

系统表名RelationId文件名ⓙpg_default pg _tablespace121312587
pg_shdepend121412598
pg_type 124712442
pg_attribute360012615
pg_proc 125512458
pg_class 125912465
pg_authid126012450
pg_auth_members126112594
pg_database360012615
_foreign_user pping141812454
pg_foreign_data_wrapper232812631
pg_shdescription239612602
pg_aggregate260012525
pg_am260112505
pg_amoppg_attrdef
pg_cast260512549
pg_constraintpg_conversion 260712562
pg_dependp❓ 261012489
pg_inherits261112485
pg_语言261212518
pg_largeobject261312571
pg_namespace261512558
pg_opclass261612501
pg _operator261712493
pg_rewrite261812528
pg_stastic261912436
pg_trigger262012535
pg_opfamily d pg_largeobject_metadata299512522
pg_extension307912627
pg_foreign_table311812639
pg_collat​​ion345612652
pg_enum359612646
359612646
pg_ts_dict360012615
pg_ts_pars呃360112619
pg_ts_config360212608
pg_ts_template376412623

如果你进一步看,你还可以找到以下系统表中的文件位于全局文件夹中,其余系统表对应的文件位于基础文件夹下的各个子文件夹中(一个子文件夹对应一个数据库):

系统表名称RelationId 文件名
p_pltemplate113612591
pg_tablespace121312587
pg_shdepend121412598
pg_authid126012450
pg_auth_members126112594
pg_databasedescriptpg_databaseg_db_ro le_setting296412581

版权声明

本文仅代表作者观点,不代表Code前端网立场。
本文系作者Code前端网发表,如需转载,请注明页面地址。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门