Open Cursors Analysis (vopencur.sql)
What This Script Does
Section titled “What This Script Does”This script displays open cursors in the database, allowing you to:
- Find all open cursors for a specific session
- Search for cursors containing specific SQL text
- Diagnose cursor leak issues
- Monitor cursor usage patterns
The Script
Section titled “The Script”rem vopencur.sqlremttitle 'Open Cursors'remcol sid format 9999 heading 'SID'col sql_text format a60 heading 'SQL TEXT'remselect sid, sql_text from v$open_cursor where upper(sql_text) like upper('&text') and sid like '&sid'/-- Basic usage@vopencur.sql
-- When prompted, enter:-- text: SQL text pattern (use % for wildcard)-- sid: Session ID (use % for all sessions)Parameters
Section titled “Parameters”The script prompts for:
- &text - SQL text pattern to search for (use % for wildcard)
- &sid - Session ID to filter (use % for all sessions)
Required Privileges
Section titled “Required Privileges”SELECT ON V$OPEN_CURSORSample Output
Section titled “Sample Output”Open Cursors
SID SQL TEXT---- ------------------------------------------------------------ 125 SELECT * FROM employees WHERE department_id = :1 125 SELECT COUNT(*) FROM orders WHERE order_date > :1 248 BEGIN dbms_stats.gather_table_stats(:1, :2); END; 248 SELECT /*+ INDEX(e EMP_IDX) */ * FROM employees e 367 INSERT INTO audit_log (user_id, action, timestamp) VALUES (Key Output Columns
Section titled “Key Output Columns”- SID - Session ID holding the open cursor
- SQL TEXT - First 60 characters of the SQL statement
Common Use Cases
Section titled “Common Use Cases”Diagnose Cursor Leaks
-- Find sessions with many open cursors@vopencur.sql-- Enter: % for text-- Enter: 125 for sid (specific session)-- Count results to see cursor usageFind Specific SQL Patterns
-- Look for specific table access@vopencur.sql-- Enter: %EMPLOYEES% for text-- Enter: % for sidMonitor Application Cursors
-- Check cursors from specific application@vopencur.sql-- Enter: %APP_SCHEMA.% for text-- Enter: % for sidCursor Management Best Practices
Section titled “Cursor Management Best Practices”Cursor Limits:
-- Check current cursor limitSHOW PARAMETER open_cursors
-- Check cursor usage statisticsSELECT name, valueFROM v$sysstatWHERE name LIKE '%cursor%';Common Issues:
- Cursor leaks - Application not closing cursors properly
- High parse rates - Not reusing cursors effectively
- ORA-01000 - Maximum open cursors exceeded
Prevention:
- Always close cursors in application code
- Use connection pooling with proper cursor management
- Monitor cursor cache hit ratio
- Set appropriate OPEN_CURSORS parameter
Additional Analysis
Section titled “Additional Analysis”-- Count open cursors per sessionSELECT sid, COUNT(*) cursor_countFROM v$open_cursorGROUP BY sidORDER BY cursor_count DESC;
-- Find sessions approaching cursor limitSELECT s.sid, s.username, s.program, COUNT(*) open_cursors, p.value max_cursorsFROM v$open_cursor oc, v$session s, v$parameter pWHERE oc.sid = s.sidAND p.name = 'open_cursors'GROUP BY s.sid, s.username, s.program, p.valueHAVING COUNT(*) > p.value * 0.8ORDER BY open_cursors DESC;Related Scripts
Section titled “Related Scripts”- Active Sessions - View session details
- SQL by Session ID - Current SQL for sessions
- Top SQL Statements - Resource-intensive SQL