------%%%% Session Related Queries %%%%------

alter system kill session '
alter system kill session 'sid,serial#' immediate;
select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,PROCESS, to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from gv$session where sid= '388';
col USERNAME for a15
col OSUSER for a13
col SCHEMANAME for a15
col MACHINE for a20
select SID,SERIAL#,USERNAME, MACHINE from gv$session where  MACHINE like 'wawplappp10%';

SELECT spid
FROM v$session a, v$process b WHERE a.paddr = b.addr AND sid = '388';

1) Session details  (status, logon time etc.)
2) Blocking sessions/Wait events etc.
3) SQL text
4) No. Of  rows  processed
5) Any change in the Access  plan
6) Any  long running sessions

select instance_name, name, open_mode from v$instance, v$database;

set lines 900
set pagesize 900

Finding details using username:
*************************************
set lines 900
set pagesize 900
col USERNAME for a15
col OSUSER for a13
col SCHEMANAME for a15
col MACHINE for a20
col LOGON_TIME for a30
col SQL_ID for a15

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from gv$session where 
osuser like '%%' order by LOGON_TIME;
select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from v$session order by SID;
select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from v$session order by STATUS;

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from gv$session where STATUS='ACTIVE';

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from v$session where username= 'SAS_ANALISTA' order by status;

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from gv$session where username='H3G_TRAFFICO' order by LOGON_TIME;

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from gv$session order by LOGON_TIME;

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from v$session where username='EBIS_DWH' and OSUSER = 'dsadmc' order by LOGON_TIME;

select SID,SERIAL#,USERNAME,OSUSER,inst_id,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from v$session order by LOGON_TIME;

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from gv$session where MACHINE='h3fah055' order by LOGON_TIME;
select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char(logon_time, 'hh24:mi:ss dd/mm/yy') login_time from gv$SESSION where OSUSER='dsadmc' and machine='h3fah055' order by LOGIN_TIME;
select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from v$session where SQL_ID='6sbr8wusrcy8a';

Sessions sorted by logon time:-
**********************************
set lines 100 pages 999
col ID  format a15
col osuser format a15
col login_time format a14
select  username
, osuser
, sid || ',' || serial# "ID"
, status
, to_char(logon_time, 'hh24:mi dd/mm/yy') login_time
, last_call_et
from v$session
where username is not null
order by login_time;

Finding details using sid:
*****************************
select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from gv$session where sid= '2191';

select SID,SERIAL#,USERNAME,OSUSER,SCHEMANAME,MACHINE,SQL_ID,STATUS,to_char (LOGON_TIME,'DD-MON-YY:HH24:MI:SS') from v$session where sid in ('1334', '2573', '2664') order by status;

To Find Blocking  Sessions :
*************************************




Query 1:
***********
select s1.username || '@' || s1.machine
   || ' ( SID=' || s1.sid || ', SERIAL#='||s1.serial#||' ) is blocking'
  || s2.username || '@' || s2.machine || ' ( SID=' || s2.sid || ', SERIAL#='||s2.serial#||' ) ' AS blocking_status
    from v$lock l1, v$session s1, v$lock l2, v$session s2
    where s1.sid=l1.sid and s2.sid=l2.sid
    and l1.BLOCK=1 and l2.request > 0
    and l1.id1 = l2.id1
    and l2.id2 = l2.id2;

## output ## BLOCKING_SESSION_STATUS
--------------------------------------------------------------------------------
OPS$GBC@htcfh006 ( SID=2622) is blocking OPS$GBC@htcfh006 ( SID=2561)
OPS$GBC@htcfh006 ( SID=2561) is blocking OPS$GBC@htcfh006 ( SID=2622)

In this example the 2622 session is blocking 2561. ##

Query 2:
***********
select blocking_session,sid,serial#,wait_class,seconds_in_wait from gv$session where blocking_session is not NULL order by blocking_session;

## output ##
BLOCKING_SESSION        SID    SERIAL# WAIT_CLASS           SECONDS_IN_WAIT
---------------- ---------- ---------- -------------------- ---------------
             148        135      61521 Idle                       64

Finding Locks:
*****************

