Library Cache Lock Analysis (vlibcache.sql)
What This Script Does
Section titled “What This Script Does”This script identifies sessions that are:
- Currently holding library cache locks
- Blocking other sessions waiting for library cache access
- Shows the current wait event and wait time for blocking sessions
Script
Section titled “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 ) /
SQL> @vlibcache.sql
Required Privileges
Section titled “Required Privileges”- SELECT on V$SESSION_WAIT
- SELECT on V$SESSION
- SELECT on X$KGLPN (requires SYSDBA or specific grants)
Sample Output
Section titled “Sample Output” SID SUBSTR(EVENT,1,30) WAIT_TIME---------- ------------------------------ ---------- 156 db file sequential read 0 234 SQL*Net message from client -1 512 log file sync 0
Key Output Columns
Section titled “Key Output Columns”- SID: Session ID of the blocking session
- EVENT: Current wait event of the blocking session (truncated to 30 chars)
- WAIT_TIME: Wait time status:
- 0: Currently waiting
- -1: Last wait was short (< 1 centisecond)
- -2: Waited unknown time
-
0: Last wait time in centiseconds
Understanding Library Cache Locks
Section titled “Understanding Library Cache Locks”Common Causes
Section titled “Common Causes”- DDL Operations: ALTER, DROP, CREATE statements
- Compilation: Procedures/packages being compiled
- Invalidations: Objects becoming invalid
- Hard Parsing: Complex SQL requiring library cache access
Lock Modes
Section titled “Lock Modes”- Share: Allows concurrent access for execution
- Exclusive: Blocks all other access (DDL operations)
Resolution Steps
Section titled “Resolution Steps”- Identify blocking session using this script
- Check what the blocking session is doing
- Wait for operation to complete or kill session if necessary
- Consider scheduling DDL during maintenance windows
Common Use Cases
Section titled “Common Use Cases”-
Troubleshooting Hangs
- Application timeouts during deployments
- Sessions hanging on object access
- Performance degradation during DDL
-
Deployment Planning
- Identify potential blocking scenarios
- Plan DDL execution order
- Minimize library cache contention
-
Performance Analysis
- Detect excessive parsing
- Find objects with high contention
- Optimize application design
Troubleshooting Tips
Section titled “Troubleshooting Tips”If no rows returned but library cache waits exist:
- The lock may have been released
- Check V$SESSION_WAIT directly for current waits
- Review alert log for related errors
For persistent issues:
- Enable library cache debugging
- Collect AWR/ASH reports
- Consider increasing shared pool size