diff --git a/src/backend/distributed/utils/foreign_key_relationship.c b/src/backend/distributed/utils/foreign_key_relationship.c index 2858e6ed3..d30c767df 100644 --- a/src/backend/distributed/utils/foreign_key_relationship.c +++ b/src/backend/distributed/utils/foreign_key_relationship.c @@ -28,6 +28,7 @@ #include "distributed/version_compat.h" #include "nodes/pg_list.h" #include "storage/lockdefs.h" +#include "utils/catcache.h" #include "utils/fmgroids.h" #include "utils/hsearch.h" #include "common/hashfn.h" @@ -96,6 +97,8 @@ static List * GetConnectedListHelper(ForeignConstraintRelationshipNode *node, bool isReferencing); static List * GetForeignConstraintRelationshipHelper(Oid relationId, bool isReferencing); +MemoryContext ForeignConstraintRelationshipMemoryContext = NULL; + /* * GetForeignKeyConnectedRelationIdList returns a list of relation id's for @@ -321,17 +324,36 @@ CreateForeignConstraintRelationshipGraph() return; } - ClearForeignConstraintRelationshipGraphContext(); + /* + * Lazily create our memory context once and reset on every reuse. + * Since we have cleared and invalidated the fConstraintRelationshipGraph, right + * before we can simply reset the context if it was already existing. + */ + if (ForeignConstraintRelationshipMemoryContext == NULL) + { + /* make sure we've initialized CacheMemoryContext */ + if (CacheMemoryContext == NULL) + { + CreateCacheMemoryContext(); + } - MemoryContext fConstraintRelationshipMemoryContext = AllocSetContextCreateInternal( - CacheMemoryContext, - "Forign Constraint Relationship Graph Context", - ALLOCSET_DEFAULT_MINSIZE, - ALLOCSET_DEFAULT_INITSIZE, - ALLOCSET_DEFAULT_MAXSIZE); + ForeignConstraintRelationshipMemoryContext = AllocSetContextCreate( + CacheMemoryContext, + "Foreign Constraint Relationship Graph Context", + ALLOCSET_DEFAULT_MINSIZE, + ALLOCSET_DEFAULT_INITSIZE, + ALLOCSET_DEFAULT_MAXSIZE); + } + else + { + fConstraintRelationshipGraph = NULL; + MemoryContextReset(ForeignConstraintRelationshipMemoryContext); + } + + Assert(fConstraintRelationshipGraph == NULL); MemoryContext oldContext = MemoryContextSwitchTo( - fConstraintRelationshipMemoryContext); + ForeignConstraintRelationshipMemoryContext); fConstraintRelationshipGraph = (ForeignConstraintRelationshipGraph *) palloc( sizeof(ForeignConstraintRelationshipGraph)); @@ -631,22 +653,3 @@ CreateOrFindNode(HTAB *adjacencyLists, Oid relid) return node; } - - -/* - * ClearForeignConstraintRelationshipGraphContext clear all the allocated memory obtained - * for foreign constraint relationship graph. Since all the variables of relationship - * graph was obtained within the same context, destroying hash map is enough as - * it deletes the context. - */ -void -ClearForeignConstraintRelationshipGraphContext() -{ - if (fConstraintRelationshipGraph == NULL) - { - return; - } - - hash_destroy(fConstraintRelationshipGraph->nodeMap); - fConstraintRelationshipGraph = NULL; -} diff --git a/src/include/distributed/foreign_key_relationship.h b/src/include/distributed/foreign_key_relationship.h index ef2c5be33..fbbee831e 100644 --- a/src/include/distributed/foreign_key_relationship.h +++ b/src/include/distributed/foreign_key_relationship.h @@ -20,7 +20,6 @@ extern bool ShouldUndistributeCitusLocalTable(Oid relationId); extern List * ReferencedRelationIdList(Oid relationId); extern List * ReferencingRelationIdList(Oid relationId); extern void SetForeignConstraintRelationshipGraphInvalid(void); -extern void ClearForeignConstraintRelationshipGraphContext(void); extern bool OidVisited(HTAB *oidVisitedMap, Oid oid); extern void VisitOid(HTAB *oidVisitedMap, Oid oid);