Skip to content

Instantly share code, notes, and snippets.

@davidfetter
Forked from anonymous/SimpleInsertTuple.patch
Created April 23, 2014 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidfetter/11229517 to your computer and use it in GitHub Desktop.
Save davidfetter/11229517 to your computer and use it in GitHub Desktop.
*** a/src/backend/executor/nodeModifyTable.c
--- b/src/backend/executor/nodeModifyTable.c
***************
*** 1451,1453 **** ExecReScanModifyTable(ModifyTableState *node)
--- 1451,1505 ----
*/
elog(ERROR, "ExecReScanModifyTable is not implemented");
}
+
+ void SimpleInsertTuple(Relation relation, HeapTuple tuple)
+ ResultRelInfo *resultRelInfo;
+ TupleTableSlot *slot;
+ TupleTableSlot *ret;
+ TupleDesc tupdesc;
+ EState *estate = CreateExecutorState();
+ Oid relation_id;
+
+ tupdesc = RelationGetDescr(relation);
+
+ /*
+ * We need a ResultRelInfo so we can use the regular executor's
+ * index-entry-making machinery.
+ */
+ resultRelInfo = makeNode(ResultRelInfo);
+ resultRelInfo->ri_RangeTableIndex = 1; /* dummy */
+ resultRelInfo->ri_RelationDesc = relation;
+ resultRelInfo->ri_TrigDesc = CopyTriggerDesc(relation->trigdesc);
+ if (resultRelInfo->ri_TrigDesc)
+ {
+ resultRelInfo->ri_TrigFunctions = (FmgrInfo *)
+ palloc0(resultRelInfo->ri_TrigDesc->numtriggers * sizeof(FmgrInfo));
+ resultRelInfo->ri_TrigWhenExprs = (List **)
+ palloc0(resultRelInfo->ri_TrigDesc->numtriggers * sizeof(List *));
+ }
+ resultRelInfo->ri_TrigInstrument = NULL;
+
+ ExecOpenIndices(resultRelInfo);
+
+ estate->es_result_relations = resultRelInfo;
+ estate->es_num_result_relations = 1;
+ estate->es_result_relation_info = resultRelInfo;
+
+ /* Set up a tuple slot too */
+ slot = ExecInitExtraTupleSlot(estate);
+ ExecSetSlotDescriptor(slot, tupdesc);
+ /* Triggers might need a slot as well */
+ estate->es_trig_tuple_slot = ExecInitExtraTupleSlot(estate);
+
+ ExecStoreTuple(tuple, slot, InvalidBuffer, false);
+ ret = ExecInsert(slot, slot, estate, false);
+
+ /* Free resources */
+ ExecResetTupleTable(estate->es_tupleTable, false);
+ ExecCloseIndices(resultRelInfo);
+ FreeExecutorState(estate);
+ RelationClose(relation);
+
+ return ret;
+ }
*** a/src/include/executor/nodeModifyTable.h
--- b/src/include/executor/nodeModifyTable.h
***************
*** 19,23 **** extern ModifyTableState *ExecInitModifyTable(ModifyTable *node, EState *estate,
--- 19,24 ----
extern TupleTableSlot *ExecModifyTable(ModifyTableState *node);
extern void ExecEndModifyTable(ModifyTableState *node);
extern void ExecReScanModifyTable(ModifyTableState *node);
+ extern void SimpleInsertTuple(Relation relation, HeapTuple tuple);
#endif /* NODEMODIFYTABLE_H */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment