From efa35a092b2527ce5124b273c53c006ad1bc1dc7 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sat, 19 Jan 2019 22:55:39 -0800
Subject: [PATCH v18 06/18] tableam: Add table_get_latest_tid.

Author:
Reviewed-By:
Discussion: https://postgr.es/m/
Backpatch:
---
 src/backend/access/heap/heapam_handler.c |  1 +
 src/backend/executor/nodeTidscan.c       | 14 +++-----------
 src/backend/utils/adt/tid.c              |  5 +++--
 src/include/access/tableam.h             | 12 ++++++++++++
 4 files changed, 19 insertions(+), 13 deletions(-)

diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 318e393dbde..f4bbf24412e 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -574,6 +574,7 @@ static const TableAmRoutine heapam_methods = {
 	.tuple_lock = heapam_lock_tuple,
 
 	.tuple_fetch_row_version = heapam_fetch_row_version,
+	.tuple_get_latest_tid = heap_get_latest_tid,
 	.tuple_satisfies_snapshot = heapam_tuple_satisfies_snapshot,
 };
 
diff --git a/src/backend/executor/nodeTidscan.c b/src/backend/executor/nodeTidscan.c
index 7d496cc4105..354dcc16d41 100644
--- a/src/backend/executor/nodeTidscan.c
+++ b/src/backend/executor/nodeTidscan.c
@@ -22,7 +22,6 @@
  */
 #include "postgres.h"
 
-#include "access/heapam.h"
 #include "access/sysattr.h"
 #include "access/tableam.h"
 #include "catalog/pg_type.h"
@@ -308,7 +307,6 @@ TidNext(TidScanState *node)
 	ScanDirection direction;
 	Snapshot	snapshot;
 	Relation	heapRelation;
-	HeapTuple	tuple;
 	TupleTableSlot *slot;
 	ItemPointerData *tidList;
 	int			numTids;
@@ -332,12 +330,6 @@ TidNext(TidScanState *node)
 	tidList = node->tss_TidList;
 	numTids = node->tss_NumTids;
 
-	/*
-	 * We use node->tss_htup as the tuple pointer; note this can't just be a
-	 * local variable here, as the scan tuple slot will keep a pointer to it.
-	 */
-	tuple = &(node->tss_htup);
-
 	/*
 	 * Initialize or advance scan position, depending on direction.
 	 */
@@ -365,7 +357,7 @@ TidNext(TidScanState *node)
 
 	while (node->tss_TidPtr >= 0 && node->tss_TidPtr < numTids)
 	{
-		tuple->t_self = tidList[node->tss_TidPtr];
+		ItemPointerData tid = tidList[node->tss_TidPtr];
 
 		/*
 		 * For WHERE CURRENT OF, the tuple retrieved from the cursor might
@@ -373,9 +365,9 @@ TidNext(TidScanState *node)
 		 * current according to our snapshot.
 		 */
 		if (node->tss_isCurrentOf)
-			heap_get_latest_tid(heapRelation, snapshot, &tuple->t_self);
+			table_get_latest_tid(heapRelation, snapshot, &tid);
 
-		if (table_fetch_row_version(heapRelation, &tuple->t_self, snapshot, slot, NULL))
+		if (table_fetch_row_version(heapRelation, &tid, snapshot, slot, NULL))
 			return slot;
 
 		/* Bad TID or failed snapshot qual; try next */
diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c
index f5ffd12cfc9..333d9801c98 100644
--- a/src/backend/utils/adt/tid.c
+++ b/src/backend/utils/adt/tid.c
@@ -23,6 +23,7 @@
 #include "access/hash.h"
 #include "access/heapam.h"
 #include "access/sysattr.h"
+#include "access/tableam.h"
 #include "catalog/namespace.h"
 #include "catalog/pg_type.h"
 #include "libpq/pqformat.h"
@@ -379,7 +380,7 @@ currtid_byreloid(PG_FUNCTION_ARGS)
 	ItemPointerCopy(tid, result);
 
 	snapshot = RegisterSnapshot(GetLatestSnapshot());
-	heap_get_latest_tid(rel, snapshot, result);
+	table_get_latest_tid(rel, snapshot, result);
 	UnregisterSnapshot(snapshot);
 
 	table_close(rel, AccessShareLock);
@@ -414,7 +415,7 @@ currtid_byrelname(PG_FUNCTION_ARGS)
 	ItemPointerCopy(tid, result);
 
 	snapshot = RegisterSnapshot(GetLatestSnapshot());
-	heap_get_latest_tid(rel, snapshot, result);
+	table_get_latest_tid(rel, snapshot, result);
 	UnregisterSnapshot(snapshot);
 
 	table_close(rel, AccessShareLock);
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index cac3a5fdd83..0fdb6b78e7f 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -225,6 +225,10 @@ typedef struct TableAmRoutine
 											TupleTableSlot *slot,
 											Relation stats_relation);
 
+	void		(*tuple_get_latest_tid) (Relation rel,
+										 Snapshot snapshot,
+										 ItemPointer tid);
+
 	/*
 	 * Does the tuple in `slot` satisfy `snapshot`?  The slot needs to be of
 	 * the appropriate type for the AM.
@@ -572,6 +576,14 @@ table_fetch_row_version(Relation rel,
 													stats_relation);
 }
 
+static inline void
+table_get_latest_tid(Relation rel,
+					 Snapshot snapshot,
+					 ItemPointer tid)
+{
+	rel->rd_tableam->tuple_get_latest_tid(rel, snapshot, tid);
+}
+
 /*
  * Return true iff tuple in slot satisfies the snapshot.
  *
-- 
2.21.0.dirty

