postgres源码解析38 表创建执行全流程梳理--4

news2024/12/25 14:30:24

本文讲解非系统表的创建逻辑([<fontcolor=0000dd>普通表和索引表]),其入口函数为heap_create,内部公共接口函数为RelationBuildLocalRelation和RelationCreateStorage相关知识回顾见:
postgres源码解析38 表创建执行全流程梳理–1
postgres源码解析38 表创建执行全流程梳理–2
postgres源码解析38 表创建执行全流程梳理–3

heap_create 执行逻辑

在这里插入图片描述
1)首先进行安全性检查,不允许在系统表中创建relations,判断是否需要创建持久化文件等;
2)根据表名、表空间、表对象标识符和文件节点relfilenode等信息调用 RelationBuildLocalRelation在内存中构建Relation,并插入全局relcache 哈希表中;
3)结合relation类型调用相应的接口函数进行relation的创建,[普通表/TOAST/物化视图: table_relation_set_new_filenode,索引/序列:RelationCreateStorage];
4) 对于无需创建持久化的relation且用户指定表空间,则需要在 pg_tablespace 中注册对应的信息。

RelationBuildLocalRelation

该函数目的是在内存中构建创建表的relcache Entry,并插入全局Relcache 哈希表中,用于加速后续对此表的访问。
1)如果不存在CacheMemoryContext,则创建此上下文,后续操作均在此上下文进行;
2)分配并初始化Relation结构体,结合入参的TueDesc填充Relation结构体中rd_att字段:字段属性的详细信息;
3)分配并根据入参填充Relation结构体中rd_att字段的Form_pg_class字段:表名、命名空间、字段属性/数目等;
4)调用 RelationInitLockInfo初始化relation描述符锁信息;
5)调用 RelationInitPhysicalAddr 初始化relation描述符对应的物理地址:spcNode/dbNode//RelNode [表空间/数据库/表]
6)将上述构建好的RelCache Entry插入全局ralcache 哈希表中,并增加该条目的引用计数
在这里插入图片描述

*
 *		RelationBuildLocalRelation
 *			Build a relcache entry for an about-to-be-created relation,
 *			and enter it into the relcache.
 */
