From a39cfd010c9292f5cfee2a8bdfc9c1f439e90243 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Fri, 8 Mar 2019 18:33:24 -0800
Subject: [PATCH v18 05/18] tableam: Add & use tableam_fetch_follow_check().

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/backend/access/nbtree/nbtinsert.c | 10 ++++++----
 src/backend/access/nbtree/nbtsort.c   |  2 +-
 src/backend/access/table/tableam.c    | 20 ++++++++++++++++++++
 src/include/access/tableam.h          |  5 +++++
 4 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.c
index 2b180288239..fe9e8a6e1a3 100644
--- a/src/backend/access/nbtree/nbtinsert.c
+++ b/src/backend/access/nbtree/nbtinsert.c
@@ -15,9 +15,9 @@
 
 #include "postgres.h"
 
-#include "access/heapam.h"
 #include "access/nbtree.h"
 #include "access/nbtxlog.h"
+#include "access/tableam.h"
 #include "access/transam.h"
 #include "access/xloginsert.h"
 #include "miscadmin.h"
@@ -414,8 +414,9 @@ _bt_check_unique(Relation rel, IndexTuple itup, Relation heapRel,
 				 * that satisfies SnapshotDirty.  This is necessary because we
 				 * have just a single index entry for the entire chain.
 				 */
-				else if (heap_hot_search(&htid, heapRel, &SnapshotDirty,
-										 &all_dead))
+				else if (table_index_fetch_tuple_check(heapRel, &htid,
+													   &SnapshotDirty,
+													   &all_dead))
 				{
 					TransactionId xwait;
 
@@ -468,7 +469,8 @@ _bt_check_unique(Relation rel, IndexTuple itup, Relation heapRel,
 					 * entry.
 					 */
 					htid = itup->t_tid;
-					if (heap_hot_search(&htid, heapRel, SnapshotSelf, NULL))
+					if (table_index_fetch_tuple_check(heapRel, &htid,
+													  SnapshotSelf, NULL))
 					{
 						/* Normal case --- it's still live */
 					}
diff --git a/src/backend/access/nbtree/nbtsort.c b/src/backend/access/nbtree/nbtsort.c
index e37cbac7b3c..05a9b03aed5 100644
--- a/src/backend/access/nbtree/nbtsort.c
+++ b/src/backend/access/nbtree/nbtsort.c
@@ -57,10 +57,10 @@
 
 #include "postgres.h"
 
-#include "access/heapam.h"
 #include "access/nbtree.h"
 #include "access/parallel.h"
 #include "access/relscan.h"
+#include "access/table.h"
 #include "access/tableam.h"
 #include "access/xact.h"
 #include "access/xlog.h"
diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c
index 9a01f74d8fe..b1cf8245e3f 100644
--- a/src/backend/access/table/tableam.c
+++ b/src/backend/access/table/tableam.c
@@ -181,6 +181,26 @@ table_beginscan_parallel(Relation relation, ParallelTableScanDesc parallel_scan)
  * ----------------------------------------------------------------------------
  */
 
+bool
+table_index_fetch_tuple_check(Relation rel,
+							  ItemPointer tid,
+							  Snapshot snapshot,
+							  bool *all_dead)
+{
+	IndexFetchTableData *scan = table_index_fetch_begin(rel);
+	TupleTableSlot *slot = table_slot_create(rel, NULL);
+	bool		call_again = false;
+	bool		found;
+
+	found = table_index_fetch_tuple(scan, tid, snapshot, slot, &call_again, all_dead);
+
+	table_index_fetch_end(scan);
+	ExecDropSingleTupleTableSlot(slot);
+
+	return found;
+}
+
+
 /*
  *	simple_table_update - replace a tuple
  *
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index abbd7f63385..cac3a5fdd83 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -543,6 +543,11 @@ table_index_fetch_tuple(struct IndexFetchTableData *scan,
 													all_dead);
 }
 
+extern bool table_index_fetch_tuple_check(Relation rel,
+							  ItemPointer tid,
+							  Snapshot snapshot,
+							  bool *all_dead);
+
 
 /* ------------------------------------------------------------------------
  * Functions for non-modifying operations on individual tuples
-- 
2.21.0.dirty

