From 05ff50ecb50e7016cf574801f7142c88a7486c0c Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Thu, 29 Mar 2018 16:17:54 +1100
Subject: [PATCH 12/16] Table AM shared memory API

Added API to provide needed shared memory for
table AM. As of now only the syncscan infrastructure
uses the shared memory, it can enhanced for other
storages.

And also all the sync scan exposed API usage is
removed outside heap.
---
 src/backend/access/heap/heapam_handler.c | 12 ++++++++++++
 src/backend/access/table/tableam.c       |  7 +++++++
 src/backend/executor/nodeSamplescan.c    |  2 +-
 src/backend/storage/ipc/ipci.c           |  4 ++--
 src/include/access/heapam.h              |  4 ++++
 src/include/access/tableam.h             |  1 +
 src/include/access/tableamapi.h          |  4 ++--
 7 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/backend/access/heap/heapam_handler.c b/src/backend/access/heap/heapam_handler.c
index 062e184095..1bfb1942b5 100644
--- a/src/backend/access/heap/heapam_handler.c
+++ b/src/backend/access/heap/heapam_handler.c
@@ -509,6 +509,17 @@ heapam_fetch_tuple_from_offset(TableScanDesc sscan, BlockNumber blkno, OffsetNum
 	return &(scan->rs_ctup);
 }
 
+Size
+heapam_storage_shmem_size()
+{
+	return SyncScanShmemSize();
+}
+
+void
+heapam_storage_shmem_init()
+{
+	return SyncScanShmemInit();
+}
 
 Datum
 heap_tableam_handler(PG_FUNCTION_ARGS)
@@ -543,6 +554,7 @@ heap_tableam_handler(PG_FUNCTION_ARGS)
 	 * BitmapHeap and Sample Scans
 	 */
 	amroutine->scan_get_heappagescandesc = heapam_get_heappagescandesc;
+	amroutine->sync_scan_report_location = ss_report_location;
 
 	amroutine->tuple_fetch = heapam_fetch;
 	amroutine->tuple_insert = heapam_heap_insert;
diff --git a/src/backend/access/table/tableam.c b/src/backend/access/table/tableam.c
index 39b0f990c4..37a1c12c86 100644
--- a/src/backend/access/table/tableam.c
+++ b/src/backend/access/table/tableam.c
@@ -20,6 +20,7 @@
 #include "utils/rel.h"
 #include "utils/tqual.h"
 
+
 /*
  *	table_fetch		- retrieve tuple with given tid
  */
@@ -99,6 +100,12 @@ tableam_get_heappagescandesc(TableScanDesc sscan)
 	return sscan->rs_rd->rd_tableamroutine->scan_get_heappagescandesc(sscan);
 }
 
+void
+table_syncscan_report_location(Relation rel, BlockNumber location)
+{
+	return rel->rd_tableamroutine->sync_scan_report_location(rel, location);
+}
+
 /*
  * heap_setscanlimits - restrict range of a heapscan
  *
diff --git a/src/backend/executor/nodeSamplescan.c b/src/backend/executor/nodeSamplescan.c
index 581494df04..bf3cf688cb 100644
--- a/src/backend/executor/nodeSamplescan.c
+++ b/src/backend/executor/nodeSamplescan.c
@@ -490,7 +490,7 @@ tablesample_getnext(SampleScanState *scanstate)
 			 * We don't guarantee any specific ordering in general, though.
 			 */
 			if (pagescan->rs_syncscan)
-				ss_report_location(scan->rs_rd, blockno);
+				table_syncscan_report_location(scan->rs_rd, blockno);
 
 			finished = (blockno == pagescan->rs_startblock);
 		}
diff --git a/src/backend/storage/ipc/ipci.c b/src/backend/storage/ipc/ipci.c
index 0c86a581c0..9f9c6618c9 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -147,7 +147,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
 		size = add_size(size, ApplyLauncherShmemSize());
 		size = add_size(size, SnapMgrShmemSize());
 		size = add_size(size, BTreeShmemSize());
