KB · Oracle Database · Script

Drill into one session: current SQL, wait event and statistics

Oracle Database · Script · Verified on Oracle 11.2 and 23ai · Updated 2026-12-03

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:

ScriptAnswers
gss.sqlWho is this session, and what's the full text of the SQL it's running? (any instance)
wait.sqlWhat is it waiting on right now, every column printed one per line
ssts.sqlWhat's its value for a statistic, e.g. commits or redo size?
cwt.sqlWhat 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.sqlMIT · Download ·
-- 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.sqlMIT · Download ·
-- 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.sqlMIT · Download ·
-- 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.sqlMIT · Download ·
-- 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

What it reads

gv$session gv$sqltext_with_newlines v$session_wait v$sesstat v$statname

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.