From 78e201522b53c1e11111f3ad411cff62a4516d10 Mon Sep 17 00:00:00 2001
From: Kyotaro Horiguchi <horiguchi.kyotaro@lab.ntt.co.jp>
Date: Fri, 16 Dec 2016 16:44:40 +0900
Subject: [PATCH 2/2] Cleanup negative cache of pg_class when dropping a schema

This feature in turn is triggered by catcache invalidation. This patch
provides a syscache invalidation callback to flush negative cache
entries corresponding to an invalidated object.
---
 src/backend/utils/cache/catcache.c |  42 ++++++
 src/backend/utils/cache/inval.c    |   8 +-
 src/backend/utils/cache/syscache.c | 301 ++++++++++++++++++++++++++++---------
 src/include/utils/catcache.h       |   3 +
 4 files changed, 283 insertions(+), 71 deletions(-)

diff --git a/src/backend/utils/cache/catcache.c b/src/backend/utils/cache/catcache.c
index c1d9d2f..094bc60 100644
--- a/src/backend/utils/cache/catcache.c
+++ b/src/backend/utils/cache/catcache.c
@@ -1424,6 +1424,48 @@ GetCatCacheHashValue(CatCache *cache,
 	return CatalogCacheComputeHashValue(cache, cache->cc_nkeys, cur_skey);
 }
 
+/*
+ * CollectOIDsForHashValue
+ *
+ * Collect OIDs corresnpond to a hash value. attnum is the column to retrieve
+ * the OIDs.
+ */
+List *
+CollectOIDsForHashValue(CatCache *cache, uint32 hashValue, int attnum)
+{
+	Index		 hashIndex = HASH_INDEX(hashValue, cache->cc_nbuckets);
+	dlist_head	*bucket = &cache->cc_bucket[hashIndex];
+	dlist_iter	 iter;
+	List *ret = NIL;
+
+	/* Nothing to return before initialized */
+	if (cache->cc_tupdesc == NULL)
+		return ret;
+
+	/* Currently only OID key is supported */
+	Assert(attnum <= cache->cc_tupdesc->natts);
+	Assert(attnum < 0 ? attnum == ObjectIdAttributeNumber :
+		   cache->cc_tupdesc->attrs[attnum]->atttypid == OIDOID);
+
+	dlist_foreach(iter, bucket)
+	{
+		CatCTup *ct = dlist_container(CatCTup, cache_elem, iter.cur);
+		bool	isNull;
+		Datum	oid;
+
+		if (ct->dead)
+			continue;			/* ignore dead entries */
+
+		if (ct->hash_value != hashValue)
+			continue;			/* quickly skip entry if wrong hash val */
+
+		oid = heap_getattr(&ct->tuple, attnum, cache->cc_tupdesc, &isNull);
+		if (!isNull)
+			ret = lappend_oid(ret, DatumGetObjectId(oid));
+	}
+
+	return ret;
+}
 
 /*
  *	SearchCatCacheList
diff --git a/src/backend/utils/cache/inval.c b/src/backend/utils/cache/inval.c
index 5803518..0290974 100644
--- a/src/backend/utils/cache/inval.c
+++ b/src/backend/utils/cache/inval.c
@@ -543,9 +543,13 @@ LocalExecuteInvalidationMessage(SharedInvalidationMessage *msg)
 		{
 			InvalidateCatalogSnapshot();
 
-			CatalogCacheIdInvalidate(msg->cc.id, msg->cc.hashValue);
-
+			/*
+			 * Call the callbacks first so that the callbacks can access the
+			 * entries corresponding to the hashValue.
+			 */
 			CallSyscacheCallbacks(msg->cc.id, msg->cc.hashValue);
+
+			CatalogCacheIdInvalidate(msg->cc.id, msg->cc.hashValue);
 		}
 	}
 	else if (msg->id == SHAREDINVALCATALOG_ID)
