On a RAC database, the session holding the lock and the sessions waiting on it are often on different instances. v$lock only shows one instance, so it can't connect them. This script snapshots gv$lock and gv$session once, then joins the snapshot so the answer is consistent.
It prints four sections:
- Locked objects: every table with a TM lock flagged as blocking, and for how many minutes. For an ordinary row-lock wait on a single instance this is usually empty: both sessions hold compatible table locks, so neither is flagged. It fills on RAC, where
BLOCK=2marks a global lock that might block (treat it as context, not proof), or when someone holds a table-level lock (LOCK TABLE). - Blocked sessions: blocker instance and SID, waiter instance and SID, minutes waiting, and the lock mode requested.
- Waiter details: user, machine, OS user, module and action,
SQL_ID, and the full SQL text for each blocked session. - Blocker details: the same for each blocker. The blocker is often idle by now, so it shows the previous SQL when there's no current statement.
Usage
SQL> @locks.sql
Run it as a user that can read the gv$ views and sys.obj$ / sys.user$, and that can create tables. The first run creates two snapshot tables, gv_lock_mon and gv_session_mon, in that user's schema. Each later run truncates and refills them.
-- locks.sql: who is blocking whom across RAC instances, with each session's SQL
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/rac-blocking-locks/
--
-- Usage: @locks.sql
--
-- Creates two snapshot tables, gv_lock_mon and gv_session_mon, in the current schema.
--- HISTORY:
-- 2013-Nov-19 VMOGILEVSKIY Added CLIENT_INFO
-- 2013-Nov-22 VMOGILEVSKIY Switched to separate batch_id for LOCKS and SESS (see mon_refresh_proc)
set term off
create table gv_lock_mon as select * from gv$lock where 1=2;
create index gv_lock_mon_indx on gv_lock_mon(type,block);
truncate table gv_lock_mon;
insert into gv_lock_mon
select * from gv$lock;
create table gv_session_mon as select x.*, x.sql_address sql_address_mon, x.sql_hash_value sql_hash_value_mon from gv$session x where 1=2;
create index gv_session_mon_indx on gv_session_mon(inst_id,sid);
truncate table gv_session_mon;
insert into gv_session_mon
select l.*,
decode(rawtohex(sql_address),'00',prev_sql_addr,sql_address),
decode(sql_hash_value,0,prev_hash_value,sql_hash_value)
from gv$session l;
commit;
set term on
ttitle off
clear col
set verify off
set serveroutput on size 1000000
set echo off
set lines 132
set trims on
set feed on
set term on
prompt blocked objects from GV$LOCK and SYS.OBJ$
set lines 132
col BLOCKED_OBJ format a35 trunc
select /*+ ORDERED */
l.inst_id
, l.sid
, l.lmode
, TRUNC(l.ctime/60) min_blocked
, u.name||'.'||o.NAME blocked_obj
from (select *
from gv_lock_mon
where type='TM'
and block!=0) l
, sys.obj$ o
, sys.user$ u
where o.obj# = l.ID1
and o.OWNER# = u.user#
and l.ctime > 10
and l.sid >= 1
order by 1,2;
prompt blocked sessions from GV$LOCK
select /*+ ORDERED */
blocker.inst_id
, blocker.sid blocker_sid
, blocked.inst_id
, blocked.sid blocked_sid
, TRUNC(blocked.ctime/60) min_blocked
, blocked.request
from (select *
from gv_lock_mon
where block != 0
and type = 'TX') blocker
, gv_lock_mon blocked
where blocked.type='TX'
and blocked.block = 0
and blocked.id1 = blocker.id1;
prompt blocked session details from GV$SESSION and GV$SQLTEXT
clear col
clear breaks
set lines 80
set trims on
set pages 9000
col sql_text format a70 word_wrapped
col inst_id format a10 noprint new_value n_inst_id
col sid format a10 noprint new_value n_sid
col serial format a10 noprint new_value n_serial
col username format a20 noprint new_value n_username
col machine format a20 noprint new_value n_machine
col osuser format a20 noprint new_value n_osuser
col process format a20 noprint new_value n_process
col action format a45 noprint new_value n_action
col SQL_ID format a13 noprint new_value n_SQL_ID
col PREV_SQL_ID format a13 noprint new_value n_PREV_SQL_ID
col display_sql_id format a13 noprint new_value n_display_sql_id
col CLIENT_INFO format a64 noprint new_value n_CLIENT_INFO
break on sid on serial on username on process on machine on action skip page
ttitle -
"Instance........ : " n_inst_id -
skip 1 -
"Sid ............ : " n_sid -
skip 1 -
"Serial ......... : " n_serial -
skip 1 -
"Username ....... : " n_username -
skip 1 -
"SQL Id ......... : " n_SQL_ID -
skip 1 -
"Prev SQL Id .... : " n_PREV_SQL_ID -
skip 1 -
"Displayed SQL Id : " n_display_sql_id -
skip 1 -
"Client Info .... : " n_CLIENT_INFO -
skip 1 -
"Machine ........ : " n_machine -
skip 1 -
"OSuser ......... : " n_osuser -
skip 1 -
"Process ........ : " n_process -
skip 1 -
"Action ......... : " n_action -
select /*+ ORDERED */
ses.inst_id,sid,serial# serial,username,machine,osuser,process,module||' '||action action,sql_text,
nvl(ses.SQL_ID,'null') SQL_ID,
nvl(ses.PREV_SQL_ID,'null') PREV_SQL_ID,
nvl(txt.SQL_ID,'null') display_sql_id,
nvl(ses.CLIENT_INFO,'null') CLIENT_INFO
--from gv$session ses, gv$sqltext txt
from gv_session_mon ses, gv$sqltext_with_newlines txt
where txt.address(+) = ses.sql_address_mon
and txt.hash_value(+) = ses.sql_hash_value_mon
and txt.inst_id(+) = ses.inst_id
and (ses.inst_id,ses.sid)
IN (select /*+ ORDERED */
-- blocker.inst_id
--, blocker.sid blocker_sid
blocked.inst_id
, blocked.sid blocked_sid
from (select *
from gv_lock_mon
where block != 0
and type = 'TX') blocker
, gv_lock_mon blocked
where blocked.type='TX'
and blocked.block = 0
and blocked.id1 = blocker.id1)
order by inst_id,sid,piece;
prompt blocker session details from GV$SESSION and GV$SQLTEXT (current or previous SQL)
select /*+ ORDERED */
ses.inst_id,sid,serial# serial,username,machine,osuser,process,module||' '||action action,sql_text,
nvl(ses.SQL_ID,'null') SQL_ID,
nvl(ses.PREV_SQL_ID,'null') PREV_SQL_ID,
nvl(txt.SQL_ID,'null') display_sql_id,
nvl(ses.CLIENT_INFO,'null') CLIENT_INFO
--from gv$session ses, gv$sqltext txt
from gv_session_mon ses, gv$sqltext_with_newlines txt
where txt.address(+) = ses.sql_address_mon
and txt.hash_value(+) = ses.sql_hash_value_mon
and txt.inst_id(+) = ses.inst_id
and (ses.inst_id,ses.sid)
IN (select /*+ ORDERED */
blocker.inst_id
, blocker.sid blocker_sid
-- blocked.inst_id
--, blocked.sid blocked_sid
from (select *
from gv_lock_mon
where block != 0
and type = 'TX') blocker
, gv_lock_mon blocked
where blocked.type='TX'
and blocked.block = 0
and blocked.id1 = blocker.id1)
order by inst_id,sid,piece;curl -O https://hashjoin.com/kb/oracle/rac-blocking-locks/locks.sql
Reading the output
Start with Blocked sessions. In this run, SID 22 holds a row lock that SID 216 is waiting for (request 6 means it wants an exclusive TX lock); on RAC the two INST_ID columns tell you which instance each is on:
INST_ID BLOCKER_SID INST_ID BLOCKED_SID MIN_BLOCKED REQUEST
---------- ----------- ---------- ----------- ----------- ----------
1 22 1 216 0 6
Blocked session details shows what the waiter is trying to run, which is the statement your application is stuck on:
Sid ............ : 216
Action ......... : KB_WAITER waiting on the holder
SQL_TEXT
----------------------------------------------------------------------
update kblab.mytable set x = x + 1
Then look up SID 22 in Blocker details, and don't expect to see the statement that took the lock. The blocker shows whatever it's doing now, which can be anything: idle in a connection pool, running a report, or, in this run, sleeping after an update it never committed:
Sid ............ : 22
Action ......... : KB_HOLDER holding a row lock
SQL_TEXT
----------------------------------------------------------------------
BEGIN dbms_session.sleep(300); END;
That's the classic picture of a transaction that was started and never committed. The usual causes are a connection pool handing back a session with an open transaction, a batch job that commits only at the end, or someone who walked away from a SQL*Plus session.
If the blocker belongs to an application, ask its owners to commit or roll back first: killing the session rolls its transaction back, which can take as long as the work did. When you do have to kill it on RAC, name the instance so you don't need to log in to it: alter system kill session 'sid,serial#,@inst_id' immediate;
Gotchas
- It leaves two tables behind. Drop
gv_lock_monandgv_session_monwhen you're done, or keep them in a monitoring schema. MIN_BLOCKEDis in whole minutes (ctime/60), so brand-new waits show 0.- The "locked objects" section ignores locks held for under 10 seconds, to cut noise.
- The snapshot is one moment. If the blocker commits between the snapshot and your read, the sessions listed may already be gone. Run it again before acting.
What it reads
gv$lock gv$session gv$sqltext_with_newlines sys.obj$ sys.user$
Related
- Drill into one session: current SQL, wait event and statistics
- Who's connected: sessions by service, host and status across RAC (gv$session)
Tired of fixing the same thing twice?
HASHJOIN turns the checks and fixes your on-call runs by hand into tooling that gives the same answer every time. Fixed scope, and you own all of the code.
New fixes and field notes by email, about once a month.