1

I have a SQL Server distributed availability group set up, and I'm currently connected to the forwarder instance. I would like to know how to use T-SQL to query the appropriate DMVs or catalog views to determine which instance is currently serving as the global primary in the distributed availability group.

I'm looking for a T-SQL script or query that I can execute on the forwarder instance to retrieve this information.

Any help or guidance on this topic would be greatly appreciated. Thank you in advance!

asked Apr 26, 2024 at 1:06

1 Answer 1

3
+200

Basically the forwarder should not have any knowledge of the AG on the other side other than the fact that the AG is part of the Distributed AG. The Distributed AG holds the endpoint_url of the listener plus the mirroring endpoint port number, this way it should always point to the primary replica's mirroring endpoint.

The below T-SQL should work, it practically wasn't tested and there may be some odd edge cases.

SELECT 
 distar.replica_server_name AS [RemoteAGName],
 LEFT
 (
 SUBSTRING
 (
 distar.[endpoint_url],
 7 /* start after TCP:// */,
 LEN(distar.[endpoint_url]) - 6
 ),
 CHARINDEX
 (
 ':',
 distar.[endpoint_url], 
 5 /* Start after : in TCP: */
 ) - 7 /* Don't include the : */
 ) AS [ListenerName]
FROM sys.availability_groups dist
 INNER JOIN sys.availability_replicas distar
 ON dist.group_id = distar.group_id
 LEFT JOIN sys.availability_groups ag
 ON ag.name = distar.replica_server_name
WHERE dist.is_distributed = 1
 AND dist.name = 'DistributedAGName'
 AND ag.group_id IS NULL;
Paul White
95.4k30 gold badges440 silver badges689 bronze badges
answered Apr 28, 2024 at 17:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.