openGauss内核分析:数据库表的创建过程


风晓
风晓 2024-01-15 09:34:20 48557 赞同 0 反对 0
分类: 资源 标签: 国产数据库
openGauss内核分析:数据库表的创建过程
除了DML之外的所有查询都通过ProcessUtility模块来执行,包括了各类DDL语句、事务相关语句、游标相关语句等。上层调用函数为exec_simple_query函数,其中PortalStart函数和PortalDrop函数部分较为简单。核心函数是PortalRun函数下层调用的standard_ProcessUtility函数,该函数通过switch case语句处理了各种类型的查询语句,包括事务相关查询、游标相关查询、schema相关操作、表空间相关操作、表定义相关操作等。

standard_ProcessUtility函数会根据nodeTag(parsetree)的值来确定sql的操作类型,create table一般都是进入T_CreateStmt分支,调用CreateCommand函数。

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
void standard_ProcessUtility(Node* parse_tree, const char* query_string, ParamListInfo params, bool is_top_level,    DestReceiver* dest,#ifdef PGXC    bool sent_to_remote,#endif /* PGXC */    char* completion_tag,    bool isCTAS){……    errno_t errorno = EOK;switch (nodeTag(parse_tree)) { // 根据nodeTag(parsetree)的值来确定sql的操作类型……        case T_CreateStmt: { // create table#ifdef PGXC            CreateCommand((CreateStmt*)parse_tree, query_string, params, is_top_level, sent_to_remote, isCTAS);#else            CreateCommand((CreateStmt*)parse_tree, query_string, params, is_top_level, isCTAS);#endif        } break;……

图片

CreateCommand函数先解析parse_tree获取stmt,如果stmt为空则表明表已经存在。如果stmt不为空对stmts进行遍历,如果是 CreateStmt就调用DefineRelation。AlterTableCreateToastTable判断是否需要创建toast表并创建,AlterCStoreCreateTables判断是否需要创建列存表并创建。

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
#ifdef PGXCvoid CreateCommand(CreateStmt *parse_tree, const char *query_string, ParamListInfo params,                   bool is_top_level, bool sent_to_remote, bool isCTAS)#elsevoid CreateCommand(CreateStmt *parse_tree, const char *query_string, ParamListInfo params, bool is_top_level,    bool isCTAS)#endif{……    /* Run parse analysis ... */    if (u_sess->attr.attr_sql.enable_parallel_ddl) // 先解析parse_tree获取stmt        stmts = transformCreateStmt((CreateStmt*)parse_tree, query_string, NIL, true, &namespace_id, is_first_node);    else        stmts = transformCreateStmt((CreateStmt*)parse_tree, query_string, NIL, false, &namespace_id);
    /*     * If stmts is NULL, then the table is exists.     * we need record that for searching the group of table.     */    if (stmts == NIL) { // 如果stmt为空则表明表已经存在        table_is_exist = true;……    /* ... and do it */    foreach (l, stmts) { // 遍历stmts Node* stmt = (Node*)lfirst(l);
        if (IsA(stmt, CreateStmt)) {  // 如果是 CreateStmt就调用DefineRelation            Datum toast_options; static const char* const validnsps[] = HEAP_RELOPT_NAMESPACES;                       /* forbid user to set or change inner options */ ForbidOutUsersToSetInnerOptions(((CreateStmt*)stmt)->options);                        /* Create the table itself */            rel_oid = DefineRelation((CreateStmt*)stmt,                                    ((CreateStmt*)stmt)->relkind == RELKIND_MATVIEW ?                                                                    RELKIND_MATVIEW : RELKIND_RELATION,                                                                                                        InvalidOid, isCTAS);……            AlterTableCreateToastTable(rel_oid, toast_options, AccessShareLock);            AlterCStoreCreateTables(rel_oid, toast_options, (CreateStmt*)stmt);            AlterDfsCreateTables(rel_oid, toast_options, (CreateStmt*)stmt);            AlterCreateChainTables(rel_oid, toast_options, (CreateStmt *)stmt);……

DefineRelation函数获取到表名relname、名字空间relnamespace、表空间reltablespace、表类型relkind和relpersistence等信息后调用heap_create_with_catalog创建relation。

  •  
  •  
  •  
  •  
  •  
  •  
(gdb) f 0#0  heap_create_with_catalog (relname=0x7fb4fa872140 "t100", relnamespace=2200, reltablespace=0, relid=0, reltypeid=0,    reloftypeid=0, ownerid=10, tupdesc=0x7fb4ff2e2e50, cooked_constraints=0x0, relkind=114 'r', relpersistence=112 'p',    shared_relation=false, mapped_relation=false, oidislocal=false, oidinhcount=0, oncommit=ONCOMMIT_NOOP,    reloptions=140415352057720, use_user_acl=true, allow_system_table_mods=false, partTableState=0x0, row_compress=1 '\001',    bucketinfo=0x0, record_dependce=true, ceLst=0x0, storage_type=HEAP_DISK, partLockMode=1) at heap.cpp:2521

其中heap_create内部首先调用了RelationBuildLocalRelation创建RelationData,并加入到relCache,RelationData表示一个表的元信息,这些信息都可以由系统表元组中的信息构造得到。然后根据这些信息通过调用RelalionCreateStorage函数创建物理文件。heap_create_with_catalog主要完成表物理文件的创建和表元信息注册到系统表中,涉及系统包包括pg_class,pg_attribute,pg_depend,pg_object,pg_type,pg_index和pg_partition。

图片

:创建表create table的函数调用栈

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
#0  RelationCreateStorage#1  heap_create#2  heap_create_with_catalog#3  DefineRelation#4  CreateCommand#5  standard_ProcessUtility#6  gsaudit_ProcessUtility_hook#7  pgaudit_ProcessUtility#8  hypo_utility_hook#9  ProcessUtility#10 PortalRunUtility#11 PortalRunMulti#12 PortalRun#13 exec_simple_query#14 PostgresMain

如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!

评价 0 条
风晓L1
粉丝 1 资源 2038 + 关注 私信
最近热门资源
银河麒麟桌面操作系统备份用户数据  127
统信桌面专业版【全盘安装UOS系统】介绍  122
银河麒麟桌面操作系统安装佳能打印机驱动方法  114
银河麒麟桌面操作系统 V10-SP1用户密码修改  105
最近下载排行榜
银河麒麟桌面操作系统备份用户数据 0
统信桌面专业版【全盘安装UOS系统】介绍 0
银河麒麟桌面操作系统安装佳能打印机驱动方法 0
银河麒麟桌面操作系统 V10-SP1用户密码修改 0
作者收入月榜
1

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益209.03元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!