set linesize 180 pagesize 90
break on Kill on username on terminal
column Kill heading 'Kill String' format a18
column res heading 'Resource Type' format 999
column id1 format 9999990
column id2 format 9999990
column lmode heading 'Lock Held' format a20;
column request heading 'Lock Requested' format a20;
column serial# format 99999
column username  format a15  heading "Username"
column terminal heading Term format a11
column tab format a45 heading "Table Name"
column owner format a9
column Address format a18
select /*+RULE*/       nvl(S.USERNAME,'Internal') username,
      nvl(S.TERMINAL,'None') terminal,
      L.SID||','||S.SERIAL# ||'  '|| P.spid Kill,
      U1.NAME||'.'||substr(T1.NAME,1,30) tab,
      decode(L.LMODE,1,'No Lock',
            2,'Row Share',
            3,'Row Exclusive',
            4,'Share',
            5,'Share Row Exclusive',
            6,'Exclusive',null) lmode,
      decode(L.REQUEST,1,'No Lock',
            2,'Row Share',
            3,'Row Exclusive',
            4,'Share',
            5,'Share Row Exclusive',
            6,'Exclusive',null) request
from  V$LOCK L,
      GV$SESSION S,
      V$PROCESS P,
      SYS.USER$ U1,
      SYS.OBJ$ T1
where L.SID = S.SID
and   P.addr=S.paddr
and   T1.OBJ# = decode(L.ID2,0,L.ID1,L.ID2)
and   U1.USER# = T1.OWNER#
and   S.TYPE != 'BACKGROUND'
--and   S.SID= 1243
                          order by 1,2,5;

To find statement running on session:
*************************************************
Query 1 if sid is known:
*****************************
set pages 1000
set lines 132
col SQL_TEXT for a127
select SQL_TEXT from   gv$sqltext
where  hash_value = (select sql_hash_value from v$session where sid =2791)
order by piece;

Query 2 if sql_id is known:
*********************************
select sql_text from gv$sql where sql_id='00p5pgs2cgnnn';

No.Of  Rows  processed for  given Session:
********************************************************
select spid, osuser, s.program from v$process p, v$session s where p.addr=s.paddr and SID=139;

select rows_processed from v$sql where hash_value=  (select sql_hash_value from v$session where paddr = (select addr from v$process where spid='&spid'));


We could see “0” rows processed with the mentioned SID.

SQL> select
   rows_processed
from
   v$sql
where
   hash_value=
  (select
      sql_hash_value
   from
      v$session
   where paddr=
   (select addr from v$process where spid='&spid')
  );   2    3    4    5    6    7    8    9   10   11   12   13
Enter value for spid: 28914
old  12:    (select addr from v$process where spid='&spid')
new  12:    (select addr from v$process where spid='28914')

ROWS_PROCESSED
--------------
             0


Accessplan comparision  (running same/diff ):
**********************************************************
select BEGIN_INTERVAL_TIME, PLAN_HASH_VALUE PHV from DBA_HIST_SQLSTAT s,
     DBA_HIST_SNAPSHOT a where SQL_ID=<sql_id found from v$session>'
  and s.snap_id=a.snap_id
  and s.instance_number=1
  and a.instance_number=1
order by 1;

col BEGIN_INTERVAL_TIME for a35
select BEGIN_INTERVAL_TIME, PLAN_HASH_VALUE PHV from DBA_HIST_SQLSTAT s,
     DBA_HIST_SNAPSHOT a where SQL_ID='adj06zhwr5n39'
  and s.snap_id=a.snap_id
  and s.instance_number=1
  and a.instance_number=1
order by 1;

Long running Sessions:
*******************************
SELECT sid,opname,sofar, totalwork, units,elapsed_seconds,time_remaining FROM v$session_longops WHERE sofar != totalwork;

set lines 100 pages 999
col username format a15
col message format a40
col remaining format 9999
select SID,username
, to_char(start_time, 'hh24:mi:ss dd/mm/yy') started
, time_remaining remaining
, message
from v$session_longops
where time_remaining = 0
order by time_remaining desc;

