Finding query from oracle which is blocking session - oracle

I want to find out the query for which any operation is blocked.
From DBA I'm getting input that particular session is blocked but I cannot proceed with investigation further without knowing which query is causing issue.
is there any way to find out exact query causing issue?

Oracle Blocking session is very common. Please check the below query it will help you to get the detailed information.
select l1.inst_id,l1.sid, ' IS BLOCKING ', l2.sid,l1.type,l2.type,l1.lmode,l2.lmode,l2.inst_id
from gv$lock l1, gv$lock l2
where l1.block =1 and l2.request > 0and l1.id1=l2.id1
and l1.id2=l2.id2;

You can use the following query to find out whichs sessions are bloking and what they do:
select s.module,
s.program,
s.machine,
s.osuser,
sql.sql_text
from v$session s,
v$sqlarea sql
where s.sql_id = sql.sql_id
and s.sid in (select blocking_session from v$session)

First get the SQL_ID of the BLOCKING session from this script:
SELECT /*+ RULE */
s.sid,
s.serial#,
p.spid "OS SID",
s.sql_hash_value "HASH VALUE",
s.username "ORA USER",
s.status,
s.osuser "OS USER",
s.machine,
s.terminal,
s.type,
s.program,
s.logon_time,
s.last_call_et,
s.sql_id,
l.id1,
l.id2,
decode(l.block,0,'WAITING',1,'BLOCKING') block,
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 ,
round(l.ctime/60,2) "MIN WAITING",
l.type
FROM v$process p, v$session s, v$Lock l
where p.addr = s.paddr
and s.sid=l.sid
and
(l.id1,l.id2,l.type) in
(SELECT l2.id1, l2.id2, l2.type
FROM V$LOCK l2
WHERE l2.request<>0)
order by l.id1,l.id2,l.block desc;
Then pass this SQL_ID into this script:
select sql_text from v$sqltext
where sql_id=<SQL_ID>;

Related

Fetch sql query with Machine Name

I want to fetch sql query with machine name where sql query has been run and machine name can belong's to any user. Please guide how is it possible to get that by joing tables like DBA_hist_sql or any other table.
I can suggest such variant
select
s.sql_id,
s.sql_text,
d.machine
from
v$sql s,
dba_hist_active_sess_history d
where
d.sql_id = s.sql_id
Maybe there is better variant or more related to your question. I hope it wil be helpful for you.
I let you links on documentation of these views.
DBA_HIST_ACTIVE_SESS_HISTORY
V$SQL
You can join DBA_HIST_ACTIVE_SESS_HISTORY and DBA_HIST_SQLTEXT , as long as the sql has been captured in the workload repository.
In the DBA_HIST_ACTIVE_SESS_HISTORY you have the field MACHINE, where you got the value of SYS_CONTEXT('userenv','host') .
You can join both views by sql_id.
However, the query will not be registered on the workload repository if it's not meaningful. You can modify this behaviour by changing settings of AWR using DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS
An example
select
distinct s.sql_id,
s.sql_text,
d.machine ,
u.username ,
d.program
from
gv$sql s inner join dba_hist_active_sess_history d
on ( d.sql_id = s.sql_id and S.INST_ID = D.INSTANCE_NUMBER )
inner join dba_users u on ( D.USER_ID = U.USER_ID )
where
u.username = '&1'
S.SQL_ID = '&2'
order by D.SAMPLE_TIME desc
You can apply the filter by username or sql_id, or both. keep in mind that the field USERNAME will show you the Oracle user who executed the query, not the operating system user behind that connection.

How to know how much temp table space required for expdp job? [duplicate]

This question already has an answer here:
Oracle 11g how to estimate needed TEMP tablespace?
(1 answer)
Closed 5 years ago.
expdp $DBNAME directory=ar_exp content=data_only dumpfile=${tablename}.dmp logfile=${tablename}.log tables=$tablename query=$SCHNAME.$tablename:'"where substr('$fieldname',1,5) in('$cpidlst')"'
Here this operation takes temp tablespace, but how much temp tablespace will be required how to calculate?
In addition to the link to the estimate answer, here is a script we use to find out how much temp is being used. You have to find the SID of your expdp session and then join it to the WHERE clause.
select
s.sid,
s.serial#,
p.spid "OS SID",
s.sql_hash_value "HASH VALUE",
s.username "ORA USER",
s.status,
s.osuser "OS USER",
s.machine,
s.terminal,
s.type,
s.program,
s.logon_time,
s.last_call_et,
su.contents,
ROUND(((su.blocks*param.value)/1024/1024),2) "MB",
tablespace,
s.sql_id
FROM
v$session s, v$sort_usage su, v$process p, v$parameter param
WHERE
param.name = 'db_block_size' and
su.session_addr = s.saddr and
p.addr = s.paddr
ORDER BY
su.blocks DESC;
Once you find the SID of your session:
Join it to V$SQL_TEXT and get the SQL_TEXT
Find out how to create PLAN_TABLE
Run "explain plan into plan_table for" the SQL_TEXT you found in step 1
Select TEMP_SPACE from PLAN_TABLE;
This should give you an estimate; however I have never found the estimate to be accurate. The best way is to watch the session running, then use script above repeatedly if you have to.

