ORACLE 锁阻塞情况查询

Posted 渔夫数据库笔记

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ORACLE 锁阻塞情况查询相关的知识,希望对你有一定的参考价值。

一:通过 gv$session 查看所有被阻塞的会话

set line 200;
col TERMINAL format a10;
col PROGRAM format a20;
col USERNAME format a10;
col MACHINE format a10;
col SQL_TEXT format a40;
select a.sid,a.SERIAL#,a.USERNAME,a.COMMAND,a.LOCKWAIT,a.STATUS,a.MACHINE,a.TERMINAL,a.PROGRAM,a.SECONDS_IN_WAIT,b.sql_text from v$session a, v$sql b where (b.sql_id = a.sql_id) and (a.blocking_instance is not null and a.blocking_session is not null);

输出如下:
select a.sid,a.SERIAL#,a.USERNAME,a.COMMAND,a.LOCKWAIT,a.STATUS,a.MACHINE,a.TERMINAL,a.PROGRAM,a.SECONDS_IN_WAIT,b.sql_text from v$session a, v$sql b where (b.sql_id = a.sql_id) and (a.blocking_instance is not null and a.blocking_session is not null);

       SID    SERIAL# USERNAME	    COMMAND LOCKWAIT	     STATUS   MACHINE	 TERMINAL   PROGRAM		 SECONDS_IN_WAIT SQL_TEXT
---------- ---------- ---------- ---------- ---------------- -------- ---------- ---------- -------------------- --------------- ----------------------------------------
       217	32683 SYS		  7 00000001D5CF5E50 ACTIVE   app-db-n	 pts/3	    sqlplus@app-db-n (TN	     192 delete from UNITEAPP.TEST_TABLE_1 where
											    S V1-V3)				 PROD_INST_ID='910453042866'

       225	 4543 SYS		  7 00000001D5CF30B0 ACTIVE   app-db-n	 pts/1	    sqlplus@app-db-n (TN	     189 delete from UNITEAPP.TEST_TABLE_1 where
											    S V1-V3)				 PROD_INST_ID='910453042866'

如果当前会话被阻塞BLOCKING_SESSION_STATUS 显示 VALID ,a.BLOCKING_INSTANCE,a.BLOCKING_SESSION 显示阻塞者的实例id和会话id

一:展示阻塞的树形结构,显示阻塞者和被阻塞者的instance_id 和 sid

with lk as (select blocking_instance||'.'||blocking_session blocker, inst_id||'.'||sid waiter 
            from gv$session where blocking_instance is not null and blocking_session is not null)
select lpad('  ',2*(level-1))||waiter lock_tree from
 (select * from lk
  union all
  select distinct 'root', blocker from lk
  where blocker not in (select waiter from lk))
connect by prior waiter=blocker start with blocker='root';

输出结果如下:
LOCK_TREE
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.209
  1.217
  1.225


三:展示阻塞的树形结构,并输出阻塞语句,被阻塞语句,并给出杀会话语句

with lk as (select a.blocking_instance||'.'||a.blocking_session blocker, a.inst_id||'.'||a.sid waiter,(select b.sql_text||'  alter system kill session '||''''||c.SID||', '||c.SERIAL#||'''' from gv$sqlarea b,gv$session c where a.BLOCKING_INSTANCE=c.INST_ID and c.SID=a.BLOCKING_SESSION and (c.SQL_ID=b.SQL_ID or c.PREV_SQL_ID=b.SQL_ID)) kill_block_sql,(select sql_text||'  alter system kill session '||''''||a.SID||', '||a.SERIAL#||'''' from gv$sqlarea b where a.INST_ID=b.INST_ID and a.SQL_ID=b.SQL_ID) kill_waiter_sql
        from gv$session a where a.blocking_instance is not null and a.blocking_session is not null)
select lpad('  ',2*(level-1))||waiter||'  '||kill_waiter_sql lock_tree from
 (select blocker,waiter,kill_waiter_sql from lk
  union all
  select distinct 'root',blocker,kill_block_sql from lk
  where blocker not in (select waiter from lk))
connect by prior waiter=blocker start with blocker='root';

输出结果如下:
LOCK_TREE
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1.209  create index idx_ICCID on UNITEAPP.TEST_TABLE_1(ICCID) tablespace TBS_UNITEAPP  alter system kill session '209, 45763'
  1.217  delete from UNITEAPP.TEST_TABLE_1 where PROD_INST_ID='910453042866'  alter system kill session '217, 32683'
  1.225  delete from UNITEAPP.TEST_TABLE_1 where PROD_INST_ID='910453042866'  alter system kill session '225, 4543'

四:直接显示阻塞关系

col block_msg for a80
select c.terminal||' ('''||a.sid||','||c.serial#||''') is blocking '||b.sid block_msg
from v$lock a,v$lock b,v$session c
where a.id1=b.id1
and a.id2=b.id2
and a.block>0
and a.sid<>b.sid
and a.sid=c.sid;

输入结果如下:

BLOCK_MSG
--------------------------------------------------------------------------------
pts/2 ('209,45763') is blocking 225
pts/2 ('209,45763') is blocking 217

以上是关于ORACLE 锁阻塞情况查询的主要内容,如果未能解决你的问题,请参考以下文章

ORACLE 锁阻塞情况查询

一条查询Oracle中的阻塞锁(以及阻塞在哪个数据上)的SQL

一条查询Oracle中的阻塞锁(以及阻塞在哪个数据上)的SQL

Mysql 查询阻塞和事物情况

oracle查询锁表与解锁情况提供解决方案

oracle 锁表处理