-		size = add_size(size, SyncScanShmemSize());
+		size = add_size(size, heapam_storage_shmem_size());
 		size = add_size(size, AsyncShmemSize());
 		size = add_size(size, BackendRandomShmemSize());
 #ifdef EXEC_BACKEND
@@ -267,7 +267,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
 	 */
 	SnapMgrInit();
 	BTreeShmemInit();
-	SyncScanShmemInit();
+	heapam_storage_shmem_init();
 	AsyncShmemInit();
 	BackendRandomShmemInit();
 
diff --git a/src/include/access/heapam.h b/src/include/access/heapam.h
index 26d882eb00..c6dfd80c04 100644
--- a/src/include/access/heapam.h
+++ b/src/include/access/heapam.h
@@ -222,4 +222,8 @@ extern void rewrite_heap_tuple(RewriteState state, HeapTuple oldTuple,
 				   HeapTuple newTuple);
 extern bool rewrite_heap_dead_tuple(RewriteState state, HeapTuple oldTuple);
 
+/* in heap/heapam_storage.c */
+extern Size heapam_storage_shmem_size(void);
+extern void heapam_storage_shmem_init(void);
+
 #endif							/* HEAPAM_H */
diff --git a/src/include/access/tableam.h b/src/include/access/tableam.h
index 7227f834a5..d29559395b 100644
--- a/src/include/access/tableam.h
+++ b/src/include/access/tableam.h
@@ -45,6 +45,7 @@ typedef void (*DeleteIndexTuples) (Relation rel, ItemPointer tid, TransactionId
 extern TableScanDesc table_beginscan_parallel(Relation relation, ParallelHeapScanDesc parallel_scan);
 extern ParallelHeapScanDesc tableam_get_parallelheapscandesc(TableScanDesc sscan);
 extern HeapPageScanDesc tableam_get_heappagescandesc(TableScanDesc sscan);
+extern void table_syncscan_report_location(Relation rel, BlockNumber location);
 extern void table_setscanlimits(TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks);
 extern TableScanDesc table_beginscan(Relation relation, Snapshot snapshot,
 				  int nkeys, ScanKey key);
diff --git a/src/include/access/tableamapi.h b/src/include/access/tableamapi.h
index 4897f1407e..982ba77ec2 100644
--- a/src/include/access/tableamapi.h
+++ b/src/include/access/tableamapi.h
@@ -107,7 +107,7 @@ typedef TableScanDesc (*ScanBegin_function) (Relation relation,
 
 typedef ParallelHeapScanDesc (*ScanGetParallelheapscandesc_function) (TableScanDesc scan);
 typedef HeapPageScanDesc(*ScanGetHeappagescandesc_function) (TableScanDesc scan);
-
+typedef void (*SyncScanReportLocation_function) (Relation rel, BlockNumber location);
 typedef void (*ScanSetlimits_function) (TableScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks);
 
 /* must return a TupleTableSlot? */
@@ -132,7 +132,6 @@ typedef bool (*HotSearchBuffer_function) (ItemPointer tid, Relation relation,
 										  Buffer buffer, Snapshot snapshot, HeapTuple heapTuple,
 										  bool *all_dead, bool first_call);
 
-
 /*
  * API struct for a table AM.  Note this must be stored in a single palloc'd
  * chunk of memory.
@@ -179,6 +178,7 @@ typedef struct TableAmRoutine
 	ScanBegin_function scan_begin;
 	ScanGetParallelheapscandesc_function scan_get_parallelheapscandesc;
 	ScanGetHeappagescandesc_function scan_get_heappagescandesc;
+	SyncScanReportLocation_function sync_scan_report_location;
 	ScanSetlimits_function scansetlimits;
 	ScanGetnext_function scan_getnext;
 	ScanGetnextSlot_function scan_getnextslot;
-- 
2.16.1.windows.4

