ablog

不器用で落着きのない技術者のメモ

Oracle Database のプロセス、セッション、ロックの状態を表示する SQL スクリプト

Oracle Database で SQL 実行したら返ってこないとか、誰が接続しているか調べたいとか、ブロックしているセッションがないか調べたいとか、そういうときにまずは一発走らせて状況を確認するための SQL スクリプトを作ってみた。

set pagesize 1000
set linesize 300
col ospid for a5
col block for 99999
col sid for 99999
col serial# for 99999
col username for a10
col lmode for 99
col request for 99
col lock_time for a10
col program for a10
col machine for a10
col osuser for a10
col sql for a40

select proc.spid ospid, 
	ses.blocking_session block,
	ses.sid, 
	ses.serial#,
	substr(ses.username,1,10) username,
	ses.status,
	lk.type,
	lk.lmode,
	lk.request,
	id1,
	id2,
	to_char(lk.ctime/60, '9990.9') lock_time,
	sql.hash_value,
	sql.address,
	substr(sql_text,1, 40) sql,
	substr(ses.machine,1,10) machine, 
	substr(ses.osuser,1,10) osuser,
	substr(ses.program,1,10) program
from v$session ses, v$process proc, v$sql sql, v$lock lk
	where ses.type = 'USER'
		and ses.paddr = proc.addr
		and ses.sid = lk.sid(+)
		and ses.sql_address = sql.address(+)
order by username, machine, osuser, program;

実行してみると、こんな感じ。

SQL> @show_proc_ses_lock.sql
OSPID  BLOCK    SID SERIAL# USERNAME   STATUS   TY LMODE REQUEST        ID1        ID2 LOCK_TIME  HASH_VALUE ADDRESS          SQL                                      MACHINE    OSUSER     PROGRAM
----- ------ ------ ------- ---------- -------- -- ----- ------- ---------- ---------- ---------- ---------- ---------------- ---------------------------------------- ---------- ---------- ----------
21517    276    269      10 SCOTT       ACTIVE                                                                                                                          db101 oracle     imp@db
28758           252   62166 SMITH    ACTIVE   TM     3       0      61930          0     2.6    3727740418 000000024F963488 UPDATE ************** SET  CUSTOMER = C db101 oracle     sqlplus@db
28758           252   62166 SMITH    ACTIVE   TM     3       0      61873          0     2.6    3727740418 000000024F963488 UPDATE **************  SET  CUSTOMER = C db101 oracle     sqlplus@db
28758           252   62166 SMITH    ACTIVE   TX     6       0     262173     299770     2.6    3727740418 000000024F963488 UPDATE **************  SET  CUSTOMER = C db101 oracle     sqlplus@db
6899            257   21942 SYS        INACTIVE                                                                                                                        db101 oracle     sqlplus@db
5301            249   38434 SYS        ACTIVE                                                     2152291145 000000025E44B548 select proc.spid ospid,  ses.blocking_se db101 oracle     sqlplus@db
6898            251   53971 SYS        INACTIVE                                                                                                                        db101 oracle     sqlplus@db
7172            218   26420            ACTIVE                                                                                                                          db101 oracle     oracle@db

8 rows selected.