Finding blocking locks
When to use it
When users say the application is frozen, a screen never returns, or a batch job stopped making progress.
Run this
Script: 02_query_blocking_locks.sql. Full version in Administration › Session management › Session locking.
-- ============================================================
-- Query: Blocking Locks
-- Descrição: Identifica locks com bloqueio
-- ============================================================
SET PAGESIZE 100
SET LINESIZE 250
SET LONG 2000
SET LONGCHUNKSIZE 2000
COLUMN RESULT FORMAT A250 WORD_WRAPPED
SELECT BLOCK || SESSION_INFO || ' SqlID=' || SQL_ID || ' Username=' || USERNAME ||
' Program='|| PROGRAM || ' Machine=' || MACHINE || ' Module=' || MODULE ||
' Action=' || ACTION || ' Sql=' || sql_text RESULT
FROM (
SELECT
DECODE(a.block,
0, ' BLOQUEADO ',
1, 'BLOQUEADOR ',
2, 'BLOQUEADOR_GBL ',
TO_CHAR(a.block)
) block,
LPAD(' ', DECODE(a.request, 0, 0, 3)) || a.sid || ',' || b.serial# || ',@' || a.inst_id session_info,
NVL(b.sql_id, b.prev_sql_id) sql_id,
b.username,
SUBSTR(b.program, 1, 25) program,
SUBSTR(b.machine, 1, 25) machine,
SUBSTR(b.module, 1, 25) module,
SUBSTR(b.action, 1, 50) action,
SUBSTR(c.sql_text, 1, 110) || '...' sql_text,
a.id1 || '|' || a.id2 || '|' || a.type id1id2type
FROM gv$lock a, gv$session b, gv$sqlstats c
WHERE (a.type, a.id1, a.id2) IN
(SELECT d.type, d.id1, d.id2
FROM gv$lock d
WHERE d.request > 0)
AND a.inst_id = b.inst_id
AND a.sid = b.sid
AND NVL(b.sql_id, b.prev_sql_id) = c.sql_id
AND b.type <> 'BACKGROUND'
ORDER BY a.id1, a.id2, a.type, a.request, a.ctime DESC
);
The labels in the output are in Portuguese: BLOQUEADOR is the blocker, BLOQUEADO is the blocked session, and BLOQUEADOR_GBL is a blocker seen through a global (RAC) lock.
More detailed versions: 01_show_blocking_locks.sql (Administration › Session management › Session locking): full blocking report across instances.
How to read the output
- No rows means nobody is waiting on a lock. Look for another cause, such as a slow SQL (chapter 9).
- Each blocker is listed first, with the sessions it blocks indented below it.
- The session is shown as
sid,serial#,@instance, the same format the kill command uses. Program,MachineandModuletell you who or what the blocker is: a person in a tool, an application server or a batch job.
What to do next
- Find the session at the top of the chain. Killing a blocked session solves nothing.
- A blocker is often an idle session with an uncommitted change. The right fix is usually to ask that person to commit or roll back.
- If the blocker must be ended, follow "Killing a session safely" and get confirmation first.
Risk level
READ ONLY Safe to run at any time. It only queries the data dictionary.