
Environment

-> Application team advised that Read-Only queries are executing indefinitely on secondary database that is part of Always On Availability group.
-> I tried below query to check for blocking and could see that a system session is blocking the select queries,
select * from sysprocesses where blocked<>0
-> I tried a simple select query as below on the secondary server and had the same symptoms where in the select query was running indefinitely,
select top 1 * from [Table]
-> My session was 73 and I tried below query specifically to check what it is waiting on,
select spid,blocked,waittype, waittime,lastwaittype,dbid,waitresource from sysprocesses where blocked<>0
go
select spid,blocked,waittype, waittime,lastwaittype,dbid,waitresource from sysprocesses where spid=73

-> Select queries are blocked by Session 84. Checked what is session id 84,

-> Seems like a system session. Tried below query,
select session_id, wait_type,db_name(database_id),command from sys.dm_exec_requests where session_id = 84

-> Session ID 84 is a system thread that is leveraged for REDO operation on secondary also known as “DB STARTUP”.
-> Checked on the Primary replica to see if there are any huge write operations on object [Table].

-> I could see an alter index command on the same object [Table] on primary replica.
-> The redo thread on Secondary replica acquires schema modification (Sch-M) locks on the secondary database to make any DDL changes that are performed on primary replica. On the secondary replica, the read-only queries acquire schema stability (Sch-S) locks to process the query. In our case the select queries were not able to acquire schema stability (Sch-S) locks as the REDO thread has already acquired schema modification (Sch-M) locks and this is the reason why we were experiencing blocking.

-> In the meantime, Alter Index command completed on the primary replica and we were able to execute select queries without any issues after that.
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.