Once you have a suspect session (from top sessions by I/O, blocking locks or a monitoring alert), these scripts answer the next three questions about it, plus one about its whole application:
| Script | Answers |
|---|---|
gss.sql | Who is this session, and what's the full text of the SQL it's running? (any instance) |
wait.sql | What is it waiting on right now, every column printed one per line |
ssts.sql | What's its value for a statistic, e.g. commits or redo size? |
cwt.sql | What are all sessions with a given client_info waiting on, grouped by event? |
Usage
SQL> @gss.sql 2 1234
SQL> @wait.sql 1234
SQL> @ssts.sql 1234 %redo%
SQL> @cwt.sql BATCH_LOADER
-- gss.sql: current SQL text and identity for one session on RAC
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/drill-into-one-session/
--
-- Usage: @gss.sql <inst_id> <sid>
-- @gss.sql 2 1234
clear col
clear breaks
set lines 80
set trims on
set pages 9000
col sql_text format a70 word_wrapped
col inst_id format a10 noprint new_value n_inst_id
col sid format a10 noprint new_value n_sid
col serial format a10 noprint new_value n_serial
col username format a20 noprint new_value n_username
col machine format a20 noprint new_value n_machine
col osuser format a20 noprint new_value n_osuser
col process format a20 noprint new_value n_process
col action format a45 noprint new_value n_action
break on sid on serial on username on process on machine on action skip page
ttitle -
"Instance...... : " n_inst_id -
skip 1 -
"Sid .......... : " n_sid -
skip 1 -
"Serial ....... : " n_serial -
skip 1 -
"Username ..... : " n_username -
skip 1 -
"Machine ...... : " n_machine -
skip 1 -
"OSuser ....... : " n_osuser -
skip 1 -
"Process ...... : " n_process -
skip 1 -
"Action ....... : " n_action -
select /*+ ORDERED */
ses.inst_id,sid,serial# serial,username,machine,osuser,process,module||' '||action action,sql_text
from gv$session ses, gv$sqltext_with_newlines txt
where txt.address(+) = ses.sql_address
and txt.hash_value(+) = ses.sql_hash_value
and txt.inst_id = ses.inst_id
and ses.inst_id = &&1
and ses.sid = &&2
order by inst_id,sid,piece;curl -O https://hashjoin.com/kb/oracle/drill-into-one-session/gss.sql
-- wait.sql: what one session is waiting on right now, printed one column per line
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/drill-into-one-session/
--
-- Usage: @wait.sql <sid>
--
-- Needs Tom Kyte's print_table procedure (see the page for the link).
set serveroutput on size unlimited
exec print_table('select * from v$session_wait where sid=&&1');curl -O https://hashjoin.com/kb/oracle/drill-into-one-session/wait.sql
-- ssts.sql: session statistics for one SID, filtered by name
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/drill-into-one-session/
--
-- Usage: @ssts.sql <sid> <stat-name-like>
-- @ssts.sql 1234 %commit%
select n.name,s.value
from v$sesstat s
, v$statname n
where s.STATISTIC#=n.STATISTIC#
and name like '&&2'
and sid=&&1
/curl -O https://hashjoin.com/kb/oracle/drill-into-one-session/ssts.sql
-- cwt.sql: what an application's sessions are waiting on, by client_info
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/drill-into-one-session/
--
-- Usage: @cwt.sql <client_info>
set lines 132
set pages 1000
set trims on
col CLIENT_IDENTIFIER format a20
col ACTION format a17
col P1TEXT format a14
col EVENT format a30
select inst_id,event,p1raw,max(seconds_in_Wait) max_wait,trunc(avg(seconds_in_Wait)) avg_wait,count(*),state
from gv$session
where client_info='&&1'
and wait_time=0
group by inst_id,event,p1raw,state
order by inst_id,event;curl -O https://hashjoin.com/kb/oracle/drill-into-one-session/cwt.sql
Before you run wait.sql
wait.sql calls Tom Kyte's print_table procedure, which prints any query's result one column per line. It isn't included here because it's his code: get it from AskTom (search for print_table) and create it in your schema first.
Gotchas
- ORA-06598: insufficient INHERIT PRIVILEGES privilege from
wait.sqlmeansprint_tableis owned by a less privileged user than the one calling it (for example, created in an app schema and run as SYS). It runs with the caller's rights, which Oracle 12c and later won't lend to a lower-privileged owner. Createprint_tablein the schema you run it from. wait.sqlandssts.sqlreadv$views, so on RAC run them on the session's own instance.gss.sqlandcwt.sqlusegv$and work from any instance.- Session statistics are cumulative since logon. Run
ssts.sqltwice a minute apart and subtract to get a rate. cwt.sqlonly shows sessions waiting right now (wait_time = 0); sessions on CPU are left out.
What it reads
gv$session gv$sqltext_with_newlines v$session_wait v$sesstat v$statname
Related
- Who is blocking whom across RAC instances (gv$lock decoded)
- Top 10 sessions by I/O right now (v$sesstat)
- Get the SQL text and execution plan for a SQL_ID (dbms_xplan.display_cursor)
Tired of fixing the same thing twice?
HASHJOIN turns the checks and fixes your on-call runs by hand into tooling that gives the same answer every time. Fixed scope, and you own all of the code.
New fixes and field notes by email, about once a month.