Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a method to determine if current node is primary #7720

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/backend/distributed/metadata/node_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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';