设为首页 收藏本站
查看: 2220|回复: 0

[经验分享] 对PostgreSQL中 pg_各表的RelationId的认识

[复制链接]

尚未签到

发表于 2016-11-21 10:44:20 | 显示全部楼层 |阅读模式
  读取普通的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_acl826
pg_pltemplate1136
pg_tablespace1213
pg_shdepend1214
pg_type1247
pg_attribute1249
pg_proc1255
pg_class1259
pg_authid1260
pg_auth_members1261
pg_database 1262
pg_foreign_server1417
pg_user_mapping1418
pg_foreign_data_wrapper2328
pg_shdescription2396
pg_aggregate2600
pg_am2601
pg_amop2602
pg_ampro2603
pg_attrdef2604
pg_cast2605
pg_constraint2606
pg_conversion2607
pg_depend2608
pg_description2609
pg_index2610
pg_inherits2611
pg_language2612
pg_largeobject2613
pg_namespace2615
pg_opclass2616
pg_operator2617
pg_rewrite2618
pg_stastic2619
pg_trigger2620
pg_opfamily2753
pg_db_role_setting2964
pg_largeobject_metadata2995
pg_extension3079
pg_foreign_table3118
pg_collation3456
pg_enum3501
pg_seclabel3596
pg_ts_dict3600
pg_ts_parser3601
pg_ts_config3602
pg_ts_config_map3603
pg_ts_template3764
  然后,我还可以进一步,观察 ,把上述表格补充完整:



/* ----------------
*        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 nameRelationIdFileName
pg_default_acl82612642
pg_pltemplate113612591
pg_tablespace121312587
pg_shdepend121412598
pg_type124712442
pg_attribute124912446
pg_proc125512458
pg_class125912465
pg_authid126012450
pg_auth_members126112594
pg_database 126212692
pg_foreign_server141712635
pg_user_mapping141812454
pg_foreign_data_wrapper232812631
pg_shdescription239612602
pg_aggregate260012525
pg_am260112505
pg_amop260212509
pg_ampro260312514
pg_attrdef260412469
pg_cast260512549
pg_constraint260612476
pg_conversion260712562
pg_depend260812567
pg_description260912543
pg_index261012489
pg_inherits261112485
pg_language261212518
pg_largeobject261312571
pg_namespace261512558
pg_opclass261612501
pg_operator261712493
pg_rewrite261812528
pg_stastic261912436
pg_trigger262012535
pg_opfamily275312497
pg_db_role_setting296412581
pg_largeobject_metadata299512522
pg_extension307912627
pg_foreign_table311812639
pg_collation345612652
pg_enum350112553
pg_seclabel359612646
pg_ts_dict360012615
pg_ts_parser360112619
pg_ts_config360212608
pg_ts_config_map360312612
pg_ts_template376412623
  如果进一步查看,还可以发现:
  只有如下几个系统表的对应文件位于 global目录,其余的系统表的对应文件则是base目录下的每个子目录中都有(一个子目录对应一个数据库):

system table nameRelationIdFileName
pg_pltemplate113612591
pg_tablespace121312587
pg_shdepend121412598
pg_authid126012450
pg_auth_members126112594
pg_database 126212692
pg_shdescription239612602
pg_db_role_setting296412581

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-303374-1-1.html 上篇帖子: 得到PostgreSQL 数据库表字段的部分信息(Name,Type,Len,PK,AutoIncrease,AllowNullable) 下篇帖子: PostgreSQL数据库备份与恢复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表