diff --git a/src/backend/distributed/executor/multi_executor.c b/src/backend/distributed/executor/multi_executor.c index 82d177054..2ca21d2ea 100644 --- a/src/backend/distributed/executor/multi_executor.c +++ b/src/backend/distributed/executor/multi_executor.c @@ -752,28 +752,12 @@ IsLocalReferenceTableJoinPlan(PlannedStmt *plan) { bool hasReferenceTable = false; bool hasLocalTable = false; - bool hasReferenceTableReplica = false; /* - * We only allow join between reference tables and local tables in the - * coordinator. + * Check if we are in the coordinator and coordinator can have reference + * table placements */ - if (!IsCoordinator()) - { - return false; - } - - /* - * All groups that have pg_dist_node entries, also have reference - * table replicas. - */ - PrimaryNodeForGroup(GetLocalGroupId(), &hasReferenceTableReplica); - - /* - * If reference table doesn't have replicas on the coordinator, we don't - * allow joins with local tables. - */ - if (!hasReferenceTableReplica) + if (!CanUseCoordinatorLocalTablesWithReferenceTables()) { return false; } diff --git a/src/backend/distributed/master/worker_node_manager.c b/src/backend/distributed/master/worker_node_manager.c index 4c32f8582..301fd4e0a 100644 --- a/src/backend/distributed/master/worker_node_manager.c +++ b/src/backend/distributed/master/worker_node_manager.c @@ -17,6 +17,7 @@ #include "commands/dbcommands.h" #include "distributed/hash_helpers.h" #include "distributed/listutils.h" +#include "distributed/master_protocol.h" #include "distributed/metadata_cache.h" #include "distributed/multi_client_executor.h" #include "distributed/worker_manager.h" @@ -402,6 +403,39 @@ NodeIsPrimaryWorker(WorkerNode *node) } +/* + * CanUseCoordinatorLocalTablesWithReferenceTables returns true if we + * are allowed to use coordinator local tables with reference tables + * for joining or defining foreign keys between them. + */ +bool +CanUseCoordinatorLocalTablesWithReferenceTables(void) +{ + /* + * Using local tables of coordinator with reference tables is only allowed + * if we are in the coordinator. + * + * Also, to check if coordinator can have reference table placements in below + * check, we should be in the coordinator. + */ + if (!IsCoordinator()) + { + return false; + } + + /* + * If reference table doesn't have placements on the coordinator, we don't + * use local tables in coordinator with reference tables. + */ + if (!CanHaveReferenceTablePlacements()) + { + return false; + } + + return true; +} + + /* * CanHaveReferenceTablePlacements returns true if current node can have * reference table placements. This is only possible if we called below diff --git a/src/include/distributed/worker_manager.h b/src/include/distributed/worker_manager.h index f5bb00ba9..e2bae5864 100644 --- a/src/include/distributed/worker_manager.h +++ b/src/include/distributed/worker_manager.h @@ -72,6 +72,7 @@ extern WorkerNode * WorkerGetLocalFirstCandidateNode(List *currentNodeList); extern uint32 ActivePrimaryWorkerNodeCount(void); extern List * ActivePrimaryWorkerNodeList(LOCKMODE lockMode); extern List * ActivePrimaryNodeList(LOCKMODE lockMode); +extern bool CanUseCoordinatorLocalTablesWithReferenceTables(void); extern List * ReferenceTablePlacementNodeList(LOCKMODE lockMode); extern List * DistributedTablePlacementNodeList(LOCKMODE lockMode); extern bool NodeCanHaveDistTablePlacements(WorkerNode *node);