From 6d049e7a7a80589abaa9efa96599a518d254a72b Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Date: Thu, 9 Jan 2020 19:22:18 +0900
Subject: [PATCH v3 2/3] Make CatCacheSearchN indirect functions

Some experiments showed that we could add new features avoidng
degradtion by making SearchCatCacheN functions replaceable using
indirect calling.
---
 src/backend/utils/cache/catcache.c | 58 ++++++++++++++++++++----------
 src/include/utils/catcache.h       | 40 ++++++++++++++++++---
 2 files changed, 75 insertions(+), 23 deletions(-)

diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index c5bad63aac..7ac59bcdd6 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -84,6 +84,15 @@ static pg_noinline HeapTuple SearchCatCacheMiss(CatCache *cache,
 												Datum v1, Datum v2,
 												Datum v3, Datum v4);
 
+static HeapTuple SearchCatCacheb(CatCache *cache,
+								 Datum v1, Datum v2, Datum v3, Datum v4);
+static HeapTuple SearchCatCache1b(CatCache *cache, Datum v1);
+static HeapTuple SearchCatCache2b(CatCache *cache, Datum v1, Datum v2);
+static HeapTuple SearchCatCache3b(CatCache *cache,
+								  Datum v1, Datum v2, Datum v3);
+static HeapTuple SearchCatCache4b(CatCache *cache,
+								  Datum v1, Datum v2, Datum v3, Datum v4);
+
 static uint32 CatalogCacheComputeHashValue(CatCache *cache, int nkeys,
 										   Datum v1, Datum v2, Datum v3, Datum v4);
 static uint32 CatalogCacheComputeTupleHashValue(CatCache *cache, int nkeys,
@@ -108,6 +117,16 @@ static void CatCacheFreeKeys(TupleDesc tupdesc, int nkeys, int *attnos,
 static void CatCacheCopyKeys(TupleDesc tupdesc, int nkeys, int *attnos,
 							 Datum *srckeys, Datum *dstkeys);
 
+static SearchCatCacheFuncsType catcache_base = {
+	SearchCatCacheb,
+	SearchCatCache1b,
+	SearchCatCache2b,
+	SearchCatCache3b,
+	SearchCatCache4b
+};
+
+SearchCatCacheFuncsType *SearchCatCacheFuncs = NULL;
+
 /* GUC assign function */
 void
 assign_catalog_cache_prune_min_age(int newval, void *extra)
@@ -817,6 +836,9 @@ InitCatCache(int id,
 		CacheHdr = (CatCacheHeader *) palloc(sizeof(CatCacheHeader));
 		slist_init(&CacheHdr->ch_caches);
 		CacheHdr->ch_ntup = 0;
+
+		SearchCatCacheFuncs = &catcache_base;
+
 #ifdef CATCACHE_STATS
 		/* set up to dump stats at backend exit */
 		on_proc_exit(CatCachePrintStats, 0);
@@ -1158,12 +1180,12 @@ IndexScanOK(CatCache *cache, ScanKey cur_skey)
  * the caller need not go to the trouble of converting it to a fully
  * null-padded NAME.
  */
-HeapTuple
-SearchCatCache(CatCache *cache,
-			   Datum v1,
-			   Datum v2,
-			   Datum v3,
-			   Datum v4)
+static HeapTuple
+SearchCatCacheb(CatCache *cache,
+				Datum v1,
+				Datum v2,
+				Datum v3,
+				Datum v4)
 {
 	return SearchCatCacheInternal(cache, cache->cc_nkeys, v1, v2, v3, v4);
 }
@@ -1175,33 +1197,33 @@ SearchCatCache(CatCache *cache,
  * bit faster than SearchCatCache().
  */
 
-HeapTuple
-SearchCatCache1(CatCache *cache,
-				Datum v1)
+static HeapTuple
+SearchCatCache1b(CatCache *cache,
+				 Datum v1)
 {
 	return SearchCatCacheInternal(cache, 1, v1, 0, 0, 0);
 }
 
 
-HeapTuple
-SearchCatCache2(CatCache *cache,
-				Datum v1, Datum v2)
+static HeapTuple
+SearchCatCache2b(CatCache *cache,
+				 Datum v1, Datum v2)
 {
 	return SearchCatCacheInternal(cache, 2, v1, v2, 0, 0);
 }
 
 
-HeapTuple
-SearchCatCache3(CatCache *cache,
-				Datum v1, Datum v2, Datum v3)
+static HeapTuple
+SearchCatCache3b(CatCache *cache,
+				 Datum v1, Datum v2, Datum v3)
 {
 	return SearchCatCacheInternal(cache, 3, v1, v2, v3, 0);
 }
 
 
-HeapTuple
-SearchCatCache4(CatCache *cache,
-				Datum v1, Datum v2, Datum v3, Datum v4)
+static HeapTuple
+SearchCatCache4b(CatCache *cache,
+				 Datum v1, Datum v2, Datum v3, Datum v4)
 {
 	return SearchCatCacheInternal(cache, 4, v1, v2, v3, v4);
 }
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index 3d3870f05a..f9e9889339 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -189,6 +189,36 @@ typedef struct catcacheheader
 	int			ch_ntup;		/* # of tuples in all caches */
 } CatCacheHeader;
 
+typedef HeapTuple (*SearchCatCache_fn)(CatCache *cache,
+									   Datum v1, Datum v2, Datum v3, Datum v4);
+typedef HeapTuple (*SearchCatCache1_fn)(CatCache *cache, Datum v1);
+typedef HeapTuple (*SearchCatCache2_fn)(CatCache *cache, Datum v1, Datum v2);
+typedef HeapTuple (*SearchCatCache3_fn)(CatCache *cache, Datum v1, Datum v2,
+										Datum v3);
+typedef HeapTuple (*SearchCatCache4_fn)(CatCache *cache,
+										Datum v1, Datum v2, Datum v3, Datum v4);
+
+typedef struct SearchCatCacheFuncsType
+{
+	SearchCatCache_fn	SearchCatCache;
+	SearchCatCache1_fn	SearchCatCache1;
+	SearchCatCache2_fn	SearchCatCache2;
+	SearchCatCache3_fn	SearchCatCache3;
+	SearchCatCache4_fn	SearchCatCache4;
+} SearchCatCacheFuncsType;
+
+extern PGDLLIMPORT SearchCatCacheFuncsType *SearchCatCacheFuncs;
+
+#define SearchCatCache(cache, v1, v2, v3, v4) \
+	SearchCatCacheFuncs->SearchCatCache(cache, v1, v2, v3, v4)
+#define SearchCatCache1(cache, v1) \
+	SearchCatCacheFuncs->SearchCatCache1(cache, v1)
+#define SearchCatCache2(cache, v1, v2) \
+	SearchCatCacheFuncs->SearchCatCache2(cache, v1, v2)
+#define SearchCatCache3(cache, v1, v2, v3) \
+	SearchCatCacheFuncs->SearchCatCache3(cache, v1, v2, v3)
+#define SearchCatCache4(cache, v1, v2, v3, v4) \
+	SearchCatCacheFuncs->SearchCatCache4(cache, v1, v2, v3, v4)
 
 /* this extern duplicates utils/memutils.h... */
 extern PGDLLIMPORT MemoryContext CacheMemoryContext;
@@ -216,15 +246,15 @@ extern CatCache *InitCatCache(int id, Oid reloid, Oid indexoid,
 							  int nbuckets);
 extern void InitCatCachePhase2(CatCache *cache, bool touch_index);
 
-extern HeapTuple SearchCatCache(CatCache *cache,
+extern HeapTuple (*SearchCatCache)(CatCache *cache,
 								Datum v1, Datum v2, Datum v3, Datum v4);
-extern HeapTuple SearchCatCache1(CatCache *cache,
+extern HeapTuple (*SearchCatCache1)(CatCache *cache,
 								 Datum v1);
-extern HeapTuple SearchCatCache2(CatCache *cache,
+extern HeapTuple (*SearchCatCache2)(CatCache *cache,
 								 Datum v1, Datum v2);
-extern HeapTuple SearchCatCache3(CatCache *cache,
+extern HeapTuple (*SearchCatCache3)(CatCache *cache,
 								 Datum v1, Datum v2, Datum v3);
-extern HeapTuple SearchCatCache4(CatCache *cache,
+extern HeapTuple (*SearchCatCache4)(CatCache *cache,
 								 Datum v1, Datum v2, Datum v3, Datum v4);
 extern void ReleaseCatCache(HeapTuple tuple);
 
-- 
2.18.4

