PostgreSQL源码学习插入数据#3

database

本节介绍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;

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);

以上是 PostgreSQL源码学习插入数据#3 的全部内容, 来源链接: utcz.com/z/533154.html

回到顶部