select SID,SERIAL#,SOFAR,START_TIME,TIME_REMAINING,ELAPSED_SECONDS,MESSAGE from v$session_longops where SQL_ID = '62z84b4rzzscu';
select SID,SERIAL#,SOFAR,START_TIME,TIME_REMAINING,ELAPSED_SECONDS,MESSAGE from v$session_longops where SQL_ID = '62z84b4rzzscu' and SID='1460';

How long the query is runnig and how much completed:-
*************************************************************

set linesize 1000
column username format a15
column target format a29
column OPNAME format a25

SELECT USERNAME,SID,SERIAL#,TARGET,OPNAME,START_TIME,LAST_UPDATE_TIME,ELAPSED_SECONDS,TIME_REMAINING,MESSAGE FROM V$SESSION_LONGOPS WHERE TIME_REMAINING>0;

SELECT USERNAME,SID,SERIAL#,TARGET,OPNAME,START_TIME,LAST_UPDATE_TIME,ELAPSED_SECONDS,TIME_REMAINING,MESSAGE FROM V$SESSION_LONGOPS WHERE TIME_REMAINING>0 and SID='1460';

set linesize 200
column username format a15
column target format a25
column OPNAME format a25
SELECT USERNAME,TARGET,TOTALWORK,SOFAR,OPNAME,ELAPSED_SECONDS,TIME_REMAINING FROM V$SESSION_LONGOPS WHERE TOTALWORK<>SOFAR;

