diff --git a/src/backend/distributed/metadata/node_metadata.c b/src/backend/distributed/metadata/node_metadata.c index d9220594317..bd02d6ae4b3 100644 --- a/src/backend/distributed/metadata/node_metadata.c +++ b/src/backend/distributed/metadata/node_metadata.c @@ -167,6 +167,7 @@ PG_FUNCTION_INFO_V1(citus_nodeport_for_nodeid); PG_FUNCTION_INFO_V1(citus_coordinator_nodeid); PG_FUNCTION_INFO_V1(citus_is_coordinator); PG_FUNCTION_INFO_V1(citus_internal_mark_node_not_synced); +PG_FUNCTION_INFO_V1(citus_is_primary_node); /* * DefaultNodeMetadata creates a NodeMetadata struct with the fields set to @@ -1664,6 +1665,26 @@ citus_is_coordinator(PG_FUNCTION_ARGS) PG_RETURN_BOOL(isCoordinator); } +/* + * citus_is_primary_node returns whether the current node is a primary for + * a given group_id. We consider the node a primary if it has + * pg_dist_node entries marked as primary + */ +Datum +citus_is_primary_node(PG_FUNCTION_ARGS) +{ + CheckCitusVersion(ERROR); + + bool isPrimary = false; + int32 groupId = GetLocalGroupId(); + WorkerNode *workerNode = PrimaryNodeForGroup(groupId, NULL); + if (workerNode != NULL && workerNode->nodeId == GetLocalNodeId()) + { + isPrimary = true; + } + + PG_RETURN_BOOL(isPrimary); +} /* * EnsureParentSessionHasExclusiveLockOnPgDistNode ensures given session id diff --git a/src/backend/distributed/sql/udfs/citus_is_primary_node/latest.sql b/src/backend/distributed/sql/udfs/citus_is_primary_node/latest.sql new file mode 100644 index 00000000000..deb1d50be17 --- /dev/null +++ b/src/backend/distributed/sql/udfs/citus_is_primary_node/latest.sql @@ -0,0 +1,7 @@ +CREATE FUNCTION pg_catalog.citus_is_primary_node() + RETURNS bool + LANGUAGE c + STRICT +AS 'MODULE_PATHNAME', $$citus_is_primary_node$$; +COMMENT ON FUNCTION pg_catalog.citus_is_primary_node() + IS 'returns whether the current node is the primary node in the group';