From e64936a923fa2772fa9151a0e51bb4a042bbd36b Mon Sep 17 00:00:00 2001
From: kommih <haribabuk@fast.au.fujitsu.com>
Date: Wed, 29 Aug 2018 14:22:07 +1000
Subject: [PATCH 1/2] check-world fixes

---
 contrib/pg_visibility/pg_visibility.c  |  5 +++--
 src/backend/access/heap/heapam.c       |  2 --
 src/backend/executor/execExprInterp.c  |  3 +++
 src/backend/executor/execMain.c        | 13 ++-----------
 src/backend/executor/execTuples.c      | 21 +++++++++++++++++++++
 src/backend/executor/nodeModifyTable.c | 12 ++++++++++--
 src/include/executor/tuptable.h        |  1 +
 7 files changed, 40 insertions(+), 17 deletions(-)

diff --git a/contrib/pg_visibility/pg_visibility.c b/contrib/pg_visibility/pg_visibility.c
index dce5262e34..88ca4fd2af 100644
--- a/contrib/pg_visibility/pg_visibility.c
+++ b/contrib/pg_visibility/pg_visibility.c
@@ -563,12 +563,13 @@ collect_corrupt_items(Oid relid, bool all_visible, bool all_frozen)
 
 	rel = relation_open(relid, AccessShareLock);
 
+	/* Only some relkinds have a visibility map */
+	check_relation_relkind(rel);
+
 	if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
 		ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
 						errmsg("only heap AM is supported")));
 
-	/* Only some relkinds have a visibility map */
-	check_relation_relkind(rel);
 
 	nblocks = RelationGetNumberOfBlocks(rel);
 
diff --git a/src/backend/access/heap/heapam.c b/src/backend/access/heap/heapam.c
index b35dcea75e..6d516ccc0b 100644
--- a/src/backend/access/heap/heapam.c
+++ b/src/backend/access/heap/heapam.c
@@ -83,8 +83,6 @@
 /* GUC variable */
 bool		synchronize_seqscans = true;
 
-static void heap_parallelscan_startblock_init(HeapScanDesc scan);
-static BlockNumber heap_parallelscan_nextpage(HeapScanDesc scan);
 static HeapTuple heap_prepare_insert(Relation relation, HeapTuple tup,
 					TransactionId xid, CommandId cid, int options);
 static XLogRecPtr log_heap_update(Relation reln, Buffer oldbuf,
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index 5ed9273d32..d91d0f25a1 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -570,6 +570,9 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
 				Assert(TTS_IS_HEAPTUPLE(scanslot) ||
 					   TTS_IS_BUFFERTUPLE(scanslot));
 
+				if (hslot->tuple == NULL)
+					ExecMaterializeSlot(scanslot);
+
 				d = heap_getsysattr(hslot->tuple, attnum,
 									scanslot->tts_tupleDescriptor,
 									op->resnull);
diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c
index f430733b69..faeb960e1d 100644
--- a/src/backend/executor/execMain.c
+++ b/src/backend/executor/execMain.c
@@ -2559,7 +2559,7 @@ EvalPlanQual(EState *estate, EPQState *epqstate,
 	 * datums that may be present in copyTuple).  As with the next step, this
 	 * is to guard against early re-use of the EPQ query.
 	 */
-	if (!TupIsNull(slot))
+	if (!TupIsNull(slot) && !TTS_IS_VIRTUAL(slot))
 		ExecMaterializeSlot(slot);
 
 #if FIXME
@@ -2766,16 +2766,7 @@ EvalPlanQualFetchRowMarks(EPQState *epqstate)
 			if (isNull)
 				continue;
 
-			elog(ERROR, "frak, need to implement ROW_MARK_COPY");
-#ifdef FIXME
-			// FIXME: this should just deform the tuple and store it as a
-			// virtual one.
-			tuple = table_tuple_by_datum(erm->relation, datum, erm->relid);
-
-			/* store tuple */
-			EvalPlanQualSetTuple(epqstate, erm->rti, tuple);
-#endif
-
+			ExecForceStoreHeapTupleDatum(datum, slot);
 		}
 	}
 }
diff --git a/src/backend/executor/execTuples.c b/src/backend/executor/execTuples.c
index 4921835c31..7628799d41 100644
--- a/src/backend/executor/execTuples.c
+++ b/src/backend/executor/execTuples.c
@@ -1397,6 +1397,27 @@ ExecStoreAllNullTuple(TupleTableSlot *slot)
 	return ExecFinishStoreSlotValues(slot);
 }
 
+void
+ExecForceStoreHeapTupleDatum(Datum data, TupleTableSlot *slot)
+{
+	HeapTuple	tuple;
+	HeapTupleHeader td;
+
+	td = DatumGetHeapTupleHeader(data);
+
+	tuple = (HeapTuple) palloc(HEAPTUPLESIZE + HeapTupleHeaderGetDatumLength(td));
+	tuple->t_len = HeapTupleHeaderGetDatumLength(td);
+	tuple->t_self = td->t_ctid;
+	tuple->t_data = (HeapTupleHeader) ((char *) tuple + HEAPTUPLESIZE);
+	memcpy((char *) tuple->t_data, (char *) td, tuple->t_len);
+
+	ExecClearTuple(slot);
+
+	heap_deform_tuple(tuple, slot->tts_tupleDescriptor,
+					  slot->tts_values, slot->tts_isnull);
+	ExecFinishStoreSlotValues(slot);
+}
+
 /* --------------------------------
  *		ExecFetchSlotTuple
  *			Fetch the slot's regular physical tuple.
diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c
index 226ba5fc21..0b38259387 100644
--- a/src/backend/executor/nodeModifyTable.c
+++ b/src/backend/executor/nodeModifyTable.c
@@ -607,7 +607,7 @@ ExecDelete(ModifyTableState *mtstate,
 		   bool canSetTag,
 		   bool changingPart,
 		   bool *tupleDeleted,
-		   TupleTableSlot **epqslot)
+		   TupleTableSlot **epqreturnslot)
 {
 	ResultRelInfo *resultRelInfo;
 	Relation	resultRelationDesc;
@@ -632,7 +632,7 @@ ExecDelete(ModifyTableState *mtstate,
 		bool		dodelete;
 
 		dodelete = ExecBRDeleteTriggers(estate, epqstate, resultRelInfo,
-										tupleid, oldtuple, epqslot);
+										tupleid, oldtuple, epqreturnslot);
 
 		if (!dodelete)			/* "do nothing" */
 			return NULL;
@@ -724,6 +724,14 @@ ldelete:;
 					/* Tuple no more passing quals, exiting... */
 					return NULL;
 				}
+
+				/**/
+				if (epqreturnslot)
+				{
+					*epqreturnslot = epqslot;
+					return NULL;
+				}
+
 				goto ldelete;
 			}
 		}
diff --git a/src/include/executor/tuptable.h b/src/include/executor/tuptable.h
index 1d316deed3..97aa26f5e0 100644
--- a/src/include/executor/tuptable.h
+++ b/src/include/executor/tuptable.h
@@ -476,6 +476,7 @@ extern TupleTableSlot *ExecCopySlot(TupleTableSlot *dstslot,
 
 extern void ExecForceStoreHeapTuple(HeapTuple tuple,
 			   TupleTableSlot *slot);
+extern void ExecForceStoreHeapTupleDatum(Datum data, TupleTableSlot *slot);
 
 extern void slot_getmissingattrs(TupleTableSlot *slot, int startAttNum, int lastAttNum);
 extern Datum slot_getattr(TupleTableSlot *slot, int attnum, bool *isnull);
-- 
2.18.0.windows.1

