Looking for Locking / Blocking Sessions in ORACLE

There are many ways to find locking /blocking sessions in Oracle, this is what I normally use.

SELECT
     a.sid Blocker_SID,
     sb.username || ' by ' || sb.osuser||'@'|| sb.Machine  Blocker,
     sb.Program "Blocker Program",
     a.sid || ', ' ||sb.serial# Blocker_SID_Serial,
     ' is blocking ' Blockage,
     se.username || ' by ' || se.osuser||'@'|| se.Machine Blockee,
     se.Program "Blockee Program",
     b.sid || ', ' ||se.serial# Blockee_Sid_Serial,
     vs.sql_text
 FROM v$lock a join v$lock b
     ON a.block = 1 and b.request > 0 and a.id1 = b.id1 and a.id2 = b.id2
 LEFT join v$session sb on a.sid=sb.sid
 LEFT join v$session se on se.sid=b.sid
 LEFT join v$sql vs on vs.sql_id =COALESCE(sb.sql_id,sb.prev_sql_id);
 /* 
    Using COALESCE(sb.sql_id,sb.prev_sql_id) to join the Previous SQL ID 
    in the case the Blocker's query has completed but the lock is not released (open transaction) 
    
 */

If Blocker is identified, in certain case you many need to kill the blocker session with below command:

ALTER SYSTEM KILL SESSION 'sid,serial#'

Leave a Reply