返回顶部
首页 > 资讯 > 数据库 >PostgreSQL源码学习--插入数据#2
  • 844
分享到

PostgreSQL源码学习--插入数据#2

PostgreSQL源码学习--插入数据#2 2020-02-11 17:02:09 844人浏览 绘本
摘要

本节介绍heap_insert函数的代码流程 本节前置 toast机制:https://www.postgresql.org/docs/12/storage-toast.html 可见性映射表:Https://www.postgresql.

PostgreSQL源码学习--插入数据#2

本节介绍heap_insert函数的代码流程

本节前置

toast机制:https://www.postgresql.org/docs/12/storage-toast.html

可见性映射表:Https://www.postgresql.org/docs/12/storage-vm.html

事务一致性检查:https://www.postgresql.org/docs/12/transaction-iso.html

WAL机制:https://www.postgresql.org/docs/12/wal-intro.html

相关数据结构

void
heap_insert(Relation relation, HeapTuple tup, CommandId cid,
			int options, BulkInsertState bistate);

heap_insert函数

//src/backend/access/heap/heapam.c


heaptup = heap_prepare_insert(relation, tup, xid, cid, options);


buffer = RelationGetBufferForTuple(relation, heaptup->t_len,
								   InvalidBuffer, options, bistate, 
								   &vmbuffer, NULL);


CheckForSerializableConflictIn(relation, NULL, InvalidBuffer);


START_CRIT_SECTION();


RelationPutHeapTuple(relation, buffer, heaptup,
					 (options & HEAP_INSERT_SPECULATIVE) != 0);


if (PageIsAllVisible(BufferGetPage(buffer)))
{
	all_visible_cleared = true;
	PageClearAllVisible(BufferGetPage(buffer));
	visibilitymap_clear(relation,
						ItemPointerGetBlockNumber(&(heaptup->t_self)),
						vmbuffer, VISIBILITYMAP_VALID_BITS);
}


MarkBufferDirty(buffer);


if (!(options & HEAP_INSERT_SKIP_WAL) && RelationNeedsWAL(relation))
{
	
	if (RelationIsAccessibleInLogicalDecoding(relation))
		log_heap_new_cid(relation, heaptup);
	
	
	if (ItemPointerGetOffsetNumber(&(heaptup->t_self)) == FirstOffsetNumber &&
		PageGetMaxOffsetNumber(page) == FirstOffsetNumber)
	{
		info |= XLOG_HEAP_INIT_PAGE;
		bufflags |= REGBUF_WILL_INIT;
	}
	
	
	XLogBeginInsert();
	XLogRegisterData((char *) &xlrec, SizeOfHeapInsert);
	
	
	XLogRegisterBuffer(0, buffer, REGBUF_STANDARD | bufflags);
	XLogRegisterBufData(0, (char *) &xlhdr, SizeOfHeapHeader);
	XLogRegisterBufData(0,
						(char *) heaptup->t_data + SizeofHeapTupleHeader,
						heaptup->t_len - SizeofHeapTupleHeader);
	
	
	XLogSetRecordFlags(XLOG_INCLUDE_ORIGIN);
	
	
	recptr = XLogInsert(RM_HEAP_ID, info);
	
	
	PageSetLSN(page, recptr);
}


END_CRIT_SECTION();


UnlockReleaseBuffer(buffer);
if (vmbuffer != InvalidBuffer)
	ReleaseBuffer(vmbuffer);


CacheInvalidateHeapTuple(relation, heaptup, NULL);


pgstat_count_heap_insert(relation, 1);


if (heaptup != tup)
{
	tup->t_self = heaptup->t_self;
	heap_freetuple(heaptup);
}
您可能感兴趣的文档:

--结束END--

本文标题: PostgreSQL源码学习--插入数据#2

本文链接: https://lsjlt.com/news/5500.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作