openGauss內(nèi)核分析(九):數(shù)據(jù)庫表的創(chuàng)建過程
除了DML之外的所有查詢都通過ProcessUtility模塊來執(zhí)行,包括了各類DDL語句、事務(wù)相關(guān)語句、游標相關(guān)語句等。上層調(diào)用函數(shù)為exec_simple_query函數(shù),其中PortalStart函數(shù)和PortalDrop函數(shù)部分較為簡單。核心函數(shù)是PortalRun函數(shù)下層調(diào)用的standard_ProcessUtility函數(shù),該函數(shù)通過switch case語句處理了各種類型的查詢語句,包括事務(wù)相關(guān)查詢、游標相關(guān)查詢、schema相關(guān)操作、表空間相關(guān)操作、表定義相關(guān)操作等。
standard_ProcessUtility函數(shù)會根據(jù)nodeTag(parsetree)的值來確定sql的操作類型,create table一般都是進入T_CreateStmt分支,調(diào)用CreateCommand函數(shù)。

CreateCommand函數(shù)先解析parse_tree獲取stmt,如果stmt為空則表明表已經(jīng)存在。如果stmt不為空對stmts進行遍歷,如果是?CreateStmt就調(diào)用DefineRelation。AlterTableCreateToastTable判斷是否需要創(chuàng)建toast表并創(chuàng)建,AlterCStoreCreateTables判斷是否需要創(chuàng)建列存表并創(chuàng)建。
?DefineRelation函數(shù)獲取到表名relname、名字空間relnamespace、表空間reltablespace、表類型relkind和relpersistence等信息后調(diào)用heap_create_with_catalog創(chuàng)建relation。
?heap_create_with_catalog主要完成表物理文件的創(chuàng)建和表元信息注冊到系統(tǒng)表中,涉及系統(tǒng)包包括pg_class,pg_attribute,pg_depend,pg_object,pg_type,pg_index和pg_partition。

其中heap_create內(nèi)部首先調(diào)用了RelationBuildLocalRelation創(chuàng)建RelationData,并加入到relCache,RelationData表示一個表的元信息,這些信息都可以由系統(tǒng)表元組中的信息構(gòu)造得到。然后根據(jù)這些信息通過調(diào)用RelalionCreateStorage函數(shù)創(chuàng)建物理文件。
附:創(chuàng)建表create table的函數(shù)調(diào)用棧