This example shows how you can use a logon trigger to automatically trace all sessions for a given user. This can be useful where an application is launched from the desktop and then fails before the DBA has had time to identify the SID and SERIAL# and start tracing it. First create the user:
SQL> conn system/manager@adhoc
Connected.
SQL> create user andrew identified by reid
2 /
User created.
SQL> grant create session,
2 create trigger,
3 alter session to andrew
4 /
Grant succeeded.
SQL>
Create the logon trigger in the new schema:
SQL> conn andrew/reid@adhoc
Connected.
SQL> create or replace trigger immediate_trace
2 after logon on andrew.schema
3 begin
4 execute immediate 'alter session set sql_trace=true';
5 end;
6 /
Trigger created.
SQL>
Logon as the new user again and run some SQL:
SQL> conn andrew/reid@adhoc
Connected.
SQL> select sysdate from dual
2 /
SYSDATE
---------
01-FEB-10
SQL>
This will produce a trace file ready for further analysis e.g. by tkprof. It will contain, among other statements, the following:
=====================
PARSING IN CURSOR #3 len=64 dep=1 uid=86 oct=47 lid=86 tim=3571197485 hv=815087497 ad='2f4d8670' sqlid='ahb55sss9agw9'
begin
execute immediate 'alter session set sql_trace=true';
end;
END OF STMT
EXEC #3:c=0,e=8020,p=0,cr=0,cu=0,mis=0,r=1,dep=1,og=1,tim=3571197471
=====================
and
=====================
PARSING IN CURSOR #3 len=24 dep=0 uid=86 oct=3 lid=86 tim=3573486632 hv=2343063137 ad='2f47564c' sqlid='7h35uxf5uhmm1'
select sysdate from dual
END OF STMT
PARSE #3:c=0,e=341,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=3573486623
EXEC #3:c=0,e=91,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=1,tim=3573487278
FETCH #3:c=0,e=60,p=0,cr=0,cu=0,mis=0,r=1,dep=0,og=1,tim=3573487570
STAT #3 id=1 cnt=1 pid=0 pos=1 obj=0 op='FAST DUAL (cr=0 pr=0 pw=0 time=0 us cost=2 size=0 card=1)'
FETCH #3:c=0,e=6,p=0,cr=0,cu=0,mis=0,r=0,dep=0,og=0,tim=3573490855
*** 2010-12-21 23:12:46.828
XCTEND rlbk=0, rd_only=1
=====================