How to get machine name/ip address of a SQL query from which that was run using sql id in ORACLE

I have the sql id of a query. Now I want to get the machine name and IP of the machine from which this query was run.
I have already checked the sql id in V$SESSION and V$ACTIVE_SESSION_HISTORY but didn't get any results.
I am able to find the sql id in v$sql and LAST_LOAD_TIME is today(7 July 2016).
select LAST_LOAD_TIME from v$sql where SQL_ID='0jf4618m2u7aw' order by LAST_LOAD_TIME desc;
2016-07-04/17:26:02
2016-07-04/17:26:02
select * from V$SESSION where SQL_ID='0jf4618m2u7aw';
no rows selected
select * from V$ACTIVE_SESSION_HISTORY where SQL_ID='0jf4618m2u7aw';
no rows selected
Please help. Thanks in advance.
You have a couple of options...
If it is an active session/process, you can use v$session and v$process:
SELECT DISTINCT
MACHINE,
UTL_INADDR.GET_HOST_ADDRESS(MACHINE) AS IP_ADDR
FROM V$SESSION S,
V$PROCESS P
WHERE S.PADDR = P.ADDR
AND S.SQL_ID = '0jf4618m2u7aw';
If you want a historical view, the DBA_HIST_ACTIVE_SESS_HISTORY might be your friend:
SELECT DISTINCT
MACHINE,
UTL_INADDR.GET_HOST_ADDRESS(MACHINE) AS IP_ADDR
FROM DBA_HIST_ACTIVE_SESS_HISTORY DHASH
WHERE DHASH.SQL_ID = '0jf4618m2u7aw'
AND DHASH.SQL_EXEC_START > TRUNC(SYSDATE);

How do I get Oracle, see what procedures are running?

Good afternoon. How do I get Oracle, see what procedures are running?
Depending on your needs, this might suffice (but relies on access to v$session and dba_objects):
select 'CALLED PLSQL', vs.username, d_o.object_name -- whatever info you need
from dba_objects d_o
inner join
v$session vs
on d_o.object_id = vs.plsql_entry_object_id
union all
select 'CURRENT PLSQL', vs.username, d_o.object_name
from dba_objects d_o
inner join
v$session vs
on d_o.object_id = vs.plsql_object_id
As per the docs:
PLSQL_ENTRY_OBJECT_ID - ID of the top-most PL/SQL subprogram on the stack; NULL if there is no PL/SQL subprogram on the stack
PLSQL_OBJECT_ID - Object ID of the currently executing PL/SQL subprogram; NULL if executing SQL
select 'CALLED PLSQL', vs.username, d_o.object_name -- whatever info you need
from dba_objects d_o
inner join
v$session vs
on d_o.object_id = vs.plsql_entry_object_id
union all
select 'CURRENT PLSQL', vs.username, d_o.object_name
from dba_objects d_o
inner join
v$session vs
on d_o.object_id = vs.plsql_object_id

How to identify what locked PL/SQL package (Oracle 10.0.4.2)?

I was trying to recompile PL/SQL package and no avail. because something obtained the lock and that wasn't released for long time. As soon as I kill all sessions I was able to recompile but encounter the same behavior (i.e. locked package) and I wonder what tools are avail to identify what could of obtain it and never release it? This happen on (Oracle 10.2.0.4). Greatly appreciate for your help.
I think you mean 10.2.0.4, as there isn't a 10.0.x.x version series.
select * from v$locked_object lo join dba_objects o on lo.object_id = o.object_id
where o.object_name = 'xxPACKAGE NAMExx' and o.object_type = 'PACKAGE';
select l.session_id, l.owner, l.name, l.type, inst_id, sql_id
, a.sql_fulltext
, 'alter system disconnect session '''||s.sid||','||s.serial#||',#'||inst_id||''' immediate' ddl
from dba_ddl_locks l
join gv$session s on s.sid = l.session_id
join gv$sqlarea a using(inst_id, sql_id)
where l.name = 'OBJECT_NAME'
;

Resources