SELECT * FROM (SELECT  USERNAME,OPNAME,SID,SERIAL#,CONTEXT,SOFAR,TOTALWORK,TARGET,ROUND(SOFAR/TOTALWORK*100,2) "% COMPLETE" FROM V$SESSION_LONGOPS) WHERE "% COMPLETE" != 100;

SELECT SID,USERNAME,TARGET,(sofar*100/TOTALWORK) "Percent work",OPNAME,ELAPSED_SECONDS,TIME_REMAINING FROM V$SESSION_LONGOPS WHERE TOTALWORK<>SOFAR and totalwork>0;


SQL to find the Session details and associated statement in excel format:
***********************************************************
SET MARKUP HTML ON ENTMAP ON SPOOL ON PREFORMAT OFF;
spool session_detail.xls
set pagesize 9999
column  username format   a10
column  machine  format   a25
column  type     format   a15
column sql_text format a40
column sid format 99999
column serial# 99999
set line 200
--
-- query metadata
--
select b.sid, b.serial#,a.spid   spid,
to_char(b.LOGON_TIME,'dd/mm/yyyy hh:mi:ss'),
floor(b.last_call_et / 60) "Idle (min)",
b.machine  machine,
b.inst_id  INST_ID,
b.process  process,
b.program,
b.type   type,
b.status  status,
b.username  username,
c.sql_text  sql_text
from v$process a,
v$session b,
v$sql   c
where b.paddr  = a.addr
and b.sql_address = c.address(+)
and b.type<>'BACKGROUND'
order by 2,4
/

Plan Table Output: Checked Xplan:
******************************
select * from table(dbms_xplan.display_cursor('ckg31y8rg6psa','','typical'));


Finding spid:
***********
select spid, osuser, s.program from v$process p, v$session s where p.addr=s.paddr and SID=889;

Sessions sorted by logon time/status:
******************************
set lines 100
set pages 999
col ID format a15
col osuser format a15
col login_time format a14
select username, osuser, sid, serial# "ID", status, to_char(logon_time, 'hh24:mi dd/mm/yy') login_time, last_call_et from v$session where username is not null order by status;

Time since last user activity:
************************
set lines 100 pages 999
select username, floor(last_call_et / 60) "Last Activity (Minutes)", status from v$session where username is not null order by last_call_et;

Show all connected users:
*********************
set lines 100 pages 999
col ID format a15
select username, sid, serial# "ID", status, last_call_et "Last Activity" from v$session where username is not null order by status desc, last_call_et desc;

Show a users current sql:
********************
select sql_text from v$sqlarea where (address, hash_value) in(select sql_address, sql_hash_value from v$session where username like '&username');

Show user info including os pid:
**************************
col username format a15
col osuser format a15
col program format a30
select s.sid, s.serial# "SID/SERIAL", s.username, s.osuser, p.spid "OS PID", s.program from v$session s, v$process p where s.paddr = p.addr order by to_number(p.spid);

Session status associated with the specified os process id:
**********************************************
select s.username, s.sid, s.serial#, p.spid, last_call_et, status from V$SESSION s, V$PROCESS p where s.PADDR = p.ADDR and p.spid='&pid';

Finding OS PID for a specific sid:
****************************
select s.sid, s.username, s.serial#, s.osuser, p.spid "OS PID", s.program from v$session s, v$process p where s.paddr=p.addr and s.sid='691';

Display any long operations:
************************
set lines 300 pages 999
col username format a15
col started format a25
col message format a40
col remaining format 9999
select SID, username, to_char(start_time, 'hh24:mi:ss dd/mm/yy') started, time_remaining remaining, message from v$session_longops
where time_remaining = 0 order by time_remaining desc;

select username, to_char(start_time, 'hh24:mi:ss dd/mm/yy') started, time_remaining remaining, message from v$session_longops where sid = '911';

List open cursors per user:
***********************
set pages 999
select sess.username, sess.sid, sess.serial#, stat.value cursors from v$sesstat stat, v$statname sn, v$session sess
where sess.username is not null and sess.sid = stat.sid
and stat.statistic# = sn.statistic#
and sn.name = 'opened cursors current'
order by value;

Show all connected users:
*********************
set lines 100 pages 999
select username, sid, serial# "ID", status, last_call_et "Last Activity" from v$session where username is not null order by status desc, last_call_et desc;

Finding Locks in DB:
******************
Set lines 200
col OBJECT_NAME for a25
col OBJECT_TYPE for a25

select a.sid,a.serial#,c.object_name,c.object_type
from v$session a,
v$locked_object b,
dba_objects c
where a.sid=b.session_id
and b.object_id=c.object_id and c.object_name='DB_MDS2_EVO_SUMMARY_TMP';
--------------------------------------------------------------------------------------------------------------
set lines 200 pages 1000
col Object_Name for a25
col Lock_Mode for a25

SELECT DISTINCT session_id as "Session Id",
       a.os_user_name as "Os_user",
       b.object_name "Object_Name",
       decode(a.locked_mode,
              2 ,'ROW SHARE MODE',
              3 ,'ROW EXCLUSIVE MODE',
              4 ,'SHARE MODE',
              5 ,'SHARE ROW EXCLUSIVE MODE',
              6 ,'EXCLUSIVE','UNKNOWN')as "Lock_Mode"
from v$locked_object a,
       all_objects b
WHERE a.object_id = b.object_id and b.object_name='REPD_EVENT_DETAIL';
order by b.object_name;
--------------------------------------------------------------------------------------------------------------




To find locks with more details:
**************************
set lines 200
set pagesize 66
break on Kill on sid on  username on terminal
column Kill heading 'Kill String' format a13
column res heading 'Resource Type' format 999
column id1 format 9999990
column id2 format 9999990
column locking heading 'Lock Held/Lock Requested' format a40
column lmode heading 'Lock Held' format a15
column request heading 'Lock Requested' format a15
column serial# format 99999
column username  format a10  heading "Username"
column terminal heading Term format a8
column tab format a20 heading "Table Name"
column owner format a9
column LAddr heading "ID1 - ID2" format a18
column Lockt heading "Lock Type" format a34
column command format a25
column sid format 9999

select
nvl(S.USERNAME,'Internal') username,
        L.SID,
        nvl(S.TERMINAL,'None') terminal,
        decode(command,
0,'None',decode(l.id2,0,U1.NAME||'.'||substr(T1.NAME,1,20),'None')) tab,
decode(command,
0,'BACKGROUND',
1,'Create Table',
2,'INSERT',
3,'SELECT',
4,'CREATE CLUSTER',
5,'ALTER CLUSTER',
6,'UPDATE',
7,'DELETE',
8,'DROP',
9,'CREATE INDEX',
10,'DROP INDEX',
11,'ALTER INDEX',
12,'DROP TABLE',
13,'CREATE SEQUENCE',
14,'ALTER SEQUENCE',
15,'ALTER TABLE',
16,'DROP SEQUENCE',
17,'GRANT',
18,'REVOKE',
19,'CREATE SYNONYM',
20,'DROP SYNONYM',
21,'CREATE VIEW',
22,'DROP VIEW',
23,'VALIDATE INDEX',
24,'CREATE PROCEDURE',
25,'ALTER PROCEDURE',
26,'LOCK TABLE',
27,'NO OPERATION',
28,'RENAME',
29,'COMMENT',
30,'AUDIT',
31,'NOAUDIT',
32,'CREATE EXTERNAL DATABASE',
33,'DROP EXTERNAL DATABASE',
34,'CREATE DATABASE',
35,'ALTER DATABASE',
36,'CREATE ROLLBACK SEGMENT',
37,'ALTER ROLLBACK SEGMENT',
38,'DROP ROLLBACK SEGMENT',
39,'CREATE TABLESPACE',
40,'ALTER TABLESPACE',
41,'DROP TABLESPACE',
42,'ALTER SESSION',
43,'ALTER USER',
44,'COMMIT',
45,'ROLLBACK',
46,'SAVEPOINT',
47,'PL/SQL EXECUTE',
48,'SET TRANSACTION',
49,'ALTER SYSTEM SWITCH LOG',
50,'EXPLAIN',
51,'CREATE USER',
52,'CREATE ROLE',
53,'DROP USER',
54,'DROP ROLE',
55,'SET ROLE',
56,'CREATE SCHEMA',
57,'CREATE CONTROL FILE',
58,'ALTER TRACING',
59,'CREATE TRIGGER',
60,'ALTER TRIGGER',
61,'DROP TRIGGER',
62,'ANALYZE TABLE',
63,'ANALYZE INDEX',
64,'ANALYZE CLUSTER',
65,'CREATE PROFILE',
66,'DROP PROFILE',
67,'ALTER PROFILE',
68,'DROP PROCEDURE',
69,'DROP PROCEDURE',
70,'ALTER RESOURCE COST',
71,'CREATE SNAPSHOT LOG',
72,'ALTER SNAPSHOT LOG',
73,'DROP SNAPSHOT LOG',
74,'CREATE SNAPSHOT',
75,'ALTER SNAPSHOT',
76,'DROP SNAPSHOT',
79,'ALTER ROLE',
85,'TRUNCATE TABLE',
86,'TRUNCATE CLUSTER',
87,'-',
88,'ALTER VIEW',
89,'-',
90,'-',
91,'CREATE FUNCTION',
92,'ALTER FUNCTION',
93,'DROP FUNCTION',
94,'CREATE PACKAGE',
95,'ALTER PACKAGE',
96,'DROP PACKAGE',
97,'CREATE PACKAGE BODY',
98,'ALTER PACKAGE BODY',
99,'DROP PACKAGE BODY',
command||' - ???') COMMAND,
        decode(L.LMODE,1,'No Lock',
                2,'Row Share',
                3,'Row Exclusive',
                4,'Share',
                5,'Share Row Exclusive',
                6,'Exclusive','NONE') lmode,
        decode(L.REQUEST,1,'No Lock',
                2,'Row Share',
                3,'Row Exclusive',
                4,'Share',
                5,'Share Row Exclusive',
                6,'Exclusive','NONE') request,
l.id1||'-'||l.id2 Laddr,
l.type||' - '||
decode(l.type,
'BL','Buffer hash table instance lock',
'CF',' Control file schema global enqueue lock',
'CI','Cross-instance function invocation instance lock',
'CS','Control file schema global enqueue lock',
'CU','Cursor bind lock',
'DF','Data file instance lock',
'DL','Direct loader parallel index create',
'DM','Mount/startup db primary/secondary instance lock',
'DR','Distributed recovery process lock',
'DX','Distributed transaction entry lock',
'FI','SGA open-file information lock',
'FS','File set lock',
'HW','Space management operations on a specific segment lock',
'IN','Instance number lock',
'IR','Instance recovery serialization global enqueue lock',
'IS','Instance state lock',
'IV','Library cache invalidation instance lock',
'JQ','Job queue lock',
'KK','Thread kick lock',
'MB','Master buffer hash table instance lock',
'MM','Mount definition gloabal enqueue lock',
'MR','Media recovery lock',
'PF','Password file lock',
'PI','Parallel operation lock',
'PR','Process startup lock',
'PS','Parallel operation lock',
'RE','USE_ROW_ENQUEUE enforcement lock',
'RT','Redo thread global enqueue lock',
'RW','Row wait enqueue lock',
'SC','System commit number instance lock',
'SH','System commit number high water mark enqueue lock',
'SM','SMON lock',
'SN','Sequence number instance lock',
'SQ','Sequence number enqueue lock',
'SS','Sort segment lock',
'ST','Space transaction enqueue lock',
'SV','Sequence number value lock',
'TA','Generic enqueue lock',
'TD','DDL enqueue lock',
'TE','Extend-segment enqueue lock',
'TM','DML enqueue lock',
'TO','Temporary Table Object Enqueue',
'TT','Temporary table enqueue lock',
'TX','Transaction enqueue lock',
'UL','User supplied lock',
'UN','User name lock',
'US','Undo segment DDL lock',
'WL','Being-written redo log instance lock',
'WS','Write-atomic-log-switch global enqueue lock',
'TS',decode(l.id2,0,'Temporary segment enqueue lock (ID2=0)',
                    'New block allocation enqueue lock (ID2=1)'),
'LA','Library cache lock instance lock (A=namespace)',
'LB','Library cache lock instance lock (B=namespace)',
'LC','Library cache lock instance lock (C=namespace)',
'LD','Library cache lock instance lock (D=namespace)',
'LE','Library cache lock instance lock (E=namespace)',
'LF','Library cache lock instance lock (F=namespace)',
'LG','Library cache lock instance lock (G=namespace)',
'LH','Library cache lock instance lock (H=namespace)',
'LI','Library cache lock instance lock (I=namespace)',
'LJ','Library cache lock instance lock (J=namespace)',
'LK','Library cache lock instance lock (K=namespace)',
'LL','Library cache lock instance lock (L=namespace)',
'LM','Library cache lock instance lock (M=namespace)',
'LN','Library cache lock instance lock (N=namespace)',
'LO','Library cache lock instance lock (O=namespace)',
'LP','Library cache lock instance lock (P=namespace)',
'LS','Log start/log switch enqueue lock',
'PA','Library cache pin instance lock (A=namespace)',
'PB','Library cache pin instance lock (B=namespace)',
'PC','Library cache pin instance lock (C=namespace)',
'PD','Library cache pin instance lock (D=namespace)',
'PE','Library cache pin instance lock (E=namespace)',
'PF','Library cache pin instance lock (F=namespace)',
'PG','Library cache pin instance lock (G=namespace)',
'PH','Library cache pin instance lock (H=namespace)',
'PI','Library cache pin instance lock (I=namespace)',
'PJ','Library cache pin instance lock (J=namespace)',
'PL','Library cache pin instance lock (K=namespace)',
'PK','Library cache pin instance lock (L=namespace)',
'PM','Library cache pin instance lock (M=namespace)',
'PN','Library cache pin instance lock (N=namespace)',
'PO','Library cache pin instance lock (O=namespace)',
'PP','Library cache pin instance lock (P=namespace)',
'PQ','Library cache pin instance lock (Q=namespace)',
'PR','Library cache pin instance lock (R=namespace)',
'PS','Library cache pin instance lock (S=namespace)',
'PT','Library cache pin instance lock (T=namespace)',
'PU','Library cache pin instance lock (U=namespace)',
'PV','Library cache pin instance lock (V=namespace)',
'PW','Library cache pin instance lock (W=namespace)',
'PX','Library cache pin instance lock (X=namespace)',
'PY','Library cache pin instance lock (Y=namespace)',
'PZ','Library cache pin instance lock (Z=namespace)',
'QA','Row cache instance lock (A=cache)',
'QB','Row cache instance lock (B=cache)',
'QC','Row cache instance lock (C=cache)',
'QD','Row cache instance lock (D=cache)',
'QE','Row cache instance lock (E=cache)',
'QF','Row cache instance lock (F=cache)',
'QG','Row cache instance lock (G=cache)',
'QH','Row cache instance lock (H=cache)',
'QI','Row cache instance lock (I=cache)',
'QJ','Row cache instance lock (J=cache)',
'QL','Row cache instance lock (K=cache)',
'QK','Row cache instance lock (L=cache)',
'QM','Row cache instance lock (M=cache)',
'QN','Row cache instance lock (N=cache)',
'QO','Row cache instance lock (O=cache)',
'QP','Row cache instance lock (P=cache)',
'QQ','Row cache instance lock (Q=cache)',
'QR','Row cache instance lock (R=cache)',
'QS','Row cache instance lock (S=cache)',
'QT','Row cache instance lock (T=cache)',
'QU','Row cache instance lock (U=cache)',
'QV','Row cache instance lock (V=cache)',
'QW','Row cache instance lock (W=cache)',
'QX','Row cache instance lock (X=cache)',
'QY','Row cache instance lock (Y=cache)',
'QZ','Row cache instance lock (Z=cache)','????') Lockt
from    V$LOCK L,
        GV$SESSION S,
        SYS.USER$ U1,
        SYS.OBJ$ T1
where   L.SID = S.SID
and     T1.OBJ#  = decode(L.ID2,0,L.ID1,1)
and     U1.USER# = T1.OWNER#
and     S.TYPE != 'BACKGROUND'
order by 1,2,5;

Finding All active sql:
*******************
set feedback offset
server output on size 9999
column username format a20
column sql_text format a55 word_wrapped
begin
for x in
(select username'('sid','serial#') ospid = ' process ' program = ' program username,
to_char(LOGON_TIME,' Day HH24:MI') logon_time,
to_char(sysdate,' Day HH24:MI') current_time,
sql_address,
sql_hash_value from v$session
where status = 'ACTIVE' and rawtohex(sql_address) <> '00'
and username is not null ) loop
for y in
(select sql_text from v$sqlarea where address = x.sql_address ) loop
if ( y.sql_text not like '%listener.get_cmd%' and
y.sql_text not like '%RAWTOHEX(SQL_ADDRESS)%' )
then dbms_output.put_line( '--------------------' );
dbms_output.put_line( x.username );
dbms_output.put_line( x.logon_time ' ' x.current_time ' SQL#=' x.sql_hash_value); dbms_output.put_line( substr( y.sql_text, 1, 250 ) );
end if;
end loop;
end loop;
end;
/

Blocking Tables:
*************
select (select username || ' - ' || osuser from v$session where sid=a.sid) blocker, a.sid || ', ' || (select serial# from v$session where sid=a.sid) sid_serial, ' is blocking ', (select username || ' - ' || osuser from v$session where sid=b.sid) blockee, b.sid || ', ' || (select serial# from v$session where sid=b.sid) sid_serial from v$lock a, v$lock b where a.block = 1 and b.request > 0 and a.id1 = b.id1 and a.id2 = b.id2;

select * from dba_dml_locks where name='FRULLO.INTERF_OPZIONI';

select c.owner, c.object_name, c.object_type, b.sid, b.serial#, b.status,b.osuser,b.machine from v$locked_object a, v$session b, dba_objects c where b.sid = a.session_id and a.object_id = c.object_id;

Identify the session which is locking the object:-
***************************************
select OWNER, TABLE_NAME from dba_tables where table_name = 'INTERF_OPZIONI';

SELECT owner,
segment_name,
segment_type,
tablespace_name,
bytes/1048576 MB,
initial_extent,
next_extent,
extents,
pct_increase
FROM
DBA_SEGMENTS
WHERE
OWNER = 'FRULLO' AND
SEGMENT_NAME = 'INTERF_OPZIONI' AND
SEGMENT_TYPE = 'TABLE'
/


select a.sid, a.serial#
from v$session a, v$locked_object b, dba_objects c
where b.object_id = c.object_id
and a.sid = b.session_id
and OBJECT_NAME='FRULLO.INTERF_OPZIONI';

To identify the locked rows, use the below query:-
****************************************

set lines 200
col object_name for a30
select do.object_name
, row_wait_obj#
, row_wait_file#
, row_wait_block#
, row_wait_row#
, dbms_rowid.rowid_create (1, ROW_WAIT_OBJ#, ROW_WAIT_FILE#,
                ROW_WAIT_BLOCK#, ROW_WAIT_ROW#) ROW_ID
from    v$session s
,   dba_objects do
where   s.ROW_WAIT_OBJ# = do.OBJECT_ID
and do.object_name=upper('<object_name_from_above_query>');

O/P:

OBJECT_NAME ROW_WAIT_OBJ#   ROW_WAIT_FILE#  ROW_WAIT_BLOCK# ROW_WAIT_ROW#   ROW_ID
----------- -------------   --------------  --------------- -------------   ------
LOCK_TEST   41862       4       70      0       AAAKOGAAEAAAABGAAA

*******************************************************
SELECT a.sid,a.serial#, a.username,
s.sql_text
FROM V$SESSION a, V$SQLTEXT s
WHERE a.sql_address = s.address
AND a.sql_hash_value = s.hash_value
AND a.username like '&user'
ORDER BY a.username,a.sid,s.piece;

******************************************************
SQL> select resource_name, current_utilization, max_utilization from v$resource_limit where resource_name in ('processes','sessions');

Show a users current sql:-
****************************
Select sql_text
from   v$sqlarea
where  (address, hash_value) in
(select sql_address, sql_hash_value
        from v$session
        where username like '&username');

Show user info including os pid:-
***************************
col "SID/SERIAL" format a10
col username format a15
col osuser format a15
col program format a40
select s.sid || ',' || s.serial# "SID/SERIAL"
, s.username
, s.osuser
, p.spid "OS PID"
, s.program
from v$session s
, v$process p
Where s.paddr = p.addr
order  by to_number(p.spid);


SELECT a.object,
       a.type,
       a.sid,
       b.username,
       b.osuser,
       b.program
FROM   v$access a,
       v$session b
WHERE  a.sid   = b.sid
AND    a.owner = UPPER('&ENTER_SCHEMA_NAME')
AND    a.sid = &enter_session_id
ORDER BY a.object
/



SELECT a.object,
       a.type,
       a.sid,
       b.username,
       b.osuser,
       b.program
FROM   v$access a,
       v$session b
WHERE  a.sid   = b.sid
AND    a.owner = UPPER('H3G_TRAFFICO')
AND    a.sid in ('544','329','307','169','447','505','546','564','583','606','624','85','65','48','1','21')
ORDER BY a.object
/

-- Finding details using username:

SELECT sid,a.SQL_TEXT FROM v$session s, v$sqlarea a WHERE s.sql_address = a.ADDRESS AND s.sql_hash_value = a.hash_value AND s.username like 'C2%';


-- Finding Blocking sessions with v$session
SELECT s.blocking_session,s.sid,s.serial#,s.seconds_in_wait FROM v$session s WHERE blocking_session IS NOT NULL;


-- find blocking sessions using v$lock
SELECT 
   l1.sid || ' is blocking ' || l2.sid blocking_sessions
FROM 
   v$lock l1, v$lock l2
WHERE
   l1.block = 1 AND
   l2.request > 0 AND
   l1.id1 = l2.id1 AND
   l1.id2 = l2.id2;
   

--blocking sessions with all available information (including machine name) 
SELECT s1.username || '@' || s1.machine
    || ' ( SID=' || s1.sid || ' )  is blocking '
    || s2.username || '@' || s2.machine || ' ( SID=' || s2.sid || ' ) ' AS blocking_status
    FROM v$lock l1, v$session s1, v$lock l2, v$session s2
    WHERE s1.sid=l1.sid AND s2.sid=l2.sid
    AND l1.BLOCK=1 AND l2.request > 0
    AND l1.id1 = l2.id1
    AND l1.id2 = l2.id2;


-- identifying blocked objects(when TM lock condition)
SELECT sid, id1 FROM v$lock WHERE TYPE='TM';    

SELECT object_name,object_type FROM dba_objects WHERE object_id=&1; (provide value ID1 from abov