Blocking and Waiting Locks (vlockb2.sql)
What This Script Does
Section titled “What This Script Does”This script identifies blocking and waiting lock situations in the database by showing both the blocking sessions and the sessions waiting for locks. It provides comprehensive session details to help DBAs quickly identify and resolve lock contention issues that can severely impact application performance.
Script
Section titled “Script”rem vlockb.sqlttitle 'Blocking/Waiting Locks'remset lines 132remcol ID1 format 9999999999col ID2 format 9999999999col LMODE format 999col REQUEST format 999col SID format 9999col username format a9col osuser format a8col module format a15col machine format a15rem ADDR RAW(4)rem KADDR RAW(4)rem TYPE VARCHAR2(2)col CTIME format 99999col BLOCK format 99999col blocker format a7rembreak on id1 on id2 skip 1remselect /*+ ordered use_nl(l,s) */ l.ID1, l.ID2, decode(l.block, 0, null, 'Blocker') blocker, l.SID, s.username, s.osuser, s.machine, l.LMODE, l.REQUEST, l.TYPE, l.ADDR, l.KADDR, l.CTIME, l.BLOCKfrom v$lock l, v$session swhere (l.id1, l.id2) in (select id1, id2 from v$lock where request > 0) and s.sid=l.sid+0order by l.id1, l.id2, l.block desc, l.lmode desc, l.CTIME desc/
-- Run the script in SQL*Plus or SQLcl@vlockb2.sql
-- No parameters required-- Shows only active blocking situations-- Ordered to show blockers first, then waiters
Required Privileges
Section titled “Required Privileges”- SELECT on V$LOCK
- SELECT on V$SESSION
- Available to most database users
Sample Output
Section titled “Sample Output”Blocking/Waiting Locks
ID1 ID2 BLOCKER SID USERNAME OSUSER MACHINE LMODE REQUEST TYPE ADDR KADDR CTIME BLOCK---------- ---------- ------- ----- --------- -------- --------------- ----- ------- ---- -------- -------- ------ ------ 131075 45678 Blocker 234 APPUSER oracle app-server01 6 0 TX 07000000 08000000 450 1 345 HR_USER jsmith workstation02 0 6 TX 07000001 08000001 25 0 456 APPUSER oracle app-server02 0 6 TX 07000002 08000002 10 0
262150 12345 Blocker 567 BATCH_USR batch batch-server 6 0 TM 07000003 08000003 1250 2 678 REPORT rptuser report-srv01 0 4 TM 07000004 08000004 75 0 789 APPUSER oracle app-server03 0 4 TM 07000005 08000005 60 0
Key Output Columns
Section titled “Key Output Columns”- ID1/ID2: Lock resource identifiers
- BLOCKER: Indicates session holding the blocking lock
- SID: Session ID
- USERNAME: Database username
- OSUSER: Operating system user
- MACHINE: Client machine name
- LMODE: Lock mode held (0-6)
- REQUEST: Lock mode requested (0-6)
- TYPE: Lock type (TX, TM, etc.)
- CTIME: Time lock held in seconds
- BLOCK: Number of sessions being blocked
Understanding Lock Modes
Section titled “Understanding Lock Modes”Lock Mode Values (LMODE/REQUEST)
Section titled “Lock Mode Values (LMODE/REQUEST)”- 0: None
- 1: Null (NULL)
- 2: Row-S (SS)
- 3: Row-X (SX)
- 4: Share (S)
- 5: S/Row-X (SSX)
- 6: Exclusive (X)
Common Lock Types
Section titled “Common Lock Types”- TX: Transaction locks (row-level)
- TM: Table locks (DML operations)
- UL: User-defined locks
- CF: Control file transaction
Performance Analysis
Section titled “Performance Analysis”Blocking Patterns
Section titled “Blocking Patterns”- Single blocker, multiple waiters: Common bottleneck
- Chain blocking: Session A blocks B, B blocks C
- Long CTIME values: Indicate long-running transactions
- High BLOCK count: Shows widespread impact
Impact Assessment
Section titled “Impact Assessment”- CTIME > 300: Long-held locks (>5 minutes)
- BLOCK > 5: Significant concurrency impact
- Multiple TX locks: Row-level contention
- TM locks with TX: Possible unindexed foreign keys
Common Use Cases
Section titled “Common Use Cases”Active Lock Investigation
Section titled “Active Lock Investigation”-- Identify current blocking situations@vlockb2.sql-- Note blocking SIDs and investigate their activity
Kill Blocking Sessions
Section titled “Kill Blocking Sessions”-- After identifying blockersALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;-- Get serial# from v$session
Application Deadlock Analysis
Section titled “Application Deadlock Analysis”-- Run during deadlock alerts@vlockb2.sql-- Check for circular dependencies in output
Troubleshooting Lock Issues
Section titled “Troubleshooting Lock Issues”Identifying Root Cause
Section titled “Identifying Root Cause”-
Check blocking session activity:
SELECT sql_id, sql_textFROM v$session s, v$sql qWHERE s.sid = &blocking_sidAND s.sql_id = q.sql_id; -
Find locked objects:
SELECT object_name, object_typeFROM dba_objectsWHERE object_id = &id1; -
Session wait details:
SELECT event, wait_time, seconds_in_waitFROM v$sessionWHERE sid IN (SELECT sid FROM v$lock WHERE request > 0);
Common Lock Scenarios
Section titled “Common Lock Scenarios”Unindexed Foreign Keys
Section titled “Unindexed Foreign Keys”- Symptom: TM locks during deletes/updates
- Solution: Create indexes on foreign key columns
- Detection: Check child tables of locked parent
Long-Running Transactions
Section titled “Long-Running Transactions”- Symptom: High CTIME values
- Solution: Commit more frequently
- Prevention: Implement transaction timeouts
Application Design Issues
Section titled “Application Design Issues”- Symptom: Frequent same-resource contention
- Solution: Review locking order
- Prevention: Use SELECT FOR UPDATE NOWAIT
Advanced Analysis
Section titled “Advanced Analysis”Lock Dependency Tree
Section titled “Lock Dependency Tree”-- Show complete blocking hierarchyWITH lock_tree AS ( SELECT LEVEL lvl, LPAD(' ', 2*(LEVEL-1)) || l.sid sid_tree, l.id1, l.id2, l.lmode, l.request, l.ctime, l.block FROM v$lock l START WITH l.block > 0 CONNECT BY PRIOR l.id1 = l.id1 AND PRIOR l.id2 = l.id2 AND PRIOR l.block > 0 AND l.request > 0)SELECT * FROM lock_tree;
Historical Lock Analysis
Section titled “Historical Lock Analysis”-- If using AWRSELECT * FROM dba_hist_enqueue_statWHERE enq_type IN ('TX','TM')AND wait_count > 0ORDER BY cum_wait_time DESC;
Lock Wait Time Analysis
Section titled “Lock Wait Time Analysis”-- Current wait timesSELECT l.sid, s.username, l.type, l.id1, l.id2, l.ctime lock_held_seconds, w.seconds_in_waitFROM v$lock l, v$session s, v$session_wait wWHERE l.sid = s.sidAND s.sid = w.sidAND l.request > 0ORDER BY w.seconds_in_wait DESC;
Related Scripts
Section titled “Related Scripts”- Lock Analysis (gvlock.sql) - Basic lock information
- Blocking Sessions (gvlockb.sql) - Simpler blocking view
- Session Analysis (gvsess.sql) - Detailed session information
- Enqueue Waits (enqueue.sql) - Enqueue wait statistics