文章详情

短信预约信息系统项目管理师 报名、考试、查分时间动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

PostgreSQL源码学习--插入数据#4,5

2016-01-11 16:56

关注

PostgreSQL源码学习--插入数据#4,5

本节介绍table_tuple_insert和ExecInsert函数。

//src/include/utils/rel.h

typedef struct RelationData
{
	
	TupleDesc	rd_att;			
	
	
	const struct TableAmRoutine *rd_tableam;
} RelationData;
//src/include/access/tupdesc.h

typedef struct TupleDescData
{
	int			natts;			
	Oid			tdtypeid;		
	int32		tdtypmod;		
	int			tdrefcount;		
	TupleConstr *constr;		
	
	FormData_pg_attribute attrs[FLEXIBLE_ARRAY_MEMBER];
}			TupleDescData;
typedef struct TupleDescData *TupleDesc;
//src/include/nodes/parsenodes.h


typedef enum WCOKind
{
	WCO_VIEW_CHECK,				
	WCO_RLS_INSERT_CHECK,		
	WCO_RLS_UPDATE_CHECK,		
	WCO_RLS_CONFLICT_CHECK		
} WCOKind;
//src/include/nodes/nodes.h

typedef enum OnConflictAction
{
	ONCONFLICT_NONE,			
	ONCONFLICT_NOTHING,			
	ONCONFLICT_UPDATE			
} OnConflictAction;
//src/include/access/tableam.h

static inline void
table_tuple_insert(Relation rel, TupleTableSlot *slot, CommandId cid,
				   int options, struct BulkInsertStateData *bistate)
{
	
	rel->rd_tableam->tuple_insert(rel, slot, cid, options,
							  bistate);
}
//src/backend/executor/nodeModifyTable.c


ExecMaterializeSlot(slot);


resultRelInfo = estate->es_result_relation_info;
resultRelationDesc = resultRelInfo->ri_RelationDesc;


if (resultRelInfo->ri_TrigDesc &&
	resultRelInfo->ri_TrigDesc->trig_insert_before_row)
{
	if (!ExecBRInsertTriggers(estate, resultRelInfo, slot))
		return NULL;		
}


if (resultRelInfo->ri_TrigDesc &&
	resultRelInfo->ri_TrigDesc->trig_insert_instead_row)
{
	if (!ExecIRInsertTriggers(estate, resultRelInfo, slot))
		return NULL;		
}

else if (resultRelInfo->ri_FdwRoutine)
{
	
	if (resultRelationDesc->rd_att->constr &&
		resultRelationDesc->rd_att->constr->has_generated_stored)
		ExecComputeStoredGenerated(estate, slot);

	
	slot = resultRelInfo->ri_FdwRoutine->ExecForeignInsert(estate,
								   resultRelInfo,
								   slot,
								   planSlot);

	if (slot == NULL)		
		return NULL;

	
	slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc);
}

else
{
	
	slot->tts_tableOid = RelationGetRelid(resultRelationDesc);
	
	
	if (resultRelationDesc->rd_att->constr &&
		resultRelationDesc->rd_att->constr->has_generated_stored)
		ExecComputeStoredGenerated(estate, slot);、
		
	
		wco_kind = (mtstate->operation == CMD_UPDATE) ?
		WCO_RLS_UPDATE_CHECK : WCO_RLS_INSERT_CHECK;
	
	
	if (resultRelInfo->ri_WithCheckOptions != NIL)
		ExecWithCheckOptions(wco_kind, resultRelInfo, slot, estate);
	
	
	if (resultRelationDesc->rd_att->constr)
		ExecConstraints(resultRelInfo, slot, estate);
	
	
	if (resultRelInfo->ri_PartitionCheck &&
		(resultRelInfo->ri_PartitionRoot == NULL ||
		 (resultRelInfo->ri_TrigDesc &&
		  resultRelInfo->ri_TrigDesc->trig_insert_before_row)))
		ExecPartitionCheck(resultRelInfo, slot, estate, true);
	
	
	if (onconflict != ONCONFLICT_NONE && resultRelInfo->ri_NumIndices > 0)
	{
		
		arbiterIndexes = resultRelInfo->ri_onConflictArbiterIndexes;
		
vlock:	
		
		specConflict = false;
		if (!ExecCheckIndexConstraints(slot, estate, &conflictTid,
							   arbiterIndexes))
		{
			
			if (onconflict == ONCONFLICT_UPDATE)
			{
				
				if (ExecOnConflictUpdate(mtstate, resultRelInfo,
							 &conflictTid, planSlot, slot,
							 estate, canSetTag, &returning))
					{
						
						InstrCountTuples2(&mtstate->ps, 1);
						return returning;
					}
				else
					
					goto vlock;
			}
			
			else
			{
				
				Assert(onconflict == ONCONFLICT_NOTHING);
				ExecCheckTIDVisible(estate, resultRelInfo, &conflictTid,
						ExecGetReturningSlot(estate, resultRelInfo));
				
				InstrCountTuples2(&mtstate->ps, 1);
				return NULL;
			}
		}
		
		specToken = SpeculativeInsertionLockAcquire(GetCurrentTransactionId());
		
		
		table_tuple_insert_speculative(resultRelationDesc, slot,
							   estate->es_output_cid,
							   0,
							   NULL,
							   specToken);
		
		
		recheckIndexes = ExecInsertIndexTuples(slot, estate, true,
							   &specConflict,
							   arbiterIndexes);
		
		
		table_tuple_complete_speculative(resultRelationDesc, slot,
							 specToken, !specConflict);
		
		
		SpeculativeInsertionLockRelease(GetCurrentTransactionId());
		
		
		if (specConflict)
		{
			list_free(recheckIndexes);
			goto vlock;
		}
		
	}
	
	else
	{
		
		table_tuple_insert(resultRelationDesc, slot,
					   estate->es_output_cid,
					   0, NULL);

		if (resultRelInfo->ri_NumIndices > 0)
			recheckIndexes = ExecInsertIndexTuples(slot, estate, false, NULL,
										   NIL);
	}
}


if (canSetTag)
{
	(estate->es_processed)++;
	setLastTid(&slot->tts_tid);
}


ar_insert_trig_tcs = mtstate->mt_transition_capture;
if (mtstate->operation == CMD_UPDATE && mtstate->mt_transition_capture
	&& mtstate->mt_transition_capture->tcs_update_new_table)
{
	ExecARUpdateTriggers(estate, resultRelInfo, NULL,
						 NULL,
						 slot,
						 NULL,
						 mtstate->mt_transition_capture);

	ar_insert_trig_tcs = NULL;
}


ExecARInsertTriggers(estate, resultRelInfo, slot, recheckIndexes,
				 ar_insert_trig_tcs);

list_free(recheckIndexes);


if (resultRelInfo->ri_WithCheckOptions != NIL)
	ExecWithCheckOptions(WCO_VIEW_CHECK, resultRelInfo, slot, estate);


if (resultRelInfo->ri_projectReturning)
	result = ExecProcessReturning(resultRelInfo, slot, planSlot);

return result;
阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