SQL query to check if secondary database is removed from availability group
I have an AG with one primary replica and one secondary replica.
I have created a script that queries transaction log backups from these database replicas and restores them to a target database.
Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.
The script now fails to restore as the recovery fork guid has changed on the secondary database.
Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?
Thanks!
sql-server backup availability-groups
New contributor
add a comment |
I have an AG with one primary replica and one secondary replica.
I have created a script that queries transaction log backups from these database replicas and restores them to a target database.
Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.
The script now fails to restore as the recovery fork guid has changed on the secondary database.
Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?
Thanks!
sql-server backup availability-groups
New contributor
add a comment |
I have an AG with one primary replica and one secondary replica.
I have created a script that queries transaction log backups from these database replicas and restores them to a target database.
Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.
The script now fails to restore as the recovery fork guid has changed on the secondary database.
Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?
Thanks!
sql-server backup availability-groups
New contributor
I have an AG with one primary replica and one secondary replica.
I have created a script that queries transaction log backups from these database replicas and restores them to a target database.
Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.
The script now fails to restore as the recovery fork guid has changed on the secondary database.
Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?
Thanks!
sql-server backup availability-groups
sql-server backup availability-groups
New contributor
New contributor
edited 1 hour ago
Tony Hinkle
2,1981422
2,1981422
New contributor
asked 1 hour ago
S.K.S.K.
1135
1135
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You could use exists
with the sys.dm_hadr_database_replica_states
DMV to see if the DB is in a AG.
Returns a row for each database that is participating in an Always On
availability group for which the local instance of SQL Server is
hosting an availability replica. This dynamic management view exposes
state information on both the primary and secondary replicas. On a
secondary replica, this view returns a row for every secondary
database on the server instance. On the primary replica, this view
returns a row for each primary database and an additional row for the
corresponding secondary database
Here is the query:
if exists(
select
DBName = db_name(s.database_id)
,s.is_local
,s.synchronization_state_desc
,s.synchronization_health_desc
from sys.dm_hadr_database_replica_states s
where db_name(s.database_id) = 'your_database'
)
begin
--do something useful
end
else
begin
--exit, probably without error
end
This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0
to the query. Or you can use this method or a similar approach.
Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.
– S.K.
1 hour ago
I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?
– scsimon
1 hour ago
It will still be in the AG on the primary if the secondary replica was removed from the AG.
– Tony Hinkle
1 hour ago
Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.
– scsimon
41 mins ago
1
@scsimon Yep--good work. Your query is lighter weight than mine, also.
– Tony Hinkle
30 mins ago
add a comment |
You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.
SELECT
*
FROM
sys.dm_hadr_availability_replica_states rs
JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
WHERE
dcs.database_name = '<databasename>'
AND acs.replica_server_name = '<servername>'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "182"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
S.K. is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229895%2fsql-query-to-check-if-secondary-database-is-removed-from-availability-group%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use exists
with the sys.dm_hadr_database_replica_states
DMV to see if the DB is in a AG.
Returns a row for each database that is participating in an Always On
availability group for which the local instance of SQL Server is
hosting an availability replica. This dynamic management view exposes
state information on both the primary and secondary replicas. On a
secondary replica, this view returns a row for every secondary
database on the server instance. On the primary replica, this view
returns a row for each primary database and an additional row for the
corresponding secondary database
Here is the query:
if exists(
select
DBName = db_name(s.database_id)
,s.is_local
,s.synchronization_state_desc
,s.synchronization_health_desc
from sys.dm_hadr_database_replica_states s
where db_name(s.database_id) = 'your_database'
)
begin
--do something useful
end
else
begin
--exit, probably without error
end
This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0
to the query. Or you can use this method or a similar approach.
Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.
– S.K.
1 hour ago
I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?
– scsimon
1 hour ago
It will still be in the AG on the primary if the secondary replica was removed from the AG.
– Tony Hinkle
1 hour ago
Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.
– scsimon
41 mins ago
1
@scsimon Yep--good work. Your query is lighter weight than mine, also.
– Tony Hinkle
30 mins ago
add a comment |
You could use exists
with the sys.dm_hadr_database_replica_states
DMV to see if the DB is in a AG.
Returns a row for each database that is participating in an Always On
availability group for which the local instance of SQL Server is
hosting an availability replica. This dynamic management view exposes
state information on both the primary and secondary replicas. On a
secondary replica, this view returns a row for every secondary
database on the server instance. On the primary replica, this view
returns a row for each primary database and an additional row for the
corresponding secondary database
Here is the query:
if exists(
select
DBName = db_name(s.database_id)
,s.is_local
,s.synchronization_state_desc
,s.synchronization_health_desc
from sys.dm_hadr_database_replica_states s
where db_name(s.database_id) = 'your_database'
)
begin
--do something useful
end
else
begin
--exit, probably without error
end
This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0
to the query. Or you can use this method or a similar approach.
Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.
– S.K.
1 hour ago
I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?
– scsimon
1 hour ago
It will still be in the AG on the primary if the secondary replica was removed from the AG.
– Tony Hinkle
1 hour ago
Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.
– scsimon
41 mins ago
1
@scsimon Yep--good work. Your query is lighter weight than mine, also.
– Tony Hinkle
30 mins ago
add a comment |
You could use exists
with the sys.dm_hadr_database_replica_states
DMV to see if the DB is in a AG.
Returns a row for each database that is participating in an Always On
availability group for which the local instance of SQL Server is
hosting an availability replica. This dynamic management view exposes
state information on both the primary and secondary replicas. On a
secondary replica, this view returns a row for every secondary
database on the server instance. On the primary replica, this view
returns a row for each primary database and an additional row for the
corresponding secondary database
Here is the query:
if exists(
select
DBName = db_name(s.database_id)
,s.is_local
,s.synchronization_state_desc
,s.synchronization_health_desc
from sys.dm_hadr_database_replica_states s
where db_name(s.database_id) = 'your_database'
)
begin
--do something useful
end
else
begin
--exit, probably without error
end
This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0
to the query. Or you can use this method or a similar approach.
You could use exists
with the sys.dm_hadr_database_replica_states
DMV to see if the DB is in a AG.
Returns a row for each database that is participating in an Always On
availability group for which the local instance of SQL Server is
hosting an availability replica. This dynamic management view exposes
state information on both the primary and secondary replicas. On a
secondary replica, this view returns a row for every secondary
database on the server instance. On the primary replica, this view
returns a row for each primary database and an additional row for the
corresponding secondary database
Here is the query:
if exists(
select
DBName = db_name(s.database_id)
,s.is_local
,s.synchronization_state_desc
,s.synchronization_health_desc
from sys.dm_hadr_database_replica_states s
where db_name(s.database_id) = 'your_database'
)
begin
--do something useful
end
else
begin
--exit, probably without error
end
This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0
to the query. Or you can use this method or a similar approach.
edited 1 hour ago
answered 1 hour ago
scsimonscsimon
1,398414
1,398414
Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.
– S.K.
1 hour ago
I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?
– scsimon
1 hour ago
It will still be in the AG on the primary if the secondary replica was removed from the AG.
– Tony Hinkle
1 hour ago
Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.
– scsimon
41 mins ago
1
@scsimon Yep--good work. Your query is lighter weight than mine, also.
– Tony Hinkle
30 mins ago
add a comment |
Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.
– S.K.
1 hour ago
I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?
– scsimon
1 hour ago
It will still be in the AG on the primary if the secondary replica was removed from the AG.
– Tony Hinkle
1 hour ago
Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.
– scsimon
41 mins ago
1
@scsimon Yep--good work. Your query is lighter weight than mine, also.
– Tony Hinkle
30 mins ago
Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.
– S.K.
1 hour ago
Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.
– S.K.
1 hour ago
I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?
– scsimon
1 hour ago
I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?
– scsimon
1 hour ago
It will still be in the AG on the primary if the secondary replica was removed from the AG.
– Tony Hinkle
1 hour ago
It will still be in the AG on the primary if the secondary replica was removed from the AG.
– Tony Hinkle
1 hour ago
Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.
– scsimon
41 mins ago
Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.
– scsimon
41 mins ago
1
1
@scsimon Yep--good work. Your query is lighter weight than mine, also.
– Tony Hinkle
30 mins ago
@scsimon Yep--good work. Your query is lighter weight than mine, also.
– Tony Hinkle
30 mins ago
add a comment |
You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.
SELECT
*
FROM
sys.dm_hadr_availability_replica_states rs
JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
WHERE
dcs.database_name = '<databasename>'
AND acs.replica_server_name = '<servername>'
add a comment |
You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.
SELECT
*
FROM
sys.dm_hadr_availability_replica_states rs
JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
WHERE
dcs.database_name = '<databasename>'
AND acs.replica_server_name = '<servername>'
add a comment |
You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.
SELECT
*
FROM
sys.dm_hadr_availability_replica_states rs
JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
WHERE
dcs.database_name = '<databasename>'
AND acs.replica_server_name = '<servername>'
You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.
SELECT
*
FROM
sys.dm_hadr_availability_replica_states rs
JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
WHERE
dcs.database_name = '<databasename>'
AND acs.replica_server_name = '<servername>'
edited 33 mins ago
answered 49 mins ago
Tony HinkleTony Hinkle
2,1981422
2,1981422
add a comment |
add a comment |
S.K. is a new contributor. Be nice, and check out our Code of Conduct.
S.K. is a new contributor. Be nice, and check out our Code of Conduct.
S.K. is a new contributor. Be nice, and check out our Code of Conduct.
S.K. is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Database Administrators Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229895%2fsql-query-to-check-if-secondary-database-is-removed-from-availability-group%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown