Library Cache Lock Analysis (vlibcache.sql)
What This Script Does
Section titled “What This Script Does”This script identifies sessions that are holding library cache locks and blocking other sessions. It:
- Finds sessions waiting on library cache locks
- Identifies the sessions holding those locks
- Shows what the holding sessions are currently doing
- Helps diagnose library cache contention issues
The Script
Section titled “The Script”select sid,substr(event,1,30),wait_time from v$session_wait where sid in (select sid from x$kglpn , v$session where KGLPNHDL in (select p1raw from v$session_wait where wait_time=0 and event like 'library cache lock%') and KGLPNMOD <> 0 and v$session.saddr=x$kglpn.kglpnuse ) /
-- Basic usage@vlibcache.sql
-- No parameters required
Required Privileges
Section titled “Required Privileges”SELECT ON V$SESSION_WAITSELECT ON V$SESSIONSELECT ON X$KGLPN
Sample Output
Section titled “Sample Output” SID SUBSTR(EVENT,1,30) WAIT_TIME---------- ------------------------------ ---------- 125 db file sequential read 0 248 SQL*Net message from client -1 367 PL/SQL lock timer 0
Key Output Columns
Section titled “Key Output Columns”- SID - Session ID of the blocking session
- EVENT - Current wait event of the blocking session
- WAIT_TIME - Wait time status:
- 0: Currently waiting
- -1: Last wait was less than 1 centisecond
- >0: Time of last wait in centiseconds
Common Wait Events for Blocking Sessions
Section titled “Common Wait Events for Blocking Sessions”- SQL*Net message from client - Session idle, waiting for client
- db file sequential read - Reading blocks from disk
- PL/SQL lock timer - Executing DBMS_LOCK.SLEEP
- direct path read/write - Performing direct I/O operations
Common Use Cases
Section titled “Common Use Cases”Diagnose Compilation Hangs
-- When DDL or package compilation hangs@vlibcache.sql-- Identify and potentially kill blocking sessions
Troubleshoot Application Freezes
-- When application seems frozen during deployment@vlibcache.sql-- Find sessions preventing code changes
Understanding Library Cache Locks
Section titled “Understanding Library Cache Locks”Common Causes:
- Long-running procedures being recompiled
- DDL operations on frequently accessed objects
- Package state issues requiring recompilation
- Debugging sessions holding locks
Lock Modes:
- Null (0): No lock
- Shared (2): Can read object definition
- Exclusive (3): Can modify object definition
Troubleshooting Steps
Section titled “Troubleshooting Steps”- Identify blockers using this script
- Get more details about blocking sessions:
SELECT sid, serial#, username, program, machine, sql_idFROM v$sessionWHERE sid IN (/* SIDs from vlibcache.sql output */);
- View what objects are locked:
SELECT * FROM v$db_object_cacheWHERE locks > 0 OR pins > 0ORDER BY locks DESC, pins DESC;
- If necessary, kill blocking session:
ALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
Prevention Strategies
Section titled “Prevention Strategies”- Avoid compiling objects during peak hours
- Use edition-based redefinition for online changes
- Implement proper error handling to avoid hung sessions
- Set appropriate timeouts for long operations
Related Scripts
Section titled “Related Scripts”- Blocking Locks - General lock blocking analysis
- Active Sessions - View all active sessions
- Compile Invalid Objects - Recompile invalid objects