diff --git a/src/backend/utils/cache/syscache.c b/src/backend/utils/cache/syscache.c
index e1ba693..cfcf4cd 100644
--- a/src/backend/utils/cache/syscache.c
+++ b/src/backend/utils/cache/syscache.c
@@ -108,6 +108,16 @@
 */
 
 /*
+ *	struct for flushing negative cache by syscache invalidation
+ */
+typedef struct SysCacheCBParam_T
+{
+	int	trig_attnum;
+	int	target_cacheid;
+	ScanKeyData skey;
+} SysCacheCBParam;
+
+/*
  *		struct cachedesc: information defining a single syscache
  */
 struct cachedesc
@@ -120,6 +130,14 @@ struct cachedesc
 
 	/* relcache invalidation stuff */
 	AttrNumber	relattrnum;		/* attr number of reloid, 0 if nothing to do */
+
+	/* catcache invalidation stuff */
+	int			trig_cacheid;	/* cache id of triggering syscache: -1 means
+								 * no triggering cache */
+	int16		trig_attnum;	/* key column in triggering cache. Must be an
+								 * OID */
+	int16		target_attnum;	/* corresponding column in this cache. Must be
+								 * an OID*/
 };
 
 static const struct cachedesc cacheinfo[] = {
@@ -133,7 +151,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AccessMethodRelationId,	/* AMNAME */
 		AmNameIndexId,
@@ -145,7 +164,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AccessMethodRelationId,	/* AMOID */
 		AmOidIndexId,
@@ -157,7 +177,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AccessMethodOperatorRelationId,	/* AMOPOPID */
 		AccessMethodOperatorIndexId,
@@ -169,7 +190,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		64,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AccessMethodOperatorRelationId,	/* AMOPSTRATEGY */
 		AccessMethodStrategyIndexId,
@@ -181,7 +203,8 @@ static const struct cachedesc cacheinfo[] = {
 			Anum_pg_amop_amopstrategy
 		},
 		64,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AccessMethodProcedureRelationId,	/* AMPROCNUM */
 		AccessMethodProcedureIndexId,
@@ -193,7 +216,8 @@ static const struct cachedesc cacheinfo[] = {
 			Anum_pg_amproc_amprocnum
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AttributeRelationId,		/* ATTNAME */
 		AttributeRelidNameIndexId,
@@ -205,7 +229,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		32,
-		Anum_pg_attribute_attrelid
+		Anum_pg_attribute_attrelid,
+		-1, 0, 0
 	},
 	{AttributeRelationId,		/* ATTNUM */
 		AttributeRelidNumIndexId,
@@ -217,7 +242,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		128,
-		Anum_pg_attribute_attrelid
+		Anum_pg_attribute_attrelid,
+		-1, 0, 0
 	},
 	{AuthMemRelationId,			/* AUTHMEMMEMROLE */
 		AuthMemMemRoleIndexId,
@@ -229,7 +255,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AuthMemRelationId,			/* AUTHMEMROLEMEM */
 		AuthMemRoleMemIndexId,
@@ -241,7 +268,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AuthIdRelationId,			/* AUTHNAME */
 		AuthIdRolnameIndexId,
@@ -253,7 +281,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{AuthIdRelationId,			/* AUTHOID */
 		AuthIdOidIndexId,
@@ -265,7 +294,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{CastRelationId,			/* CASTSOURCETARGET */
 		CastSourceTargetIndexId,
@@ -277,7 +307,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		256,
-		0
+		0,
+		-1, 0, 0
 	},
 	{OperatorClassRelationId,	/* CLAAMNAMENSP */
 		OpclassAmNameNspIndexId,
@@ -289,7 +320,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{OperatorClassRelationId,	/* CLAOID */
 		OpclassOidIndexId,
@@ -301,7 +333,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{CollationRelationId,		/* COLLNAMEENCNSP */
 		CollationNameEncNspIndexId,
@@ -313,7 +346,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{CollationRelationId,		/* COLLOID */
 		CollationOidIndexId,
@@ -325,7 +359,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ConversionRelationId,		/* CONDEFAULT */
 		ConversionDefaultIndexId,
@@ -337,7 +372,8 @@ static const struct cachedesc cacheinfo[] = {
 			ObjectIdAttributeNumber,
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ConversionRelationId,		/* CONNAMENSP */
 		ConversionNameNspIndexId,
@@ -349,7 +385,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ConstraintRelationId,		/* CONSTROID */
 		ConstraintOidIndexId,
@@ -361,7 +398,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ConversionRelationId,		/* CONVOID */
 		ConversionOidIndexId,
@@ -373,7 +411,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{DatabaseRelationId,		/* DATABASEOID */
 		DatabaseOidIndexId,
@@ -385,7 +424,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{DefaultAclRelationId,		/* DEFACLROLENSPOBJ */
 		DefaultAclRoleNspObjIndexId,
@@ -397,7 +437,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{EnumRelationId,			/* ENUMOID */
 		EnumOidIndexId,
@@ -409,7 +450,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{EnumRelationId,			/* ENUMTYPOIDNAME */
 		EnumTypIdLabelIndexId,
@@ -421,7 +463,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{EventTriggerRelationId,	/* EVENTTRIGGERNAME */
 		EventTriggerNameIndexId,
@@ -433,7 +476,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{EventTriggerRelationId,	/* EVENTTRIGGEROID */
 		EventTriggerOidIndexId,
@@ -445,7 +489,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ForeignDataWrapperRelationId,		/* FOREIGNDATAWRAPPERNAME */
 		ForeignDataWrapperNameIndexId,
@@ -457,7 +502,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ForeignDataWrapperRelationId,		/* FOREIGNDATAWRAPPEROID */
 		ForeignDataWrapperOidIndexId,
@@ -469,7 +515,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ForeignServerRelationId,	/* FOREIGNSERVERNAME */
 		ForeignServerNameIndexId,
@@ -481,7 +528,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ForeignServerRelationId,	/* FOREIGNSERVEROID */
 		ForeignServerOidIndexId,
@@ -493,7 +541,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ForeignTableRelationId,	/* FOREIGNTABLEREL */
 		ForeignTableRelidIndexId,
@@ -505,7 +554,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{IndexRelationId,			/* INDEXRELID */
 		IndexRelidIndexId,
@@ -517,7 +567,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		64,
-		0
+		0,
+		-1, 0, 0
 	},
 	{LanguageRelationId,		/* LANGNAME */
 		LanguageNameIndexId,
@@ -529,7 +580,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{LanguageRelationId,		/* LANGOID */
 		LanguageOidIndexId,
@@ -541,7 +593,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{NamespaceRelationId,		/* NAMESPACENAME */
 		NamespaceNameIndexId,
@@ -553,7 +606,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{NamespaceRelationId,		/* NAMESPACEOID */
 		NamespaceOidIndexId,
@@ -565,7 +619,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{OperatorRelationId,		/* OPERNAMENSP */
 		OperatorNameNspIndexId,
@@ -577,7 +632,8 @@ static const struct cachedesc cacheinfo[] = {
 			Anum_pg_operator_oprnamespace
 		},
 		256,
-		0
+		0,
+		-1, 0, 0
 	},
 	{OperatorRelationId,		/* OPEROID */
 		OperatorOidIndexId,
@@ -589,7 +645,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		32,
-		0
+		0,
+		-1, 0, 0
 	},
 	{OperatorFamilyRelationId,	/* OPFAMILYAMNAMENSP */
 		OpfamilyAmNameNspIndexId,
@@ -601,7 +658,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{OperatorFamilyRelationId,	/* OPFAMILYOID */
 		OpfamilyOidIndexId,
@@ -613,7 +671,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{PartitionedRelationId,		/* PARTRELID */
 		PartitionedRelidIndexId,
@@ -625,7 +684,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		32,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ProcedureRelationId,		/* PROCNAMEARGSNSP */
 		ProcedureNameArgsNspIndexId,
@@ -637,7 +697,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		128,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ProcedureRelationId,		/* PROCOID */
 		ProcedureOidIndexId,
@@ -649,7 +710,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		128,
-		0
+		0,
+		-1, 0, 0
 	},
 	{RangeRelationId,			/* RANGETYPE */
 		RangeTypidIndexId,
@@ -661,7 +723,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{RelationRelationId,		/* RELNAMENSP */
 		ClassNameNspIndexId,
@@ -673,7 +736,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		128,
-		0
+		0,
+		NAMESPACEOID, ObjectIdAttributeNumber, Anum_pg_class_relnamespace
 	},
 	{RelationRelationId,		/* RELOID */
 		ClassOidIndexId,
@@ -685,7 +749,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		128,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ReplicationOriginRelationId,		/* REPLORIGIDENT */
 		ReplicationOriginIdentIndex,
@@ -697,7 +762,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{ReplicationOriginRelationId,		/* REPLORIGNAME */
 		ReplicationOriginNameIndex,
@@ -709,7 +775,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{RewriteRelationId,			/* RULERELNAME */
 		RewriteRelRulenameIndexId,
@@ -721,7 +788,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		8,
-		0
+		0,
+		-1, 0, 0
 	},
 	{SequenceRelationId,			/* SEQRELID */
 		SequenceRelidIndexId,
@@ -733,7 +801,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		32,
-		0
+		0,
+		-1, 0, 0
 	},
 	{StatisticRelationId,		/* STATRELATTINH */
 		StatisticRelidAttnumInhIndexId,
@@ -745,7 +814,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		128,
-		Anum_pg_statistic_starelid
+		Anum_pg_statistic_starelid,
+		-1, 0, 0
 	},
 	{TableSpaceRelationId,		/* TABLESPACEOID */
 		TablespaceOidIndexId,
@@ -757,7 +827,8 @@ static const struct cachedesc cacheinfo[] = {
 			0,
 		},
 		4,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TransformRelationId,		/* TRFOID */
 		TransformOidIndexId,
@@ -769,7 +840,8 @@ static const struct cachedesc cacheinfo[] = {
 			0,
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TransformRelationId,		/* TRFTYPELANG */
 		TransformTypeLangIndexId,
@@ -781,7 +853,8 @@ static const struct cachedesc cacheinfo[] = {
 			0,
 		},
 		16,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSConfigMapRelationId,		/* TSCONFIGMAP */
 		TSConfigMapIndexId,
@@ -793,7 +866,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSConfigRelationId,		/* TSCONFIGNAMENSP */
 		TSConfigNameNspIndexId,
@@ -805,7 +879,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSConfigRelationId,		/* TSCONFIGOID */
 		TSConfigOidIndexId,
@@ -817,7 +892,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSDictionaryRelationId,	/* TSDICTNAMENSP */
 		TSDictionaryNameNspIndexId,
@@ -829,7 +905,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSDictionaryRelationId,	/* TSDICTOID */
 		TSDictionaryOidIndexId,
@@ -841,7 +918,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSParserRelationId,		/* TSPARSERNAMENSP */
 		TSParserNameNspIndexId,
@@ -853,7 +931,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSParserRelationId,		/* TSPARSEROID */
 		TSParserOidIndexId,
@@ -865,7 +944,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSTemplateRelationId,		/* TSTEMPLATENAMENSP */
 		TSTemplateNameNspIndexId,
@@ -877,7 +957,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TSTemplateRelationId,		/* TSTEMPLATEOID */
 		TSTemplateOidIndexId,
@@ -889,7 +970,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TypeRelationId,			/* TYPENAMENSP */
 		TypeNameNspIndexId,
@@ -901,7 +983,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		64,
-		0
+		0,
+		-1, 0, 0
 	},
 	{TypeRelationId,			/* TYPEOID */
 		TypeOidIndexId,
@@ -913,7 +996,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		64,
-		0
+		0,
+		-1, 0, 0
 	},
 	{UserMappingRelationId,		/* USERMAPPINGOID */
 		UserMappingOidIndexId,
@@ -925,7 +1009,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	},
 	{UserMappingRelationId,		/* USERMAPPINGUSERSERVER */
 		UserMappingUserServerIndexId,
@@ -937,7 +1022,8 @@ static const struct cachedesc cacheinfo[] = {
 			0
 		},
 		2,
-		0
+		0,
+		-1, 0, 0
 	}
 };
 
@@ -972,7 +1058,8 @@ static ScanKeyData	oideqscankey; /* ScanKey for reloid match  */
 
 static int	oid_compare(const void *a, const void *b);
 static void SysCacheRelInvalCallback(Datum arg, Oid reloid);
-
+static void SysCacheSysCacheInvalCallback(Datum arg, int cacheid,
+										  uint32 hashvalue);
 /*
  * InitCatalogCache - initialize the caches
  *
@@ -1027,6 +1114,34 @@ InitCatalogCache(void)
 				cacheinfo[cacheId].relattrnum;
 			relinval_callback_count++;
 		}
+
+		/*
+		 * If this syscache has syscache invalidation trigger, register
+		 * it.
+		 */
+		if (cacheinfo[cacheId].trig_cacheid >= 0)
+		{
+			SysCacheCBParam *param;
+
+			param = MemoryContextAlloc(CacheMemoryContext,
+									   sizeof(SysCacheCBParam));
+			param->target_cacheid = cacheId;
+
+			/*
+			 * XXXX: Create a scankeydata for OID comparison. We don't have a
+			 * means to check the type of the column in the system catalog at
+			 * this time. So we have to belive the definition.
+			 */
+			fmgr_info_cxt(F_OIDEQ, &param->skey.sk_func, CacheMemoryContext);
+			param->skey.sk_attno = cacheinfo[cacheId].target_attnum;
+			param->trig_attnum = cacheinfo[cacheId].trig_attnum;
+			param->skey.sk_strategy = BTEqualStrategyNumber;
+			param->skey.sk_subtype = InvalidOid;
+			param->skey.sk_collation = InvalidOid;
+			CacheRegisterSyscacheCallback(cacheinfo[cacheId].trig_cacheid,
+										  SysCacheSysCacheInvalCallback,
+										  PointerGetDatum(param));
+		}
 	}
 
 	Assert(SysCacheRelationOidSize <= lengthof(SysCacheRelationOid));
@@ -1403,6 +1518,54 @@ RelationInvalidatesSnapshotsOnly(Oid relid)
 }
 
 /*
+ * SysCacheSysCacheInvalCallback
+ *
+ * Callback function for negative cache flushing by syscache invalidation.
+ * Fetches an OID (not restricted to system oid column) from the invalidated
+ * tuple and flushes negative entries that matches the OID in the target
+ * syscache.
+ */
+static void
+SysCacheSysCacheInvalCallback(Datum arg, int cacheid, uint32 hashValue)
+{
+	SysCacheCBParam *param;
+	CatCache	*trigger_cache;		/* triggering catcache */
+	CatCache	*target_cache;		/* target catcache */
+	List *oids;
+	ListCell *lc;
+	int			trigger_cacheid = cacheid;
+	int			target_cacheid;
+
+	param = (SysCacheCBParam *)DatumGetPointer(arg);
+	target_cacheid = param->target_cacheid;
+
+	trigger_cache = SysCache[trigger_cacheid];
+	target_cache = SysCache[target_cacheid];
+
+	/*
+	 * collect OIDs for target syscache entries. oids contains one value for
+	 * most cases, but two or more for the case hashvalue has synonyms. At
+	 * least one of them is the right OID but is cannot be distinguished using
+	 * the given hash value.
+	 *
+	 * As the result some unnecessary entries may be flushed but it won't harm
+	 * so much than letting them bloat catcaches.
+	 */
+	oids =
+		CollectOIDsForHashValue(trigger_cache, hashValue, param->trig_attnum);
+
+	foreach (lc, oids)
+	{
+		ScanKeyData skey;
+		Oid oid = lfirst_oid (lc);
+
+		memcpy(&skey, &param->skey, sizeof(skey));
+		skey.sk_argument = ObjectIdGetDatum(oid);
+		CleanupCatCacheNegEntries(target_cache, &skey);
+	}
+}
+
+/*
  * Test whether a relation has a system cache.
  */
 bool
diff --git a/src/include/utils/catcache.h b/src/include/utils/catcache.h
index cb662c0..b279174 100644
--- a/src/include/utils/catcache.h
+++ b/src/include/utils/catcache.h
@@ -179,6 +179,9 @@ extern uint32 GetCatCacheHashValue(CatCache *cache,
 					 Datum v1, Datum v2,
 					 Datum v3, Datum v4);
 
+extern List *CollectOIDsForHashValue(CatCache *cache,
+									 uint32 hashValue, int attnum);
+
 extern CatCList *SearchCatCacheList(CatCache *cache, int nkeys,
 				   Datum v1, Datum v2,
 				   Datum v3, Datum v4);
-- 
2.9.2

