Introduction π
Maintaining the health and performance of your SQL Server Availability Groups (AG) is essential for a resilient database infrastructure. In this blog post, we’ll delve into a T-SQL script designed to retrieve detailed information about your AG replicas and the associated database health. This script provides a comprehensive overview of key metrics, allowing database administrators to monitor and ensure the robustness of their AG configurations.
Requirements π οΈ
Before using the T-SQL script, ensure that the following requirements are met:
- SQL Server Availability Groups: The script is designed for environments with configured Availability Groups.
- Permissions: The account executing the script should have sufficient permissions to query the necessary dynamic management views.
T-SQL Script π
Use the following T-SQL script to obtain detailed information about your AG replicas and database health:
SELECT
ar.replica_server_name,
adc.database_name,
ag.name AS ag_name,
CASE HDRS.is_primary_replica
WHEN 1 THEN 'Primary Replica'
ELSE 'Secondary Replica'
END AS Replica,
HDRS.synchronization_state_desc,
HDRS.synchronization_health_desc,
HDRS.recovery_lsn,
HDRS.truncation_lsn,
HDRS.last_sent_lsn,
HDRS.last_sent_time,
HDRS.last_received_lsn,
HDRS.last_received_time,
HDRS.last_hardened_lsn,
HDRS.last_hardened_time,
HDRS.last_redone_lsn,
HDRS.last_redone_time,
HDRS.log_send_queue_size,
HDRS.log_send_rate,
HDRS.redo_queue_size,
HDRS.redo_rate,
HDRS.filestream_send_rate,
HDRS.end_of_log_lsn,
HDRS.last_commit_lsn,
HDRS.last_commit_time
FROM sys.dm_hadr_database_replica_states AS HDRS
INNER JOIN sys.availability_databases_cluster AS adc
ON HDRS.group_id = adc.group_id AND
HDRS.group_database_id = adc.group_database_id
INNER JOIN sys.availability_groups AS ag
ON ag.group_id = HDRS.group_id
INNER JOIN sys.availability_replicas AS ar
ON HDRS.group_id = ar.group_id AND
HDRS.replica_id = ar.replica_id
This script combines data from various system views to present a holistic view of your AG configuration and the health status of associated databases.
Conclusion π
Effectively managing your SQL Server Availability Groups involves not only understanding the configuration details but also regularly monitoring the health of replicas and databases. By utilizing the provided T-SQL script, database administrators can proactively identify and address potential issues, ensuring the continuous availability and reliability of their databases.
Regularly run this script and incorporate the insights gained into your monitoring routine to keep your SQL Server environment robust and resilient. May your databases always be available and your replicas in sync! πͺ
Thank You,
Vivek Janakiraman
Disclaimer:
The views expressed on this blog are mine alone and do not reflect the views of my company or anyone else. All postings on this blog are provided βAS ISβ with no warranties, and confers no rights.