Relation
RelationBuildLocalRelation(const char *relname,
						   Oid relnamespace,
						   TupleDesc tupDesc,
						   Oid relid,
						   Oid accessmtd,
						   Oid relfilenode,
						   Oid reltablespace,
						   bool shared_relation,
						   bool mapped_relation,
						   char relpersistence,
						   char relkind)
{
	Relation	rel;
	MemoryContext oldcxt;
	int			natts = tupDesc->natts;
	int			i;
	bool		has_not_null;
	bool		nailit;

	AssertArg(natts >= 0);

	/*
	 * check for creation of a rel that must be nailed in cache.
	 *
	 * XXX this list had better match the relations specially handled in
	 * RelationCacheInitializePhase2/3.
	 */
	switch (relid)
	{
		case DatabaseRelationId:
		case AuthIdRelationId:
		case AuthMemRelationId:
		case RelationRelationId:
		case AttributeRelationId:
		case ProcedureRelationId:
		case TypeRelationId:
			nailit = true;
			break;
		default:
			nailit = false;
			break;
	}

	/*
	 * check that hardwired list of shared rels matches what's in the
	 * bootstrap .bki file.  If you get a failure here during initdb, you
	 * probably need to fix IsSharedRelation() to match whatever you've done
	 * to the set of shared relations.
	 */
	if (shared_relation != IsSharedRelation(relid))
		elog(ERROR, "shared_relation flag for \"%s\" does not match IsSharedRelation(%u)",
			 relname, relid);

	/* Shared relations had better be mapped, too */
	Assert(mapped_relation || !shared_relation);

	/*
	 * switch to the cache context to create the relcache entry.
	 */
	if (!CacheMemoryContext)
		CreateCacheMemoryContext();

	oldcxt = MemoryContextSwitchTo(CacheMemoryContext);

	/*
	 * allocate a new relation descriptor and fill in basic state fields.
	 */
	rel = (Relation) palloc0(sizeof(RelationData));

	/* make sure relation is marked as having no open file yet */
	rel->rd_smgr = NULL;

	/* mark it nailed if appropriate */
	rel->rd_isnailed = nailit;

	rel->rd_refcnt = nailit ? 1 : 0;

	/* it's being created in this transaction */
	rel->rd_createSubid = GetCurrentSubTransactionId();
	rel->rd_newRelfilenodeSubid = InvalidSubTransactionId;
	rel->rd_firstRelfilenodeSubid = InvalidSubTransactionId;
	rel->rd_droppedSubid = InvalidSubTransactionId;

	/*
	 * create a new tuple descriptor from the one passed in.  We do this
	 * partly to copy it into the cache context, and partly because the new
	 * relation can't have any defaults or constraints yet; they have to be
	 * added in later steps, because they require additions to multiple system
	 * catalogs.  We can copy attnotnull constraints here, however.
	 */
	rel->rd_att = CreateTupleDescCopy(tupDesc);
	rel->rd_att->tdrefcount = 1;	/* mark as refcounted */
	has_not_null = false;
	for (i = 0; i < natts; i++)
	{
		Form_pg_attribute satt = TupleDescAttr(tupDesc, i);
		Form_pg_attribute datt = TupleDescAttr(rel->rd_att, i);

		datt->attidentity = satt->attidentity;
		datt->attgenerated = satt->attgenerated;
		datt->attnotnull = satt->attnotnull;
		has_not_null |= satt->attnotnull;
	}

	if (has_not_null)
	{
		TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));

		constr->has_not_null = true;
		rel->rd_att->constr = constr;
	}

	/*
	 * initialize relation tuple form (caller may add/override data later)
	 */
	rel->rd_rel = (Form_pg_class) palloc0(CLASS_TUPLE_SIZE);

	namestrcpy(&rel->rd_rel->relname, relname);
	rel->rd_rel->relnamespace = relnamespace;

	rel->rd_rel->relkind = relkind;
	rel->rd_rel->relnatts = natts;
	rel->rd_rel->reltype = InvalidOid;
	/* needed when bootstrapping: */
	rel->rd_rel->relowner = BOOTSTRAP_SUPERUSERID;

	/* set up persistence and relcache fields dependent on it */
	rel->rd_rel->relpersistence = relpersistence;
	switch (relpersistence)
	{
		case RELPERSISTENCE_UNLOGGED:
		case RELPERSISTENCE_PERMANENT:
			rel->rd_backend = InvalidBackendId;
			rel->rd_islocaltemp = false;
			break;
		case RELPERSISTENCE_TEMP:
			Assert(isTempOrTempToastNamespace(relnamespace));
			rel->rd_backend = BackendIdForTempRelations();
			rel->rd_islocaltemp = true;
			break;
		default:
			elog(ERROR, "invalid relpersistence: %c", relpersistence);
			break;
	}

	/* if it's a materialized view, it's not populated initially */
	if (relkind == RELKIND_MATVIEW)
		rel->rd_rel->relispopulated = false;
	else
		rel->rd_rel->relispopulated = true;

	/* set replica identity -- system catalogs and non-tables don't have one */
	if (!IsCatalogNamespace(relnamespace) &&
		(relkind == RELKIND_RELATION ||
		 relkind == RELKIND_MATVIEW ||
		 relkind == RELKIND_PARTITIONED_TABLE))
		rel->rd_rel->relreplident = REPLICA_IDENTITY_DEFAULT;
	else
		rel->rd_rel->relreplident = REPLICA_IDENTITY_NOTHING;

	/*
	 * Insert relation physical and logical identifiers (OIDs) into the right
	 * places.  For a mapped relation, we set relfilenode to zero and rely on
	 * RelationInitPhysicalAddr to consult the map.
	 */
	rel->rd_rel->relisshared = shared_relation;

	RelationGetRelid(rel) = relid;

	for (i = 0; i < natts; i++)
		TupleDescAttr(rel->rd_att, i)->attrelid = relid;

	rel->rd_rel->reltablespace = reltablespace;

	if (mapped_relation)
	{
		rel->rd_rel->relfilenode = InvalidOid;
		/* Add it to the active mapping information */
		RelationMapUpdateMap(relid, relfilenode, shared_relation, true);
	}
	else
		rel->rd_rel->relfilenode = relfilenode;

	RelationInitLockInfo(rel);	/* see lmgr.c */

	RelationInitPhysicalAddr(rel);

	rel->rd_rel->relam = accessmtd;

	/*
	 * RelationInitTableAccessMethod will do syscache lookups, so we mustn't
	 * run it in CacheMemoryContext.  Fortunately, the remaining steps don't
	 * require a long-lived current context.
	 */
	MemoryContextSwitchTo(oldcxt);

	if (relkind == RELKIND_RELATION ||
		relkind == RELKIND_SEQUENCE ||
		relkind == RELKIND_TOASTVALUE ||
		relkind == RELKIND_MATVIEW)
		RelationInitTableAccessMethod(rel);

	/*
	 * Okay to insert into the relcache hash table.
	 *
	 * Ordinarily, there should certainly not be an existing hash entry for
	 * the same OID; but during bootstrap, when we create a "real" relcache
	 * entry for one of the bootstrap relations, we'll be overwriting the
	 * phony one created with formrdesc.  So allow that to happen for nailed
	 * rels.
	 */
	RelationCacheInsert(rel, nailit);

	/*
	 * Flag relation as needing eoxact cleanup (to clear rd_createSubid). We
	 * can't do this before storing relid in it.
	 */
	EOXactListAdd(rel);

	/* It's fully valid */
	rel->rd_isvalid = true;

	/*
	 * Caller expects us to pin the returned entry.
	 */
	RelationIncrementReferenceCount(rel);

	return rel;
}

