Skip to Main Content
  • Questions
  • how to find my sessions record in v$session

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Rob .

Asked: May 02, 2000 - 1:21 pm UTC

Last updated: November 07, 2018 - 12:59 am UTC

Version:

Viewed 50K+ times! This question is

You Asked

How do I get the program name at
execution time? It is in v$session,
but that returns many rows, some of them
with null username. This is being used
in a trigger for auditing purposes.
I thought I had it by creating a procedure
in SYS using the following:
Create procedure get_prog_id
(prog_id OUT varchar2)
as
begin
select substr(program, 1, 8) INTO prog_id
from v$session
where user = username;
end;
SYS has to grant execute on this to PUBLIC.
However, I got tripped up today due to an
inactive session (so the select returned
two rows and failed).

Your assistance would be much appreciated.

Thanks

Rob Das



and Tom said...


the query you are looking for is:

select substr(program, 1, 8) INTO prog_id
from v$session
where audsid = SYS_CONTEXT('USERENV','sessionid');


that'll get the row from v$session for the current session.


Rating

  (7 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Confused on difference between SID, SERIAl#, SESSIONID, AUDSID

Joseph Chuan, June 12, 2003 - 3:24 pm UTC

Tom

Thanks for all your valuable insights. I have a follow-up question. After reading many of your responses on SESSIONID, is it correct to state that

- SID combined (somehow) with SERIAL# make up SESSIONID
- SESSIONID = AUDSID (just different name when use for auditing)
- SID and SERIAL# are not unique each can be reused there SESSIONID is also not UNIQUE i.e. two users at a different time can have the same SESSIONID. It is not unique across time in the same database.
- Is there a routine to get a (1) within the database ID that is always UNIQUE (2) across the database, a globally unique number.

Thanks

Tom Kyte
June 12, 2003 - 4:00 pm UTC

sessionid comes from the sequence sys.audses$, it has no relation to sid/serial#

sid and serial# are unique, you won't see two sid/serials at the same time.
sessionid is unique but can wrap around over time.

sys_guid()? does that meet your needs?

Prasad, June 12, 2003 - 7:55 pm UTC

Tom,

you said

sessionid is unique but can wrap around over time.

But what I see is sid is getting generated on a max+1 basis . while sessionid seems sequence generated unique ( I am not sure if it wraps around over time).

This is my session output

pk@fd>@connect pk@ed
Enter password: ***
Connected.
pk@ed>select distinct sid from v$mystat;

SID
----------
12


pk@ed>select sid,audsid from v$session;

SID AUDSID
---------- ----------
1 0
2 0
3 0
4 0
5 0
6 0
10 10892773
12 10892774

8 rows selected.

pk@ed>disco
Disconnected from Oracle9i Enterprise Edition Release 9.0.1.3.0 - 64bit Production
With the Partitioning option
JServer Release 9.0.1.3.0 - Production
pk@ed>@connect pk@ed
Enter password: ***
Connected.
pk@ed>select distinct sid from v$mystat;

SID
----------
12

pk@ed>select sid,audsid from v$session;

SID AUDSID
---------- ----------
1 0
2 0
3 0
4 0
5 0
6 0
10 10892773
12(same as session before) 10892775(Changed)

8 rows selected.

your input please.

Thanks


Tom Kyte
June 12, 2003 - 8:33 pm UTC

in looking at v$session, sid serial are unique. will they be reused? sure. are they unique at a point in time? yes - definitely.

they are not good for a "primary key to a session", nor is "audsid" really cause it can wrap. you would have to generate your "own key" for this.

Using sessionid as UK

Tim, May 09, 2006 - 10:38 pm UTC

I am using the sessionid as a UK as part of my application auditing. So - now that I read your comment - I am concerned about wrapping / duplicates. In the last 12 months - the sessionid in our audit table has gone from about 4,000,000 to about 12,100,000.
The SYS.AUDSES$ (which you mention is where the sessionid comes from) has a max value of 2,000,000,000 so I guess we will not "wrap" (during my tenure) - assuming the last 12 months is somewhat typical of future activity.
However - I am wondering what other ways the sequence could become duplicated. For instance - we are currently on Oracle 9.2.0.4 database server. We are supposed to be moving on to Oracle 10.? sometime next year.
Depending on how the "move" is accomplished - is it reasonable to be concerned about the sessionid on the database being "reset" and not "moving over" at the same currval?

I find the sessionid to be a very convenient UK. I have FK pointing to it - works nice. But I am wondering - if this will be a problem I would (of course) rather deal with it ahead of time rather than having to scurry at the last minute. (Plus I like fixing things before anybody else knows I broke it!!!) Is there a "database id" which would be unique for the database instance that I could store as another column and then make the UK the combination of the sessionid and the databaseid?
Or would you not see that as a good idea or perhaps have a better suggestion?


Tom Kyte
May 10, 2006 - 7:37 am UTC

the sessionid could definitely repeat based on how you "move" to 10g. It is not tied to your application, it is tied to our application (that being the database). create a new database and fill it with your data and sessionid will be whatever we want it to be.


there is db_id - but that too can be duplicated depending on how you do things.

you probably have "time" in there right - the combination of sessionid plus timestamp should be more than unique enough.

A reader, August 18, 2006 - 12:29 am UTC


audsid not unique??

houman, January 25, 2007 - 5:34 pm UTC

Tom,

I have three sessions (all SYS user) that have the same
audsid. Doesn't uniqueness of audsid apply to SYS user sessions?
1* select sid,serial#,audsid,status from v$session where username is not null and terminal is no
SYS@dev:SQL> /

SID SERIAL# AUDSID STATUS
---------- ---------- ---------- --------
119 15763 320250 INACTIVE
120 23406 320262 INACTIVE
121 1164 318277 INACTIVE
124 64202 320174 INACTIVE
126 11401 316806 INACTIVE
127 22526 4294967295 ACTIVE
130 23565 316930 INACTIVE
131 28189 320045 INACTIVE
132 21807 316923 INACTIVE
133 23376 320146 INACTIVE
134 17579 4294967295 INACTIVE
136 47362 320046 INACTIVE
139 32268 4294967295 KILLED
141 12878 316807 ACTIVE
158 10289 314464 INACTIVE

15 rows selected.

Inactive sessions

Mark, July 26, 2007 - 6:33 am UTC

Hello Tom,
Why do I see lot of INACTIVE sessions in V$SESSIONS ?
Tom Kyte
July 27, 2007 - 8:36 am UTC

umm, because they are not active?

you have applications that are connected

but are not currently asking the database to process a sql statement.

userenv depreciated

Terence Rudkin, November 06, 2018 - 1:48 pm UTC

The information is correct but examples use userenv('sessionid');

This usage is depreciated and should be replaced with SYS_CONTEXT('USERENV','sessionid');
Connor McDonald
November 07, 2018 - 12:59 am UTC

Thanks - we've updated the original answer.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library