Is there any way to get information about current session from gv$session in oracle?

自作多情 提交于 2019-12-22 07:47:06

问题


Is there any way to uniquely identify current session in GV$SESSION view in Oracle?

I've faced with the problem that the following query may return more than one row in case of Oracle RAC configuration:

SELECT SID, SERIAL#
FROM GV$SESSION
WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID')
   AND SID = Sys_Context('USERENV', 'SID');

Using V$MYSTAT is not an option either, because V$MYSTAT may not be accessible for the current session (for example when statistic is disabled).


回答1:


Try this:

SELECT SID, SERIAL#
FROM V$SESSION
WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID');

Since you're interested in current session, the current session must be on the local instance (by definition), so use V$SESSION instead of GV$SESSION. Also, all you need is AUDSID to uniquely identify your session.

If you've got some reason you really need to use GV$SESSION (can't imagine why that would be), you could do this instead:

SELECT SID, SERIAL#
    FROM GV$SESSION
    WHERE AUDSID = Sys_Context('USERENV', 'SESSIONID')
      AND INST_ID = USERENV('Instance');

Also, an alternate way to get the SID of the current session is:

select sid from v$mystat where rownum=1;

Hope that helps.




回答2:


Joining gv$session and v$mystat :)

 SELECT SID, SERIAL#,inst_id
  FROM GV$SESSION
 WHERE sid=(select sid from v$mystat where rownum=1); 



回答3:


Use V$SESSION instead of GV$SESSION

SELECT SID, SERIAL#
FROM V$SESSION
WHERE SID = Sys_Context('USERENV', 'SID');



回答4:


Try this (will need access to v$session and v$sql views) -

select /*My Sess*/ 'My Sess Text = hello world @ '||systimestamp SID_SR_TXT from dual
union
select 'SID = '||sid||' Serial# = '||serial# SID_SR_TXT  from v$session where sql_id in 
(select sql_id from v$sql where sql_text like '% My Sess %');


来源:https://stackoverflow.com/questions/28814543/is-there-any-way-to-get-information-about-current-session-from-gvsession-in-ora

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!