Skip to content

Commit 64bbead

Browse files
Skip non-readable AG secondary databases in cross-database collectors (#325) (#345)
Collectors that iterate databases and USE/query into them now exclude AG secondary replicas configured with READ_ONLY connections, which reject non-read-intent sessions with "not accessible for queries" errors. Filter uses sys.availability_replicas.secondary_role_allow_connections_desc to identify secondaries that won't accept our connection type. Databases on primaries, non-AG databases, and secondaries with ALL connections continue to be collected normally. Changed locations: - install/39_collect_database_configuration.sql (scoped config cursor) - install/09_collect_query_store.sql (QS discovery cursor) - Lite/RemoteCollectorService.ServerConfig.cs (scoped config db list) - Lite/RemoteCollectorService.QueryStore.cs (QS discovery cursor) Clean install verified on sql2016 with zero errors. Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 640eb23 commit 64bbead

4 files changed

Lines changed: 68 additions & 0 deletions

File tree

Lite/Services/RemoteCollectorService.QueryStore.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ WHERE d.database_id > 4
4848
AND d.database_id < 32761
4949
AND d.state_desc = N'ONLINE'
5050
AND d.name <> N'PerformanceMonitor'
51+
AND d.database_id NOT IN
52+
(
53+
SELECT
54+
d2.database_id
55+
FROM sys.databases AS d2
56+
JOIN sys.availability_replicas AS r
57+
ON d2.replica_id = r.replica_id
58+
WHERE NOT EXISTS
59+
(
60+
SELECT
61+
1/0
62+
FROM sys.dm_hadr_availability_group_states AS s
63+
WHERE s.primary_replica = r.replica_server_name
64+
)
65+
AND r.secondary_role_allow_connections_desc = N'READ_ONLY'
66+
AND r.replica_server_name = @@SERVERNAME
67+
)
5168
OPTION(RECOMPILE);
5269
5370
OPEN db_check;

Lite/Services/RemoteCollectorService.ServerConfig.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,23 @@ FROM sys.databases AS d
336336
AND d.database_id < 32761
337337
AND d.name <> N'PerformanceMonitor'
338338
AND d.state_desc = N'ONLINE'
339+
AND d.database_id NOT IN
340+
(
341+
SELECT
342+
d2.database_id
343+
FROM sys.databases AS d2
344+
JOIN sys.availability_replicas AS r
345+
ON d2.replica_id = r.replica_id
346+
WHERE NOT EXISTS
347+
(
348+
SELECT
349+
1/0
350+
FROM sys.dm_hadr_availability_group_states AS s
351+
WHERE s.primary_replica = r.replica_server_name
352+
)
353+
AND r.secondary_role_allow_connections_desc = N'READ_ONLY'
354+
AND r.replica_server_name = @@SERVERNAME
355+
)
339356
ORDER BY d.name
340357
OPTION(RECOMPILE);";
341358

install/09_collect_query_store.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,23 @@ BEGIN
291291
AND d.is_read_only = 0
292292
AND d.name <> N'PerformanceMonitor'
293293
AND d.database_id < 32761 /*exclude contained AG system databases*/
294+
AND d.database_id NOT IN
295+
(
296+
SELECT
297+
d2.database_id
298+
FROM sys.databases AS d2
299+
JOIN sys.availability_replicas AS r
300+
ON d2.replica_id = r.replica_id
301+
WHERE NOT EXISTS
302+
(
303+
SELECT
304+
1/0
305+
FROM sys.dm_hadr_availability_group_states AS s
306+
WHERE s.primary_replica = r.replica_server_name
307+
)
308+
AND r.secondary_role_allow_connections_desc = N'READ_ONLY'
309+
AND r.replica_server_name = @@SERVERNAME
310+
)
294311
OPTION(RECOMPILE);
295312

296313
OPEN @db_check_cursor;

install/39_collect_database_configuration.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,23 @@ BEGIN
165165
AND d.name != DB_NAME()
166166
AND d.state_desc = N'ONLINE'
167167
AND d.database_id < 32761 /*exclude contained AG system databases*/
168+
AND d.database_id NOT IN
169+
(
170+
SELECT
171+
d2.database_id
172+
FROM sys.databases AS d2
173+
JOIN sys.availability_replicas AS r
174+
ON d2.replica_id = r.replica_id
175+
WHERE NOT EXISTS
176+
(
177+
SELECT
178+
1/0
179+
FROM sys.dm_hadr_availability_group_states AS s
180+
WHERE s.primary_replica = r.replica_server_name
181+
)
182+
AND r.secondary_role_allow_connections_desc = N'READ_ONLY'
183+
AND r.replica_server_name = @@SERVERNAME
184+
)
168185
ORDER BY
169186
d.name
170187
OPTION (RECOMPILE);

0 commit comments

Comments
 (0)