RelationCreateStorage
物理文件的创建由磁盘管理器负责,pg中所有文件系统均调用这统一接口,而RelationCreateStorage 函数的实现就是通过调用这些函数进一步封装而成,期执行流程如下:
1)对于持久化的relation,设置字段表示need_wal,表明需要写WAL日志,对于临时relation或者unlogged relation无需此操作;
2)根据输入的RelFileNode调用 smgropen返回 SMgrRelation对象,不存在会创建一个;
3)结合上述返回的 SMgrRelation和ForkNumber号调用 smgrcreate创建relation的物理文件;
4)如需写WAL日志,调用 log_smgrcreate函数记录下此relation的实际物理信息;
5)最后将其添加至PendingRelDelete链表尾,在事务真正提交的时候如需回滚则可通过此信息将创建的文件删除,并返回 SMgrRelation对象。

在这里插入图片描述

/*
 * RelationCreateStorage
 *		Create physical storage for a relation.
 *
 * Create the underlying disk file storage for the relation. This only
 * creates the main fork; additional forks are created lazily by the
 * modules that need them.
 *
 * This function is transactional. The creation is WAL-logged, and if the
 * transaction aborts later on, the storage will be destroyed.
 */
SMgrRelation
RelationCreateStorage(RelFileNode rnode, char relpersistence)
{
	PendingRelDelete *pending;
	SMgrRelation srel;
	BackendId	backend;
	bool		needs_wal;

	Assert(!IsInParallelMode());	/* couldn't update pendingSyncHash */

	switch (relpersistence)
	{
		case RELPERSISTENCE_TEMP:
			backend = BackendIdForTempRelations();
			needs_wal = false;
			break;
		case RELPERSISTENCE_UNLOGGED:
			backend = InvalidBackendId;
			needs_wal = false;
			break;
		case RELPERSISTENCE_PERMANENT:
			backend = InvalidBackendId;
			needs_wal = true;
			break;
		default:
			elog(ERROR, "invalid relpersistence: %c", relpersistence);
			return NULL;		/* placate compiler */
	}

	srel = smgropen(rnode, backend);
	smgrcreate(srel, MAIN_FORKNUM, false);

	if (needs_wal)
		log_smgrcreate(&srel->smgr_rnode.node, MAIN_FORKNUM);

	/* Add the relation to the list of stuff to delete at abort */
	pending = (PendingRelDelete *)
		MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
	pending->relnode = rnode;
	pending->backend = backend;
	pending->atCommit = false;	/* delete if abort */
	pending->nestLevel = GetCurrentTransactionNestLevel();
	pending->next = pendingDeletes;
	pendingDeletes = pending;

	if (relpersistence == RELPERSISTENCE_PERMANENT && !XLogIsNeeded())
	{
		Assert(backend == InvalidBackendId);
		AddPendingSync(&rnode);
	}

	return srel;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/54677.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

宝宝入托,爸妈要避开这5种心态

孩子入托&#xff0c;父母也要做好心理准备&#xff0c;尤其需要避免以下5种常见的、不良心理状态&#xff0c;否则会加重孩子入托的困难度。 01.“生离死别式”的入托状态 即每次送孩子入园&#xff0c;就像一场生离死别。宝宝屋里哭&#xff0c;家长屋外哭&#xff0c;最后多…

大数据编程实验四:SparkStreaming编程

大数据编程实验四&#xff1a;SparkStreaming编程 文章目录大数据编程实验四&#xff1a;SparkStreaming编程一、实验目的与要求二、实验内容三、实验步骤1、利用Spark Streaming对不同类型数据源的数据进行处理2、完成DStream的两种有状态转换操作3、完成把DStream的数据输出保…

推荐一个对pytorch代码详细注释的github项目

今天在无意间找一个pytorch代码和注释的Github项目。 先上项目&#xff1a; https://github.com/labmlai/annotated_deep_learning_paper_implementations 这个项目还有个网站&#xff0c;地址&#xff1a;https://nn.labml.ai/ 这个项目将论文和pytorch代码结合起来&#xff…

jsp源码商城系统Myeclipse开发mysql数据库servlet开发java编程计算机网页项目

一、源码特点 JSP 源码商城系统 是一套完善的web设计系统&#xff0c;对理解JSP java编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统采用serlvetdaobean mvc 模式&#xff0c;系统主要采用B/S模式开发。开发环境为TOMCAT7.0,Myeclipse8.5开发…

想学习如何把excel图片转表格?1分钟教会你图片转表格怎么转

应该有不少小伙伴接收过上司或领导以图片格式发送过来的excel表格吧&#xff1f;并且还会要求我们将里面的内容整理为电子档&#xff0c;便于后期的内容编辑以及数据修改。 而当你们收到这种任务时&#xff0c;是怎么去操作的呢&#xff1f;是不是大部分人会选择手动重新制作&a…

【数据可视化】第四章—— 基于pandas的数据可视化(pandas数据结构)

文章目录前言1. Pandas库的引用2. Pandas库的数据类型2.1 Series类型2.2 Series创建方式2.3 Series类型的基本操作2.3.1 Series类型的切片和索引2.3.2 Series类型的对齐操作2.3.3 Series类型的name属性2.3.4 Series类型的修改2.4 DataFrame类型2.5 DataFrame类型创建2.6 DataFr…

毕设选题推荐基于python的django框架医院预约挂号系统

精彩专栏推荐订阅&#xff1a;在 下方专栏&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; &#x1f496;&#x1f525;作者主页&#xff1a;计算机毕设老哥&#x1f525; &#x1f496; Java实战项目专栏 Python实…

公网SSH远程连接内网Ubuntu主机【cpolar内网穿透】

SSH为建立在应用层基础上的安全协议&#xff0c;专为远程登录会话和其他网络服务提供安全性的协议。但在没有公网IP的环境下&#xff0c;只能在同个局域网下远程。 本篇教程主要实现通过内网穿透&#xff0c;在公网环境下SSH远程登录内网的Ubuntu主机&#xff0c;突破局域网的…

manjaro gnome 记录 3 配置国内镜像源

manjaro gnome 记录 3 配置国内镜像源初manjaro 记录 3 配置国内镜像源更改这个文件设置源初 希望能写一些简单的教程和案例分享给需要的人 manjaro 记录 3 配置国内镜像源 打开图像界面的软件管理&#xff0c;点击右上角&#xff1a;三个点的图标 点击首选项 输入管理员密…

Day17-购物车页面-收获地址-初步封装my-address组件

1.创建收货地址组件&#xff08;my-address&#xff09; 我的操作&#xff1a; 1>在uni_modules文件夹右键新建一个组件 2>还需要自己补全代码 1>和2>的阶段效果图&#xff1a; my-address组件已经被渲染成功了。 *********************************************…

一文看懂linux 内核网络中 RPS/RFS 原理

1 自带 irqbalance 瓶颈 基于简单的中断负载均衡(如系统自带的irqbalance进程)可能会弄巧成拙。因为其并不识别网络流&#xff0c;只识别到这是一个数据包&#xff0c;不能识别到数据包的元组信息。 在多处理器系统的每个处理器都有单独的硬件高速缓存&#xff0c;如果其中一…

黑马程序员软件测试实战项目

Ego微商 “Ego微商”微信小程序应用&#xff0c;主要针对于有特色的食品类商品线上零售。通过微信平台的大流量入口&#xff0c;在一定程度上升高了特色食品的影响力&#xff0c;同时借助微信的模板消息快速推送更新的商品&#xff0c;实现轻量级应用的C2C或者是B2C的线上销售…

《论文阅读》BALM: Bundle Adjustment for Lidar Mapping

留个笔记自用 BALM: Bundle Adjustment for Lidar Mapping 做什么 首先是最基础的&#xff0c;Structure-from-Motion&#xff08;SFM&#xff09;&#xff0c;SFM可以简单翻译成运动估计&#xff0c;是一种基于dui8序列图片进行三维重建的算法。简单来说就是是从运动中不同…

12月2日第壹简报,星期五,农历十一月初九

12月2日第壹简报&#xff0c;星期五&#xff0c;农历十一月初九1. 银保监会&#xff1a;2023年1月起在北京、上海、江苏、浙江、福建、广东等10个省市开展商业养老金业务试点。2. 国家首批未来产业科技园试点名单出炉&#xff1a;空天科技未来产业科技园、未来能源与智能机器人…

2022-12-02 编译Android平台OpenCV,用到读取视频时报错:AMediaXXX

文章目录编译Android平台OpenCV&#xff0c;用到读取视频时报错&#xff1a;解决参考编译Android平台OpenCV&#xff0c;用到读取视频时报错&#xff1a; ld: error: undefined symbol: AMediaExtractor_new ld: error: undefined symbol: AMediaExtractor_setDataSourceFd ld…

PyQt5的安装

0. 准备工作 Anaconda3-5.2.0-Windows-x86_64pycharm-professional-2018.2.4PyQt5 5.8.1 1. 如何正确安装PyQt5&#xff1f; 1.1 安装PyQt5 pip install PyQt5 -i https://pypi.douban.com/simple- i表示指定安装源&#xff0c;表示国内源 https://pypi.douban.com/simple …

创建一个SpringCloud项目

文章目录1.首先在**SpringCloud官网**中查看依赖版本号2.创建主Maven项目&#xff1a;在pom文件中引入依赖3.再在这个Maven项目中创建子模块&#xff08;子模块也是Maven&#xff09;(1)创建一个数据库db01和表dept(2)创建实体类dept&#xff08;注意&#xff1a;**每个实体类都…

导包问题解决--ImportError: DLL load failed while importing _path: 找不到指定的模块

一、问题反馈 在运行某个Python程序时&#xff0c;需要导入numpy和matplotlib包如下&#xff1a; import numpy as np import matplotlib.pyplot as plt运行程序时会报错“ImportError: DLL load failed while importing _path: 找不到指定的模块”&#xff1a; 二、问题解决…

信号发生器的电路构成及工作原理

一、信号发生器的电路构成 信号发生器的电路组成有多种形式&#xff0c;一般包括以下几个环节: 基本波形产生电路:波形产生可以由RC振荡器、文丘里电桥振荡器或压控振荡器产生。 波形转换电路:基本波形由正弦波、方波、三角波经过矩形波整形电路、正弦波整形电路、三角波整形电…

经众多Nature文章使用认证!艾美捷抗酒石酸酸性磷酸酶TRAP染色试剂盒

抗酒石酸酸性磷酸酶&#xff08;TRAP&#xff0c;tartrate-resistant acid phosphatase&#xff09;为破骨细胞的标志酶&#xff0c;特异地分布于破骨细胞中&#xff0c;为破骨细胞所特有。通常作为鉴别破骨细胞的重要标志物&#xff0c;使破骨细胞呈红色。Kamiya艾美捷抗酒石酸…