Latch Names Lookup (vlatchname.sql)
Latch Names Lookup
Section titled “Latch Names Lookup”This script provides a simple lookup utility to find Oracle latch names and their corresponding latch numbers, essential for performance analysis and troubleshooting latch contention issues.
Script: vlatchname.sql
Section titled “Script: vlatchname.sql”select latch#, namefrom v$latchnamewhere latch# like '&latchnum'order by 1/
What This Script Does
Section titled “What This Script Does”This script provides latch information lookup by:
- Interactive Search: Prompts for latch number pattern using LIKE operator
- Number-to-Name Mapping: Shows latch numbers with their descriptive names
- Pattern Matching: Supports wildcards for finding related latches
- Ordered Results: Lists latches in numerical order
- Simple Interface: Quick lookup tool for performance analysis
-- Find a specific latch by number@vlatchname.sqlEnter value for latchnum: 177
-- Find all latches (use wildcard)@vlatchname.sqlEnter value for latchnum: %
-- Find latches in a range@vlatchname.sqlEnter value for latchnum: 17%
-- Direct usage without promptingSELECT latch#, name FROM v$latchname WHERE name LIKE '%cache%';
Required Privileges
Section titled “Required Privileges”- SELECT privilege on
V$LATCHNAME
- Typically requires DBA role or SELECT_CATALOG_ROLE
Sample Output
Section titled “Sample Output” LATCH# NAME---------- -------------------------------------------------- 177 cache buffers chains 178 cache buffers lru chain 179 checkpoint queue latch 180 redo writing 181 redo allocation 182 archive process latch 183 library cache 184 library cache pin 185 library cache pin allocation 186 shared pool
Understanding Oracle Latches
Section titled “Understanding Oracle Latches”What Are Latches?
Section titled “What Are Latches?”- Serialization Mechanisms: Protect shared memory structures in Oracle
- Low-Level Locks: Lightweight, short-duration locks
- Performance Critical: Essential for Oracle’s internal operations
- Contention Points: Can become bottlenecks under heavy load
Common Latch Types
Section titled “Common Latch Types”- Cache Buffers Chains: Protects buffer cache hash chains
- Library Cache: Protects shared SQL and PL/SQL areas
- Shared Pool: Protects shared pool memory allocation
- Redo Allocation: Protects redo log buffer allocation
- LRU Chain: Protects buffer cache LRU chain operations
Common Use Cases
Section titled “Common Use Cases”1. Latch Contention Investigation
Section titled “1. Latch Contention Investigation”-- After identifying latch contention, find the latch name@vlatchname.sqlEnter value for latchnum: 177
-- Find all cache-related latchesSELECT latch#, name FROM v$latchname WHERE name LIKE '%cache%';
-- Find all redo-related latchesSELECT latch#, name FROM v$latchname WHERE name LIKE '%redo%';
2. Performance Analysis Correlation
Section titled “2. Performance Analysis Correlation”-- Correlate with latch statisticsSELECT ln.name, ls.gets, ls.misses, ls.sleepsFROM v$latch ls, v$latchname lnWHERE ls.latch# = ln.latch# AND ln.latch# = 177ORDER BY ls.sleeps DESC;
-- Find top latch contentionSELECT ln.name, ls.sleeps, ls.wait_timeFROM v$latch ls, v$latchname lnWHERE ls.latch# = ln.latch# AND ls.sleeps > 0ORDER BY ls.sleeps DESC;
3. Historical Analysis
Section titled “3. Historical Analysis”-- AWR-based latch analysisSELECT ln.name, SUM(sleeps_delta) as total_sleeps, SUM(wait_time_delta) as total_wait_timeFROM dba_hist_latch h, v$latchname lnWHERE h.latch_number = ln.latch# AND h.snap_id BETWEEN &begin_snap AND &end_snapGROUP BY ln.nameORDER BY total_sleeps DESC;
4. Specific Latch Categories
Section titled “4. Specific Latch Categories”-- Library cache latchesSELECT latch#, name FROM v$latchnameWHERE name LIKE '%library%' OR name LIKE '%cache%'ORDER BY latch#;
-- Memory management latchesSELECT latch#, name FROM v$latchnameWHERE name LIKE '%shared pool%' OR name LIKE '%heap%'ORDER BY latch#;
-- I/O related latchesSELECT latch#, name FROM v$latchnameWHERE name LIKE '%buffer%' OR name LIKE '%file%'ORDER BY latch#;
Integration with Performance Analysis
Section titled “Integration with Performance Analysis”1. Latch Wait Analysis
Section titled “1. Latch Wait Analysis”-- Current latch waitsSELECT s.sid, s.username, s.event, s.p1, s.p2, ln.nameFROM v$session s, v$latchname lnWHERE s.event LIKE 'latch%' AND s.p2 = ln.latch#;
-- Latch holder identificationSELECT s.sid, s.username, s.program, ln.nameFROM v$session s, v$latchholder lh, v$latchname lnWHERE s.sid = lh.sid AND lh.latch# = ln.latch#;
2. Comprehensive Latch Report
Section titled “2. Comprehensive Latch Report”-- Complete latch analysisSELECT ln.name, l.gets, l.misses, l.sleeps, l.immediate_gets, l.immediate_misses, ROUND(l.misses/l.gets*100, 2) as miss_ratio, ROUND(l.sleeps/l.misses*100, 2) as sleep_ratioFROM v$latch l, v$latchname lnWHERE l.latch# = ln.latch# AND l.gets > 0ORDER BY l.sleeps DESC;
3. Latch Trends Over Time
Section titled “3. Latch Trends Over Time”-- Hourly latch activitySELECT TO_CHAR(sample_time, 'YYYY-MM-DD HH24') as hour, event, COUNT(*) as wait_countFROM v$active_session_historyWHERE event LIKE 'latch%' AND sample_time >= SYSDATE - 1GROUP BY TO_CHAR(sample_time, 'YYYY-MM-DD HH24'), eventORDER BY hour, wait_count DESC;
Troubleshooting Common Latch Issues
Section titled “Troubleshooting Common Latch Issues”1. Cache Buffers Chains Latch
Section titled “1. Cache Buffers Chains Latch”-- Find hot blocks causing contentionSELECT obj.owner, obj.object_name, obj.object_typeFROM v$bh bh, dba_objects objWHERE bh.obj = obj.object_id AND bh.file# || '-' || bh.block# IN ( SELECT p1 || '-' || p2 FROM v$session_wait WHERE event = 'latch: cache buffers chains' );
2. Library Cache Latch
Section titled “2. Library Cache Latch”-- Find frequently parsed SQLSELECT sql_text, parse_calls, executionsFROM v$sqlareaWHERE parse_calls > 100ORDER BY parse_calls DESC;
3. Shared Pool Latch
Section titled “3. Shared Pool Latch”-- Check shared pool fragmentationSELECT pool, name, bytes/(1024*1024) as mbFROM v$sgastatWHERE pool = 'shared pool' AND name = 'free memory'ORDER BY bytes DESC;
Oracle Version Considerations
Section titled “Oracle Version Considerations”Version Differences
Section titled “Version Differences”- Oracle 9i and earlier: Fewer latch types
- Oracle 10g+: Additional latches for new features
- Oracle 11g+: Mutex introduction reduces some latch usage
- Oracle 12c+: Additional latches for new functionality
Evolution of Latches
Section titled “Evolution of Latches”-- Count latches by version (documentation purposes)SELECT COUNT(*) as total_latches FROM v$latchname;
-- Find version-specific latchesSELECT name FROM v$latchnameWHERE name LIKE '%mutex%' OR name LIKE '%adaptive%';
Best Practices
Section titled “Best Practices”Analysis Guidelines
Section titled “Analysis Guidelines”- Start with Wait Events: Use latch waits in V$SESSION_WAIT first
- Identify Patterns: Look for consistent latch contention
- Correlate with Application: Match latch waits to application activity
- Historical Analysis: Use AWR/StatsPack for trend analysis
- Focus on Sleeps: Latch sleeps indicate contention
Performance Tuning
Section titled “Performance Tuning”- Application Changes: Often most effective for latch reduction
- Parameter Tuning: Adjust relevant Oracle parameters
- SQL Optimization: Reduce parsing for library cache latches
- Buffer Cache: Tune for cache buffers chains issues
- Shared Pool: Size appropriately to reduce allocation latches
Integration with Other Scripts
Section titled “Integration with Other Scripts”This script works well with:
- Latch Statistics (vlatch.sql) - For detailed latch statistics
- Active Session Analysis (gvsess.sql) - For session-level latch waits
- Wait Event Analysis (vsysev.sql) - For system-wide wait analysis
- Library Cache Analysis (vlibcache.sql) - For library cache latch issues
Quick Reference
Section titled “Quick Reference”Most Common Latches
Section titled “Most Common Latches”- 177: cache buffers chains
- 183: library cache
- 186: shared pool
- 181: redo allocation
- 198: enqueue hash chains
This simple but essential script provides the foundation for latch-based performance analysis in Oracle databases.