From fa29d6667aac7e88e066042d1110d56e43a59435 Mon Sep 17 00:00:00 2001 From: Hanefi Onaldi Date: Wed, 2 Jun 2021 14:45:35 +0300 Subject: [PATCH] Accept invalidation before fk graph validity check (#5017) InvalidateForeignKeyGraph sends an invalidation via shared memory to all backends, including the current one. However, we might not call AcceptInvalidationMessages before reading from the cache below. It would be better to also add a call to AcceptInvalidationMessages in IsForeignConstraintRelationshipGraphValid. --- .../distributed/utils/foreign_key_relationship.c | 10 +++++++++- src/include/distributed/foreign_key_relationship.h | 1 - 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/backend/distributed/utils/foreign_key_relationship.c b/src/backend/distributed/utils/foreign_key_relationship.c index 770a7175d..8996feb15 100644 --- a/src/backend/distributed/utils/foreign_key_relationship.c +++ b/src/backend/distributed/utils/foreign_key_relationship.c @@ -32,6 +32,7 @@ #if PG_VERSION_NUM >= PG_VERSION_13 #include "common/hashfn.h" #endif +#include "utils/inval.h" #include "utils/memutils.h" @@ -82,6 +83,7 @@ static ForeignConstraintRelationshipNode * GetRelationshipNodeForRelationId(Oid relationId, bool *isFound); static void CreateForeignConstraintRelationshipGraph(void); +static bool IsForeignConstraintRelationshipGraphValid(void); static List * GetNeighbourList(ForeignConstraintRelationshipNode *relationshipNode, bool isReferencing); static List * GetRelationIdsFromRelationshipNodeList(List *fKeyRelationshipNodeList); @@ -348,9 +350,15 @@ CreateForeignConstraintRelationshipGraph() /* * IsForeignConstraintGraphValid check whether there is a valid graph. */ -bool +static bool IsForeignConstraintRelationshipGraphValid() { + /* + * We might have some concurrent metadata changes. In order to get the changes, + * we first need to accept the cache invalidation messages. + */ + AcceptInvalidationMessages(); + if (fConstraintRelationshipGraph != NULL && fConstraintRelationshipGraph->isValid) { return true; diff --git a/src/include/distributed/foreign_key_relationship.h b/src/include/distributed/foreign_key_relationship.h index 3aa040d76..491142d13 100644 --- a/src/include/distributed/foreign_key_relationship.h +++ b/src/include/distributed/foreign_key_relationship.h @@ -20,7 +20,6 @@ extern bool ConnectedToReferenceTableViaFKey(Oid relationId); extern List * ReferencedRelationIdList(Oid relationId); extern List * ReferencingRelationIdList(Oid relationId); extern void SetForeignConstraintRelationshipGraphInvalid(void); -extern bool IsForeignConstraintRelationshipGraphValid(void); extern void ClearForeignConstraintRelationshipGraphContext(void); extern HTAB * CreateOidVisitedHashSet(void); extern bool OidVisited(HTAB *oidVisitedMap, Oid oid);