From b8d71745eac0a12740a70dc78cbcdedadade37f8 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Tue, 26 Feb 2019 18:21:44 -0800 Subject: Store table oid and tuple's tid in tuple slots directly. After the introduction of tuple table slots all table AMs need to support returning the table oid of the tuple stored in a slot created by said AM. It does not make sense to re-implement that in every AM, therefore move handling of table OIDs into the TupleTableSlot structure itself. It's possible that we, at a later date, might want to get rid of HeapTupleData.t_tableOid entirely, but doing so before the abstractions for table AMs are integrated turns out to be too hard, so delay that for now. Similarly, every AM needs to support the concept of a tuple identifier (tid / item pointer) for its tuples. It's quite possible that we'll generalize the exact form of a tid at a future point (to allow for things like index organized tables), but for now many parts of the code know about tids, so there's not much point in abstracting tids away. Therefore also move into slot (rather than providing API to set/get the tid associated with the tuple in a slot). Once table AM includes insert/updating/deleting tuples, the responsibility to set the correct tid after such an action will move into that. After that change, code doing such modifications, should not have to deal with HeapTuples directly anymore. Author: Andres Freund, Haribabu Kommi and Ashutosh Bapat Discussion: https://postgr.es/m/20180703070645.wchpu5muyto5n647@alap3.anarazel.de --- src/backend/executor/nodeModifyTable.c | 57 +++++++++++++++++----------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'src/backend/executor/nodeModifyTable.c') diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index 566858c19b3..fe62da06ead 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -166,20 +166,15 @@ ExecProcessReturning(ResultRelInfo *resultRelInfo, /* Make tuple and any needed join variables available to ExecProject */ if (tupleSlot) econtext->ecxt_scantuple = tupleSlot; - else - { - HeapTuple tuple; - - /* - * RETURNING expressions might reference the tableoid column, so - * initialize t_tableOid before evaluating them. - */ - Assert(!TupIsNull(econtext->ecxt_scantuple)); - tuple = ExecFetchSlotHeapTuple(econtext->ecxt_scantuple, true, NULL); - tuple->t_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc); - } econtext->ecxt_outertuple = planSlot; + /* + * RETURNING expressions might reference the tableoid column, so + * reinitialize tts_tableOid before evaluating them. + */ + econtext->ecxt_scantuple->tts_tableOid = + RelationGetRelid(resultRelInfo->ri_RelationDesc); + /* Compute the RETURNING expressions */ return ExecProject(projectReturning); } @@ -332,19 +327,21 @@ ExecInsert(ModifyTableState *mtstate, /* * AFTER ROW Triggers or RETURNING expressions might reference the - * tableoid column, so initialize t_tableOid before evaluating them. + * tableoid column, so (re-)initialize tts_tableOid before evaluating + * them. */ - tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + slot->tts_tableOid = RelationGetRelid(resultRelInfo->ri_RelationDesc); + } else { WCOKind wco_kind; /* - * Constraints might reference the tableoid column, so initialize - * t_tableOid before evaluating them. + * Constraints might reference the tableoid column, so (re-)initialize + * tts_tableOid before evaluating them. */ - tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + slot->tts_tableOid = RelationGetRelid(resultRelationDesc); /* * Check any RLS WITH CHECK policies. @@ -458,6 +455,8 @@ ExecInsert(ModifyTableState *mtstate, estate->es_output_cid, HEAP_INSERT_SPECULATIVE, NULL); + slot->tts_tableOid = RelationGetRelid(resultRelationDesc); + ItemPointerCopy(&tuple->t_self, &slot->tts_tid); /* insert index entries for tuple */ recheckIndexes = ExecInsertIndexTuples(slot, &(tuple->t_self), @@ -503,6 +502,8 @@ ExecInsert(ModifyTableState *mtstate, heap_insert(resultRelationDesc, tuple, estate->es_output_cid, 0, NULL); + slot->tts_tableOid = RelationGetRelid(resultRelationDesc); + ItemPointerCopy(&tuple->t_self, &slot->tts_tid); /* insert index entries for tuple */ if (resultRelInfo->ri_NumIndices > 0) @@ -515,7 +516,7 @@ ExecInsert(ModifyTableState *mtstate, if (canSetTag) { (estate->es_processed)++; - setLastTid(&(tuple->t_self)); + setLastTid(&slot->tts_tid); } /* @@ -647,8 +648,6 @@ ExecDelete(ModifyTableState *mtstate, } else if (resultRelInfo->ri_FdwRoutine) { - HeapTuple tuple; - /* * delete from foreign table: let the FDW do it * @@ -670,12 +669,11 @@ ExecDelete(ModifyTableState *mtstate, /* * RETURNING expressions might reference the tableoid column, so - * initialize t_tableOid before evaluating them. + * (re)initialize tts_tableOid before evaluating them. */ if (TTS_EMPTY(slot)) ExecStoreAllNullTuple(slot); - tuple = ExecFetchSlotHeapTuple(slot, true, NULL); - tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + slot->tts_tableOid = RelationGetRelid(resultRelationDesc); } else { @@ -985,9 +983,10 @@ ExecUpdate(ModifyTableState *mtstate, /* * AFTER ROW Triggers or RETURNING expressions might reference the - * tableoid column, so initialize t_tableOid before evaluating them. + * tableoid column, so (re-)initialize tts_tableOid before evaluating + * them. */ - tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + slot->tts_tableOid = RelationGetRelid(resultRelationDesc); } else { @@ -995,10 +994,10 @@ ExecUpdate(ModifyTableState *mtstate, bool partition_constraint_failed; /* - * Constraints might reference the tableoid column, so initialize - * t_tableOid before evaluating them. + * Constraints might reference the tableoid column, so (re-)initialize + * tts_tableOid before evaluating them. */ - tuple->t_tableOid = RelationGetRelid(resultRelationDesc); + slot->tts_tableOid = RelationGetRelid(resultRelationDesc); /* * Check any RLS UPDATE WITH CHECK policies @@ -1184,6 +1183,8 @@ lreplace:; estate->es_crosscheck_snapshot, true /* wait for commit */ , &hufd, &lockmode); + ItemPointerCopy(&tuple->t_self, &slot->tts_tid); + switch (result) { case HeapTupleSelfUpdated: -- cgit v1.2.3