From 245c40a2ac2e44e039d8657590873515d25793db Mon Sep 17 00:00:00 2001
From: Hari Babu <kommi.haribabu@gmail.com>
Date: Wed, 3 Jan 2018 15:31:42 +1100
Subject: [PATCH 12/12] Storage shared memory API

Added API to provide needed shared memory for
storage. 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_storage.c | 17 +++++++++++++++++
 src/backend/access/storage/storageam.c   | 24 ++++++++++++++++++++++++
 src/backend/executor/nodeSamplescan.c    |  2 +-
 src/backend/storage/ipc/ipci.c           |  5 +++--
 src/include/access/storageam.h           |  4 ++++
 src/include/access/storageamapi.h        |  4 ++++
 6 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/src/backend/access/heap/heapam_storage.c b/src/backend/access/heap/heapam_storage.c
index f8846519d6..78e5d9de8e 100644
--- a/src/backend/access/heap/heapam_storage.c
+++ b/src/backend/access/heap/heapam_storage.c
@@ -30,6 +30,11 @@
 #include "utils/rel.h"
 #include "utils/tqual.h"
 
+static Size heapam_storage_shmem_size(void);
+static void heapam_storage_shmem_init(void);
+
+StorageShmemSize_hook_type StorageShmemSize_hook = heapam_storage_shmem_size;
+StorageShmemInit_hook_type StorageShmemInit_hook = heapam_storage_shmem_init;
 
 /* ----------------------------------------------------------------
  *				storage AM support routines for heapam
@@ -501,6 +506,17 @@ retry:
 	return result;
 }
 
+static Size
+heapam_storage_shmem_size()
+{
+	return SyncScanShmemSize();
+}
+
+static void
+heapam_storage_shmem_init()
+{
+	return SyncScanShmemInit();
+}
 
 Datum
 heapam_storage_handler(PG_FUNCTION_ARGS)
@@ -535,6 +551,7 @@ heapam_storage_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/storage/storageam.c b/src/backend/access/storage/storageam.c
index 39aa55f80c..a848d04698 100644
--- a/src/backend/access/storage/storageam.c
+++ b/src/backend/access/storage/storageam.c
@@ -20,6 +20,9 @@
 #include "utils/rel.h"
 #include "utils/tqual.h"
 
+extern StorageShmemSize_hook_type StorageShmemSize_hook;
+extern StorageShmemInit_hook_type StorageShmemInit_hook;
+
 /*
  *	storage_fetch		- retrieve tuple with given tid
  */
@@ -89,6 +92,12 @@ storageam_get_heappagescandesc(StorageScanDesc sscan)
 	return sscan->rs_rd->rd_stamroutine->scan_get_heappagescandesc(sscan);
 }
 
+void
+storage_syncscan_report_location(Relation rel, BlockNumber location)
+{
+	return rel->rd_stamroutine->sync_scan_report_location(rel, location);
+}
+
 /*
  * heap_setscanlimits - restrict range of a heapscan
  *
@@ -435,3 +444,18 @@ storage_rewrite_dead_tuple(Relation rel, RewriteState state, HeapTuple oldTuple)
 {
 	return rel->rd_stamroutine->rewrite_heap_dead_tuple(state, oldTuple);
 }
+
+Size
+storage_shmem_size()
+{
+	if (StorageShmemSize_hook)
+		return (*StorageShmemSize_hook)();
+	return 0;
+}
+
+void
+storage_shmem_init()
+{
+	if (StorageShmemInit_hook)
+		(*StorageShmemInit_hook) ();
+}
diff --git a/src/backend/executor/nodeSamplescan.c b/src/backend/executor/nodeSamplescan.c
index e85abd1555..88b2cef715 100644
--- a/src/backend/executor/nodeSamplescan.c
+++ b/src/backend/executor/nodeSamplescan.c
@@ -511,7 +511,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);
+				storage_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..50157d4175 100644
--- a/src/backend/storage/ipc/ipci.c
+++ b/src/backend/storage/ipc/ipci.c
@@ -19,6 +19,7 @@
 #include "access/heapam.h"
 #include "access/multixact.h"
 #include "access/nbtree.h"
+#include "access/storageam.h"
 #include "access/subtrans.h"
 #include "access/twophase.h"
 #include "commands/async.h"
@@ -147,7 +148,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, storage_shmem_size());
 		size = add_size(size, AsyncShmemSize());
 		size = add_size(size, BackendRandomShmemSize());
 #ifdef EXEC_BACKEND
@@ -267,7 +268,7 @@ CreateSharedMemoryAndSemaphores(bool makePrivate, int port)
 	 */
 	SnapMgrInit();
 	BTreeShmemInit();
-	SyncScanShmemInit();
+	storage_shmem_init();
 	AsyncShmemInit();
 	BackendRandomShmemInit();
 
diff --git a/src/include/access/storageam.h b/src/include/access/storageam.h
index 3e151a70ca..996cbda63c 100644
--- a/src/include/access/storageam.h
+++ b/src/include/access/storageam.h
@@ -45,6 +45,7 @@ typedef void (*DeleteIndexTuples) (Relation rel, ItemPointer tid, TransactionId
 extern StorageScanDesc storage_beginscan_parallel(Relation relation, ParallelHeapScanDesc parallel_scan);
 extern ParallelHeapScanDesc storageam_get_parallelheapscandesc(StorageScanDesc sscan);
 extern HeapPageScanDesc storageam_get_heappagescandesc(StorageScanDesc sscan);
+extern void storage_syncscan_report_location(Relation rel, BlockNumber location);
 extern void storage_setscanlimits(StorageScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks);
 extern StorageScanDesc storage_beginscan(Relation relation, Snapshot snapshot,
 										 int nkeys, ScanKey key);
@@ -130,4 +131,7 @@ extern void storage_rewrite_tuple(Relation rel, RewriteState state, HeapTuple ol
 				   HeapTuple newTuple);
 extern bool storage_rewrite_dead_tuple(Relation rel, RewriteState state, HeapTuple oldTuple);
 
+extern Size storage_shmem_size(void);
+extern void storage_shmem_init(void);
+
 #endif
diff --git a/src/include/access/storageamapi.h b/src/include/access/storageamapi.h
index 4c39de6f31..2e0733e35c 100644
--- a/src/include/access/storageamapi.h
+++ b/src/include/access/storageamapi.h
@@ -106,6 +106,7 @@ typedef StorageScanDesc(*ScanBegin_function) (Relation relation,
 											  bool temp_snap);
 typedef ParallelHeapScanDesc (*ScanGetParallelheapscandesc_function) (StorageScanDesc scan);
 typedef HeapPageScanDesc(*ScanGetHeappagescandesc_function) (StorageScanDesc scan);
+typedef void (*SyncScanReportLocation_function) (Relation rel, BlockNumber location);
 
 typedef void (*ScanSetlimits_function) (StorageScanDesc sscan, BlockNumber startBlk, BlockNumber numBlks);
 
@@ -130,6 +131,8 @@ typedef bool (*HotSearchBuffer_function) (ItemPointer tid, Relation relation,
 										  Buffer buffer, Snapshot snapshot, HeapTuple heapTuple,
 										  bool *all_dead, bool first_call);
 
+typedef Size (*StorageShmemSize_hook_type) (void);
+typedef void (*StorageShmemInit_hook_type) (void);
 
 /*
  * API struct for a storage AM.  Note this must be stored in a single palloc'd
@@ -177,6 +180,7 @@ typedef struct StorageAmRoutine
 	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.15.0.windows.1

