KB · Oracle Database · Script

Who's connected: sessions by service, host and status across RAC (gv$session)

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

When the session count jumps or an app server pool leaks, the first question is where the sessions come from. These four scripts answer it at increasing detail:

ScriptAnswers
ccs.sqlHow many sessions each service has on each instance
con.sqlSessions grouped by machine, service and status, with digits in machine names masked so a pool of app01..app40 shows as one row
hc.sqlFor one host: sessions per instance, grouped by status and SQL
hcs.sqlFor one host: every session, with user, logon time, idle minutes and SQL IDs

Usage

SQL> @ccs.sql %
SQL> @con.sql
SQL> @hc.sql "app45%"
SQL> @hcs.sql app45.example.com

The host argument is a LIKE pattern on gv$session.machine.

ccs.sqlMIT · Download ·
-- ccs.sql: session count per service and instance
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/sessions-by-service-and-host/
--
-- Usage: @ccs.sql <service-like>
--        @ccs.sql %        every service

break on service_name skip page

col service_name format a20

select service_name, inst_id, count(*) cnt
  from gv$session where service_name like '&&1'
group by inst_id, service_name
order by service_name, inst_id;

curl -O https://hashjoin.com/kb/oracle/sessions-by-service-and-host/ccs.sql

con.sqlMIT · Download ·
-- con.sql: sessions by machine (digits masked), service and status
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/sessions-by-service-and-host/
--
-- Usage: @con.sql
--
-- Digits in machine names become X, so app-01..app-40 collapse into one row.

set lines 132
set pages 150

ttit "Breakdown of aggregated Connections"
col machine format a45
col service_name format a30

select count(*)
,      translate(machine,1234567890,'XXXXXXXXXX') machine
,       SERVICE_NAME 
,       status
from gv$session group by translate(machine,1234567890,'XXXXXXXXXX'), SERVICE_NAME, status
order by 2;

curl -O https://hashjoin.com/kb/oracle/sessions-by-service-and-host/con.sql

hc.sqlMIT · Download ·
-- hc.sql: sessions per instance from one host, with the SQL they run
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/sessions-by-service-and-host/
--
-- Usage: @hc.sql <machine-like>
--        @hc.sql "app45%"

set pages 1000
ttit off
clear col
col machine format a45
set lines 132
set trims on

break on inst_id skip 2
compute sum of cnt on inst_id

select count(*) cnt, inst_id, machine, status,
       nvl(SQL_ID,PREV_SQL_ID) SQL_ID,
       decode(SQL_HASH_VALUE,0,PREV_HASH_VALUE,null,PREV_HASH_VALUE) HASH_VALUE,
       nvl(SQL_CHILD_NUMBER,PREV_CHILD_NUMBER) CHILD_NUMBER
  from gv$session
 where machine like '&&1'
 group by inst_id, machine, status,
          nvl(SQL_ID,PREV_SQL_ID),
          decode(SQL_HASH_VALUE,0,PREV_HASH_VALUE,null,PREV_HASH_VALUE),
          nvl(SQL_CHILD_NUMBER,PREV_CHILD_NUMBER)
 order by inst_id;

curl -O https://hashjoin.com/kb/oracle/sessions-by-service-and-host/hc.sql

hcs.sqlMIT · Download ·
-- hcs.sql: every session from one host: user, status, idle minutes, SQL
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/sessions-by-service-and-host/
--
-- Usage: @hcs.sql <machine-like>
--        @hcs.sql app45.example.com

set lines 161
col USERNAME format a20
col service_name format a14 trunc
col sid format 99999
col INST_ID format 9999 heading "Ins#"


select INST_ID, sid, serial#, USERNAME, STATUS,
       to_char(logon_time,'DDth HH24:MI:SS') logon,
       floor(last_call_et/60) last_call,
       SQL_ID,SQL_HASH_VALUE,
       PREV_SQL_ID,PREV_HASH_VALUE,
       service_name
from gv$session
where machine like '&&1'
/

curl -O https://hashjoin.com/kb/oracle/sessions-by-service-and-host/hcs.sql

Reading the output

Start with con.sql. A row with hundreds of INACTIVE sessions from one machine pattern is usually a connection pool sized far above what the app uses. A pool that keeps growing with INACTIVE sessions is usually a leak: the app opens connections and never returns them.

Then use hc.sql on one host from that row. If most of its sessions share the same SQL_ID, that statement is what the pool is busy with (or was busy with, for idle sessions: the script falls back to the previous SQL).

Gotchas

What it reads

gv$session

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.