Global Blocking and Waiting Locks Analysis (gvlockb.sql)
What This Script Does
Section titled “What This Script Does”This script provides comprehensive analysis of blocking and waiting lock relationships across all RAC instances. It identifies lock contention situations, deadlock scenarios, and session blocking chains that can severely impact database performance. The script focuses specifically on locks where sessions are waiting (REQUEST > 0), showing both the blocking sessions and the sessions being blocked.
The Script
Section titled “The Script”rem gvlockb.sqlttitle 'Global Blocking/Waiting Locks'remset lines 600remcol ID1 format 9999999999col ID2 format 9999999999col LMODE format 999col REQUEST format 999col SID format 9999col inst_id format 999 heading 'INST'col username format a9col osuser format a8col module format a15rem ADDR RAW(4)rem KADDR RAW(4)rem TYPE VARCHAR2(2)col CTIME format 999999col 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.inst_id, l.SID, s.username, s.osuser, s.module, l.LMODE, l.REQUEST, l.TYPE, -- l.ADDR, -- l.KADDR, l.CTIME, l.BLOCK, --s.sql_hash_value s.sql_id, sw.eventfrom gv$lock l, gv$session s, gv$session_wait swwhere (l.id1, l.id2) in (select id1, id2 from gv$lock where request > 0) and s.sid(+)=l.sid+0 and s.sid = sw.sid(+) and s.inst_id = sw.inst_id(+) and s.inst_id(+) = l.inst_idorder by l.id1, l.id2, l.block desc, l.lmode desc, l.CTIME desc/Key Features
Section titled “Key Features”- RAC-Aware Analysis: Monitors locks across all instances in RAC environment
- Blocking Relationship Mapping: Clearly identifies blocker and waiter relationships
- Session Context: Shows user, OS user, and application module for each session
- Lock Detail Analysis: Displays lock modes, types, and timing information
- SQL Correlation: Includes SQL_ID and current wait events
- Hierarchical Display: Groups results by lock resource with logical ordering
@gvlockb.sqlNo parameters required - analyzes all current lock contention across instances.
Required Privileges
Section titled “Required Privileges”SELECT on GV$LOCKSELECT on GV$SESSIONSELECT on GV$SESSION_WAITSample Output
Section titled “Sample Output” Global Blocking/Waiting Locks
ID1 ID2 BLOCKER INST SID USERNAME OSUSER MODULE LMODE REQUEST TY CTIME BLOCK SQL_ID EVENT---------- ---------- ------- ---- ---- --------- -------- --------------- ----- ------- -- -------- ------ ------------- --------------- 393254 55842 Blocker 1 145 SALES_APP jsmith MyApp.exe 6 0 TX 12456 1 8fz9x2q7mv8k2 2 289 SALES_APP rjones MyApp.exe 0 6 TX 8934 0 3p7w9k5x2m1q4 enq: TX - row lock contention
393255 55843 Blocker 1 167 FINANCE mbrown Finance.exe 3 0 TM 5678 1 7k2m9p4x8v3q1 1 234 FINANCE kwhite Finance.exe 0 3 TM 3456 0 9m3k7p2x5v8q4 enq: TM - contention 2 156 REPORTS tblack Reports.exe 0 3 TM 2345 0 4x8v9m2k7p3q1 enq: TM - contention
393256 55844 1 178 BATCH_JOB oracle sqlplus.exe 0 4 TX 1234 0 6v3q9m7k2p8x5 enq: TX - allocate ITL entryKey Output Columns
Section titled “Key Output Columns”- ID1/ID2: Lock resource identifiers (combination uniquely identifies the resource)
- BLOCKER: Indicates if session is blocking others (‘Blocker’ or blank)
- INST: RAC instance ID where the session resides
- SID: Session ID holding or waiting for the lock
- USERNAME: Database username
- OSUSER: Operating system user
- MODULE: Application module name
- LMODE: Lock mode held (0=None, 1=Null, 2=Row Share, 3=Row Exclusive, 4=Share, 5=Share Row Exclusive, 6=Exclusive)
- REQUEST: Lock mode requested (same scale as LMODE, 0=not waiting)
- TY: Lock type (TX=Transaction, TM=Table, etc.)
- CTIME: Time lock has been held (in centiseconds)
- BLOCK: Number of sessions this lock is blocking
- SQL_ID: Current SQL statement identifier
- EVENT: Current wait event for the session
Understanding Lock Modes
Section titled “Understanding Lock Modes”Lock Mode Reference
Section titled “Lock Mode Reference”0 = None (not holding)1 = Null (NULL)2 = Row Share (RS)3 = Row Exclusive (RX)4 = Share (S)5 = Share Row Exclusive (SRX)6 = Exclusive (X)Lock Type Reference
Section titled “Lock Type Reference”TX = Transaction Lock (row-level)TM = Table Lock (DML operations)UL = User Lock (application-defined)CU = Cursor LockHW = High Water Mark LockST = Space Transaction LockLock Compatibility Matrix
Section titled “Lock Compatibility Matrix” | 1 2 3 4 5 6-----|------------------ 1 | Y Y Y Y Y Y 2 | Y Y Y Y Y N 3 | Y Y Y N N N 4 | Y Y N Y N N 5 | Y Y N N N N 6 | Y N N N N N
Y = Compatible, N = IncompatibleCommon Use Cases
Section titled “Common Use Cases”-
Lock Contention Analysis
- Identify blocking session chains
- Analyze lock wait patterns
- Determine root cause of contention
- Plan contention resolution strategies
-
Deadlock Investigation
- Investigate deadlock scenarios
- Analyze circular wait conditions
- Identify deadlock-prone applications
- Plan deadlock prevention strategies
-
Performance Troubleshooting
- Diagnose application slowdowns
- Identify resource bottlenecks
- Analyze wait event patterns
- Support performance tuning efforts
-
RAC-Specific Issues
- Cross-instance lock contention
- Global lock coordinator analysis
- Instance-specific blocking patterns
- Cluster interconnect impact assessment
Advanced Analysis
Section titled “Advanced Analysis”Blocking Chain Analysis
Section titled “Blocking Chain Analysis”-- Understanding blocking chains:-- Session A blocks Session B-- Session B blocks Session C-- Session C blocks Session D-- Kill Session A to resolve entire chainLock Duration Analysis
Section titled “Lock Duration Analysis”-- CTIME interpretation:-- High CTIME values indicate:-- Long-running transactions-- Potential application issues-- Inefficient transaction design-- Need for transaction optimizationCross-Instance Contention
Section titled “Cross-Instance Contention”-- RAC-specific patterns:-- Instance 1 blocking Instance 2-- Global lock coordination overhead-- Interconnect communication delays-- Consider workload partitioningTroubleshooting Applications
Section titled “Troubleshooting Applications”Immediate Resolution
Section titled “Immediate Resolution”Kill Blocking Sessions
Section titled “Kill Blocking Sessions”-- For immediate relief:-- Identify the root blocker (session with BLOCKER='Blocker')-- Consider impact of killing the session-- Use ALTER SYSTEM KILL SESSION-- Monitor for cascade effectsTransaction Analysis
Section titled “Transaction Analysis”-- Analyze blocking transactions:-- Check transaction size and scope-- Review SQL efficiency-- Identify unnecessary long transactions-- Plan transaction optimizationLong-Term Solutions
Section titled “Long-Term Solutions”Application Design
Section titled “Application Design”-- Address application issues:-- Minimize transaction duration-- Avoid unnecessary exclusive locks-- Implement proper error handling-- Use appropriate isolation levelsDatabase Configuration
Section titled “Database Configuration”-- Database-level optimizations:-- Adjust lock timeout parameters-- Configure deadlock detection-- Optimize buffer pool sizing-- Consider partitioning strategiesLock Type Analysis
Section titled “Lock Type Analysis”Transaction Locks (TX)
Section titled “Transaction Locks (TX)”Row Lock Contention
Section titled “Row Lock Contention”-- TX locks with REQUEST > 0:-- Multiple sessions updating same rows-- Long-running transactions holding locks-- Insufficient commit frequency-- Application serialization issuesITL Shortage
Section titled “ITL Shortage”-- TX locks for ITL allocation:-- Table blocks need more ITL entries-- Consider INITRANS and MAXTRANS-- Analyze block utilization-- Plan table reorganizationTable Locks (TM)
Section titled “Table Locks (TM)”DML Lock Contention
Section titled “DML Lock Contention”-- TM locks indicate:-- Conflicting DML operations-- DDL operations blocking DML-- Foreign key constraint issues-- Partition lock contentionSchema Modification
Section titled “Schema Modification”-- TM locks during DDL:-- ALTER TABLE operations-- Index creation/rebuild-- Constraint operations-- Statistics gatheringPerformance Impact Assessment
Section titled “Performance Impact Assessment”Wait Event Correlation
Section titled “Wait Event Correlation”-- Common wait events with locks:-- "enq: TX - row lock contention"-- "enq: TM - contention"-- "gc buffer busy acquire"-- "gc current block busy"Application Impact
Section titled “Application Impact”-- Assess business impact:-- Critical application modules affected-- Peak usage time correlation-- User experience degradation-- SLA violation potentialResource Utilization
Section titled “Resource Utilization”-- System resource impact:-- CPU usage patterns-- Memory consumption-- I/O wait times-- Interconnect utilizationPrevention Strategies
Section titled “Prevention Strategies”Application Development
Section titled “Application Development”-- Best practices:-- Minimize transaction scope-- Access objects in consistent order-- Implement retry logic for deadlocks-- Use appropriate lock hintsDatabase Design
Section titled “Database Design”-- Design considerations:-- Proper normalization-- Effective partitioning-- Appropriate indexing-- Foreign key designMonitoring Integration
Section titled “Monitoring Integration”-- Automated monitoring:-- Alert on high lock waits-- Track blocking session patterns-- Monitor lock duration trends-- Integrate with performance dashboardsRAC-Specific Considerations
Section titled “RAC-Specific Considerations”Global Lock Manager
Section titled “Global Lock Manager”-- RAC lock coordination:-- Global cache coordination-- Cross-instance messaging-- Lock mastering concepts-- DRM (Dynamic Resource Mastery)Instance Affinity
Section titled “Instance Affinity”-- Workload distribution:-- Partition instance affinity-- Application-instance mapping-- Service-based routing-- Connection pooling strategiesInterconnect Impact
Section titled “Interconnect Impact”-- Network considerations:-- Interconnect bandwidth-- Latency effects on locks-- Cluster wait events-- Performance monitoringIntegration with Other Analysis
Section titled “Integration with Other Analysis”Session Analysis
Section titled “Session Analysis”-- Combine with session scripts:-- gvsess.sql for detailed session info-- gvwait.sql for wait event analysis-- vsession.sql for single-instance focus-- Track session behavior patternsSQL Analysis
Section titled “SQL Analysis”-- Correlate with SQL performance:-- Identify problematic SQL statements-- Analyze execution plans-- Review bind variable usage-- Plan SQL optimizationHistorical Analysis
Section titled “Historical Analysis”-- Trend analysis:-- Track lock contention over time-- Identify peak contention periods-- Correlate with application changes-- Plan capacity and optimizationAutomation and Alerting
Section titled “Automation and Alerting”Automated Monitoring
Section titled “Automated Monitoring”-- Schedule regular execution:-- Monitor for blocking chains-- Alert on contention thresholds-- Log results for analysis-- Integrate with monitoring systemsEscalation Procedures
Section titled “Escalation Procedures”-- Define escalation criteria:-- Blocking duration thresholds-- Number of sessions affected-- Critical application impact-- Automated resolution actionsRelated Scripts
Section titled “Related Scripts”- gvlock.sql - Comprehensive lock analysis
- vlockb2.sql - Single-instance blocking locks
- vlock.sql - Basic lock analysis
- gvsess.sql - Session analysis across RAC
Best Practices
Section titled “Best Practices”Regular Monitoring
Section titled “Regular Monitoring”-- Establish monitoring routine:-- Schedule during peak hours-- Monitor for blocking patterns-- Track resolution effectiveness-- Document common scenariosDocumentation
Section titled “Documentation”-- Maintain incident documentation:-- Record blocking scenarios-- Document resolution steps-- Track application patterns-- Share knowledge with teamsProactive Management
Section titled “Proactive Management”-- Prevent issues:-- Review application design-- Optimize transaction patterns-- Implement proper indexing-- Monitor trends and patternsSummary
Section titled “Summary”This script is essential for:
- Lock Contention Analysis - Identifying and resolving blocking relationships
- Deadlock Investigation - Understanding and preventing deadlock scenarios
- RAC Performance - Analyzing cross-instance lock coordination
- Application Tuning - Optimizing transaction and locking patterns
- Incident Response - Quick identification and resolution of blocking issues