对PostgreSQL中 pg_各表的RelationId的认识

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

读取普通的table或者系统表,都会调用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)))); else if (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就是在base目录下的某个子目录里面的文件名。

但是对于系统表而言,则不同。
比如 pg_tablespace 的RelationId为 1213(这已经写死在PostgreSQL源代码中),
但是其对应的文件的名称为 12587(对应global/12587文件)。

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

 

 pg_default_acl 826
pg_pltemplate 1136
pg_tablespace 1213
pg_shdepend 1214
pg_type 1247
pg_attribute 1249
pg_proc 1255
pg_class 1259
pg_authid 1260
pg_auth_members 1261
pg_database  1262
pg_foreign_server 1417
pg_user_mapping 1418
pg_foreign_data_wrapper 2328
pg_shdescription 2396
pg_aggregate 2600
pg_am 2601
pg_amop 2602
pg_ampro 2603
pg_attrdef 2604
pg_cast 2605
pg_constraint 2606
pg_conversion 2607
pg_depend 2608
pg_description 2609
pg_index 2610
pg_inherits 2611
pg_language 2612
pg_largeobject 2613
pg_namespace 2615
pg_opclass 2616
pg_operator 2617
pg_rewrite 2618
pg_stastic 2619
pg_trigger 2620
pg_opfamily 2753
pg_db_role_setting 2964
pg_largeobject_metadata 2995
pg_extension 3079
pg_foreign_table 3118
pg_collation 3456
pg_enum 3501
pg_seclabel 3596
pg_ts_dict 3600
pg_ts_parser 3601
pg_ts_config 3602
pg_ts_config_map 3603
pg_ts_template 3764

 然后,我还可以进一步,观察 ,把上述表格补充完整:

复制代码
/* ----------------
 *        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->rd_node.relNode);

    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;
}
复制代码

加入了调试代码后,我可以看到,pg_tablespace 的 RelationId是 1213,而它的对应文件名是 12587。

下面,补充完整:

system table name RelationId FileName
pg_default_acl 826 12642
pg_pltemplate 1136 12591
pg_tablespace 1213 12587
pg_shdepend 1214 12598
pg_type 1247 12442
pg_attribute 1249 12446
pg_proc 1255 12458
pg_class 1259 12465
pg_authid 1260 12450
pg_auth_members 1261 12594
pg_database  1262 12692
pg_foreign_server 1417 12635
pg_user_mapping 1418 12454
pg_foreign_data_wrapper 2328 12631
pg_shdescription 2396 12602
pg_aggregate 2600 12525
pg_am 2601 12505
pg_amop 2602 12509
pg_ampro 2603 12514
pg_attrdef 2604 12469
pg_cast 2605 12549
pg_constraint 2606 12476
pg_conversion 2607 12562
pg_depend 2608 12567
pg_description 2609 12543
pg_index 2610 12489
pg_inherits 2611 12485
pg_language 2612 12518
pg_largeobject 2613 12571
pg_namespace 2615 12558
pg_opclass 2616 12501
pg_operator 2617 12493
pg_rewrite 2618 12528
pg_stastic 2619 12436
pg_trigger 2620 12535
pg_opfamily 2753 12497
pg_db_role_setting 2964 12581
pg_largeobject_metadata 2995 12522
pg_extension 3079 12627
pg_foreign_table 3118 12639
pg_collation 3456 12652
pg_enum 3501 12553
pg_seclabel 3596 12646
pg_ts_dict 3600 12615
pg_ts_parser 3601 12619
pg_ts_config 3602 12608
pg_ts_config_map 3603 12612
pg_ts_template 3764 12623

 如果进一步查看,还可以发现:

只有如下几个系统表的对应文件位于 global目录,其余的系统表的对应文件则是base目录下的每个子目录中都有(一个子目录对应一个数据库):

system table name RelationId FileName
pg_pltemplate 1136 12591
pg_tablespace 1213 12587
pg_shdepend 1214 12598
pg_authid 1260 12450
pg_auth_members 1261 12594
pg_database  1262 12692
pg_shdescription 2396 12602
pg_db_role_setting 2964

12581









本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/p/3169560.html,如需转载请自行联系原作者


相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
6天前
|
存储 关系型数据库 Java
polardb有没有搞过pg 全量及增量备份管理的
【1月更文挑战第3天】【1月更文挑战第11篇】 polardb有没有搞过pg 全量及增量备份管理的
44 1
|
6天前
|
关系型数据库 Serverless 分布式数据库
高峰无忧,探索PolarDB PG版Serverless的弹性魅力
在数字经济时代,数据库成为企业命脉,面对爆炸式增长的数据,企业面临管理挑战。云原生和Serverless技术革新数据库领域,PolarDB PG Serverless作为阿里云的云原生数据库解决方案,融合Serverless与PostgreSQL,实现自动弹性扩展,按需计费,降低运维成本。它通过计算与存储分离技术,提供高可用性、灾备策略和简化运维。PolarDB PG Serverless智能应变业务峰值,实时监控与调整资源,确保性能稳定。通过免费体验,用户可观察其弹性性能和价格力,感受技术优势。
|
6天前
|
关系型数据库 测试技术 Serverless
5分钟免费体验PolarDB PG版Serverless的极致弹性!
基于阿里云瑶池数据库解决方案体验馆,带你体验PolarDB PG版 Serverless形态下的性能压测环境,基于可选择的标准压测工具进行压测,构造弹性场景进行压测,实时动态展示弹性能力、价格和性价比结果,压测环境可开放定制修改、可重复验证。参与活动即有机会获得小爱随身音响、体脂秤、极客时间VIP月卡、鼠标垫等精美礼品。
|
6天前
|
负载均衡 监控 关系型数据库
PostgreSQL从小白到高手教程 - 第48讲:PG高可用实现keepalived
PostgreSQL技术大讲堂 - 第48讲:PG高可用实现keepalived
77 1
|
6天前
|
SQL 关系型数据库 数据库
PostgreSQL从小白到高手教程 - 第44讲:pg流复制部署
PostgreSQL技术大讲堂 - 第44讲:pg流复制部署
83 0
|
6天前
|
SQL 关系型数据库 数据库
postgresql|数据库|pg数据库的文件系统详解---最全面的解析
postgresql|数据库|pg数据库的文件系统详解---最全面的解析
162 0
|
6天前
|
安全 关系型数据库 数据库
上新|阿里云RDS PostgreSQL支持PG 16版本,AliPG提供丰富自研能力
AliPG在社区版16.0的基础上,在安全、成本、可运维性等多个方面做了提升,丰富的内核/插件特性支持,满足业务场景的需求
|
5月前
|
关系型数据库 数据库 PostgreSQL
flink postgresql cdc实时同步(含pg安装配置等)
flink postgresql cdc实时同步(含pg安装配置等)
214 0
|
8月前
|
消息中间件 关系型数据库 分布式数据库
PolarDB for PG
PolarDB for PG
112 1
|
10月前
|
SQL 关系型数据库 数据库
flink postgresql cdc实时同步(含pg安装配置等)
flink postgresql cdc实时同步(含pg安装配置等)
1922 2