User Session Analysis (vuser.sql)
What This Script Does
Section titled “What This Script Does”This script provides comprehensive analysis of user sessions, combining session information with I/O statistics and process details. It shows database connections, resource usage patterns, and user activity, making it valuable for security monitoring, performance analysis, and session management.
Script
Section titled “Script”rem vuser.sqlremttitle 'User Sessions'remset linesize 100remcol pid format 999 heading 'PID'col spid format a6 heading 'SERVER|PID'col sid format 9999 heading 'SID'col serial# format 99999 heading 'SERIAL'col process format a6 heading 'CLIENT|PID'col osuser format a8 heading 'OS|USERNAME'col username format a10 heading 'ORACLE|USERNAME'col shadow format a10 heading 'SHADOW|USERNAME'col program format a30 heading 'PROGRAM'col logical format b999999999 heading 'LOGICAL|READS'col physical_reads format b9999999 heading 'PHYSICAL|READS'col audsid format b9999999 heading 'AUDIT|SESSION'col logon_time format a14 heading 'LOGON TIME'remselect s.process, p.spid, p.pid, s.sid, s.serial#, s.osuser, s.username, i.block_gets + i.consistent_gets logical, i.physical_reads, s.audsid, to_char(s.logon_time, 'MM/DD HH24:MI:SS') logon_time from v$process p, v$session s, v$sess_io i where i.sid = s.sid and s.paddr = p.addr and ( s.osuser like '&osuser' or s.osuser is null ) and ( s.username like upper('&user') or s.username is null ) order by i.block_gets + i.consistent_gets desc;
-- Run the script in SQL*Plus or SQLcl@vuser.sql
-- When prompted, enter:-- osuser: Operating system username pattern (% for all)-- user: Oracle database username pattern (% for all)
-- ExamplesEnter value for osuser: %Enter value for user: %
-- Or filter specific usersEnter value for osuser: appuserEnter value for user: SALES_USER
Parameters
Section titled “Parameters”- &osuser: Operating system username pattern (use % for wildcard)
- &user: Oracle database username pattern (use % for wildcard)
Required Privileges
Section titled “Required Privileges”- SELECT on V$PROCESS
- SELECT on V$SESSION
- SELECT on V$SESS_IO
- Generally available to most database users
Sample Output
Section titled “Sample Output”User Sessions
CLIENT SERVER PID SID SERIAL OS ORACLE LOGICAL PHYSICAL AUDIT LOGON TIMEPID PID # USERNAME USERNAME READS READS SESSION------ ------ --- ----- ------ -------- ---------- --------- -------- --------- --------------12345 23456 45 145 34567 appuser SALES_USER 45678901 123456 87654321 12/15 09:15:2367890 34567 67 234 45678 dbadmin SYS 2345678 45678 98765432 12/15 08:30:4554321 45678 89 345 56789 webuser WEB_APP 1234567 23456 12345678 12/15 10:22:1798765 56789 123 456 67890 batchjob BATCH_USER 9876543 234567 23456789 12/15 07:45:33
Key Output Columns
Section titled “Key Output Columns”- CLIENT PID: Client process ID (from client machine)
- SERVER PID: Oracle server process ID
- PID: Oracle process number
- SID: Session ID
- SERIAL#: Session serial number
- OS USERNAME: Operating system username
- ORACLE USERNAME: Database username
- LOGICAL READS: Total logical I/O (block gets + consistent gets)
- PHYSICAL READS: Physical disk reads performed
- AUDIT SESSION: Audit session ID
- LOGON TIME: Session login timestamp
Understanding Session Metrics
Section titled “Understanding Session Metrics”Resource Usage Indicators
Section titled “Resource Usage Indicators”- High LOGICAL READS: Sessions performing intensive database operations
- High PHYSICAL READS: Sessions causing disk I/O
- Read Ratios: Physical/Logical ratio indicates buffer cache efficiency
Session Identification
Section titled “Session Identification”- SID + SERIAL#: Unique session identifier for administration
- Process mapping: Links database sessions to OS processes
- User correlation: Maps database users to OS users
Security Monitoring
Section titled “Security Monitoring”- Login patterns: Unusual login times or frequencies
- User activity: Resource usage by user
- Connection sources: Client process identification
Performance Analysis
Section titled “Performance Analysis”High-Resource Sessions
Section titled “High-Resource Sessions”- I/O Intensive users: High LOGICAL or PHYSICAL READS
- Long-running sessions: Old LOGON_TIME with high activity
- System resource impact: Sessions affecting overall performance
Session Efficiency
Section titled “Session Efficiency”- Buffer cache hit ratio: (LOGICAL - PHYSICAL) / LOGICAL * 100
- Read efficiency: Low PHYSICAL READS relative to LOGICAL
- Activity patterns: Resource usage distribution
Common Use Cases
Section titled “Common Use Cases”Security Monitoring
Section titled “Security Monitoring”-- Monitor all active user sessions@vuser.sql-- Enter % for both parameters-- Review for suspicious activity or unauthorized access
Performance Analysis
Section titled “Performance Analysis”-- Identify high-resource users@vuser.sql-- Focus on sessions with high I/O values-- Correlate with application performance issues
Session Management
Section titled “Session Management”-- Find specific user sessions for termination@vuser.sql-- Enter specific username patterns-- Get SID and SERIAL# for ALTER SYSTEM KILL SESSION
Capacity Planning
Section titled “Capacity Planning”-- Analyze user resource consumption patterns@vuser.sql-- Review resource usage by user type-- Plan system capacity requirements
Session Administration
Section titled “Session Administration”Killing Sessions
Section titled “Killing Sessions”-- Use SID and SERIAL# from outputALTER SYSTEM KILL SESSION 'sid,serial#' IMMEDIATE;
-- Example based on outputALTER SYSTEM KILL SESSION '145,34567' IMMEDIATE;
Session Monitoring
Section titled “Session Monitoring”-- Get additional session detailsSELECT sid, username, status, machine, program, sql_id, last_call_et, blocking_sessionFROM v$sessionWHERE sid = &session_sid;
Resource Limits
Section titled “Resource Limits”-- Check session resource usage against limitsSELECT username, resource_name, current_utilization, max_utilization, initial_allocation, limit_valueFROM v$resource_limitWHERE username IS NOT NULL;
Troubleshooting Session Issues
Section titled “Troubleshooting Session Issues”High I/O Sessions
Section titled “High I/O Sessions”-
Identify problematic SQL:
SELECT sid, sql_id, sql_textFROM v$session s, v$sql qWHERE s.sql_address = q.addressAND s.sid = &high_io_sid; -
Check execution plans:
SELECT plan_table_outputFROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('&sql_id'));
Blocked Sessions
Section titled “Blocked Sessions”- Find blocking relationships:
SELECT waiting.sid waiting_sid,blocking.sid blocking_sid,waiting.username waiting_user,blocking.username blocking_userFROM v$session waiting, v$session blockingWHERE waiting.blocking_session = blocking.sid;
Long-Running Sessions
Section titled “Long-Running Sessions”- Identify old sessions:
SELECT sid, username, status,ROUND((SYSDATE - logon_time) * 24, 2) hours_connected,last_call_et seconds_since_last_callFROM v$sessionWHERE logon_time < SYSDATE - 1;
Advanced Analysis
Section titled “Advanced Analysis”Session Resource Summary
Section titled “Session Resource Summary”-- Aggregate resource usage by userSELECT username, COUNT(*) session_count, SUM(i.block_gets + i.consistent_gets) total_logical_reads, SUM(i.physical_reads) total_physical_reads, ROUND(AVG(i.block_gets + i.consistent_gets), 0) avg_logical_readsFROM v$session s, v$sess_io iWHERE s.sid = i.sidAND s.username IS NOT NULLGROUP BY usernameORDER BY total_logical_reads DESC;
Historical Session Analysis
Section titled “Historical Session Analysis”-- Track session patterns over timeSELECT TO_CHAR(logon_time, 'HH24') login_hour, COUNT(*) login_count, COUNT(DISTINCT username) unique_usersFROM v$sessionWHERE logon_time > SYSDATE - 1GROUP BY TO_CHAR(logon_time, 'HH24')ORDER BY login_hour;
Connection Source Analysis
Section titled “Connection Source Analysis”-- Analyze connection sourcesSELECT machine, program, COUNT(*) connection_count, COUNT(DISTINCT username) unique_usersFROM v$sessionWHERE username IS NOT NULLGROUP BY machine, programORDER BY connection_count DESC;
Related Scripts
Section titled “Related Scripts”- All User Sessions (gvuserall.sql) - RAC-wide user session analysis
- Active Session Analysis (../performance-analysis/gvsess.md) - Current activity monitoring
- Session I/O Statistics (../performance-analysis/vsessio.md) - Detailed I/O analysis
- User Account Analysis (duser.sql) - Database user account information