PostgreSQL源码学习–插入数据#3

PostgreSQL源码学习–插入数据#3本节介绍heapam_tuple_insert函数的代码流程 相关数据结构 结构体中有些成员可能目前难以理解,暂时先列出来,先把当前用到的成员能搞明白就可以。 // src/include/exec…

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

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

相关数据结构

结构体中有些成员可能目前难以理解,暂时先列出来,先把当前用到的成员能搞明白就可以。

// src/include/executor/tuptable.h

typedef struct TupleTableSlot
{
	NodeTag		type;
#define FIELDNO_TUPLETABLESLOT_FLAGS 1
	uint16		tts_flags;		/* Boolean states */
#define FIELDNO_TUPLETABLESLOT_NVALID 2
	AttrNumber	tts_nvalid;		/* # of valid values in tts_values */
	const TupleTableSlotOps *const tts_ops; /* implementation of slot */
#define FIELDNO_TUPLETABLESLOT_TUPLEDESCRIPTOR 4
	TupleDesc	tts_tupleDescriptor;	/* slot"s tuple descriptor */
#define FIELDNO_TUPLETABLESLOT_VALUES 5
	Datum	   *tts_values;		/* current per-attribute values */
#define FIELDNO_TUPLETABLESLOT_ISNULL 6
	bool	   *tts_isnull;		/* current per-attribute isnull flags */
	MemoryContext tts_mcxt;		/* slot itself is in this context */
	ItemPointerData tts_tid;	/* stored tuple"s tid */
	Oid			tts_tableOid;	/* table oid of tuple */
} TupleTableSlot;

代码100分

代码100分static void
heapam_tuple_insert(Relation relation, TupleTableSlot *slot, CommandId cid,
					int options, BulkInsertState bistate);

heapam_tuple_insert函数

/* 获取tuple。在当前示例中其tts_ops指向的是TTSOpsBufferHeapTuple,
   所以调用tts_buffer_heap_get_heap_tuple来取得tuple */
HeapTuple	tuple = ExecFetchSlotHeapTuple(slot, true, &shouldFree);

/* 设置tuple的tableoid为目标relation的oid */
slot->tts_tableOid = RelationGetRelid(relation);
tuple->t_tableOid = slot->tts_tableOid;

/*  插入tuple,复制Item指针到slot->tts_tid */
heap_insert(relation, tuple, cid, options, bistate);
ItemPointerCopy(&tuple->t_self, &slot->tts_tid);

/* 在获取tuple时如果使用了copy,那么现在可以释放了 */
if (shouldFree)
	pfree(tuple);

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8984.html

(0)
上一篇 2023-02-18
下一篇 2023-02-18

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注