/
install_sesspack_packages.sql
716 lines (602 loc) · 31.5 KB
/
install_sesspack_packages.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
-- Copyright 2018 Tanel Poder. All rights reserved. More info at http://tanelpoder.com
-- Licensed under the Apache License, Version 2.0. See LICENSE.txt for terms & conditions.
--------------------------------------------------------------------------------
--
-- Author: Tanel Poder
-- Copyright: (c) http://www.tanelpoder.com
--
-- Notes: This software is provided AS IS and doesn't guarantee anything
-- Proofread before you execute it!
--
--------------------------------------------------------------------------------
create or replace package sesspack
authid definer -- using definer authid so executing users don't have to have explicit grants on
-- schema and V$ objects
-- authid current_user -- safer option, allows use of privileges granted through roles
is
function in_list( p_sql in varchar2 ) return sawr$SIDlist;
function valid_stat_modes return sawr$ModeList;
procedure snap_sidlist_internal( p_sidlist in sawr$SIDlist, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
procedure snap_sidlist_internal( p_sql in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap current session
procedure snap_me ( p_session_stats in varchar2 default 'ALL', p_snapid in number default null );
-- snap session with given SID
procedure snap_sid ( p_sid in number, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
procedure snap_sid ( p_sid in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap all sessions by oracle user
procedure snap_orauser( p_username in varchar2 default user, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap all sessions by os user
procedure snap_osuser( p_username in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap all sessions by program name (v$session.program)
procedure snap_program( p_program in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap all sessions by terminal (v$session.terminal)
procedure snap_terminal( p_terminal in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap all sessions by machine (v$session.machine)
procedure snap_machine( p_machine in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap the session being served by SPID (v$process.spid)
procedure snap_spid( p_spid in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
procedure snap_spid( p_spid in number, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap all sessions by client PID (v$session.process)
procedure snap_cpid( p_cpid in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
procedure snap_cpid( p_cpid in number, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap all sessions
procedure snap_all ( p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap background sessions
procedure snap_bg ( p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- snap user sessions
procedure snap_fg ( p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null );
-- purge repository data
procedure purge_data ( p_days_threshold in number default 7, p_snapid in number default null );
end sesspack;
/
create or replace package body sesspack as
--==================================================================================================================
--==================================================================================================================
--
-- Global Variables
--
--==================================================================================================================
--==================================================================================================================
--------------------------------------------------------------------------------------------------------------------
-- g_snap_mode is inserted into sawr$snapshots table during first call to assign_snapid()
-- the global variable is changed in the beginning of each external procedure which can be
-- called directly. this means the directly-called procedure's g_snap_mode will be recorded
-- to sawr$snapshots table and its values will be ignored from there on
--------------------------------------------------------------------------------------------------------------------
g_snap_mode varchar2(100) := 'Undefined';
--==================================================================================================================
--==================================================================================================================
--
-- Internal Procs used by sesspack itself
--
--==================================================================================================================
--==================================================================================================================
--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: assign_snapid
-- PURPOSE: generate new snapid if the the parent snap function is executed standalone.
-- if its executed from proper top-level function, a snapid to use should have been
-- passed down from there.
-- the reason for that is that we might need to run several different snap commands
-- for getting a snapshot (e.g. get all sessions where USERNAME = 'SYS' and all with
-- MODULE='HR')
--------------------------------------------------------------------------------------------------------------------
function assign_snapid (p_snapid in number, l_snap_comment varchar2 default '' ) return number
as
l_snapid number;
begin
if (p_snapid is null) then
-- insert a line for each distinct snapshot. primary method of entry for querying
-- snapshot data
--
-- TODO: update the snapshot row with snapshot end sysdate too
-- or put the number of milliseconds spent taking the snapshot
select sawr$snapid_seq.nextval into l_snapid from dual;
insert into sawr$snapshots values ( l_snapid, sysdate, user, g_snap_mode, l_snap_comment );
return l_snapid;
else
return p_snapid;
end if;
end assign_snapid;
--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: in_list
-- PURPOSE: generic function for returning a collection of numbers from the dynamic query
-- passed in p_sql parameter.
-- for example, it's used for getting list of SIDs based on dynamic query
-- against v$session
-- PARAMETERS:
-- p_sql : SQL text string which should return a list of session IDs (in number format)
-- : no restriction where and how the list of SIDs is retrieved, such SIDs should
-- : just exist in V$SESSION, otherwise nothing is sampled
-- : for example, value can be 'select sid from v$session where username = '''APPS''''
-- : or 'select sid from v$session where status = '''ACTIVE''' and last_call_et > 5'
--------------------------------------------------------------------------------------------------------------------
function in_list( p_sql in varchar2 ) return sawr$SIDlist
as
type rc is ref cursor;
l_cursor rc;
l_tmp number;
l_data sawr$sidlist := sawr$SIDlist();
begin
open l_cursor for p_sql;
loop
fetch l_cursor into l_tmp;
exit when l_cursor%notfound;
l_data.extend;
l_data(l_data.count) := l_tmp;
end loop;
close l_cursor;
return l_data;
end in_list; -- ( p_sql in varchar2 )
--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: valid_stat_modes
-- PURPOSE: Function for returning a collection of valid snap modes as determined by
-- the sawr$session_stat_mode table.
--------------------------------------------------------------------------------------------------------------------
function valid_stat_modes return sawr$ModeList is
l_modes sawr$ModeList;
begin
select distinct upper(mode_id) bulk collect into l_modes
from sawr$session_stat_mode;
return l_modes;
end valid_stat_modes;
--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: snap_sidlist_internal (overloaded, p_sidlist as sawr$SIDlist type)
--
-- PURPOSE: this is the low-level procedure doing the actual sampling from V$ views
-- and inserting the result to SAWR$ tables
--
-- PARAMETERS:
-- p_sidlist : sawr$SIDlist collection, this is array of SIDs to be sampled from V$SESSION
-- p_snapid : snapshot ID to be inserted into SAWR$ tables. normally this comes from
-- parent functions, but is autogenerated when the low-level function is
-- executed manually and by leaving p_snapid as NULL.
--------------------------------------------------------------------------------------------------------------------
procedure snap_sidlist_internal( p_sidlist in sawr$SIDlist, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null)
as
l_snapid number;
l_codeloc varchar2(200) := 'SNAP_SIDLIST_INTERNAL(P_SIDLIST): BEGIN';
begin
-- this variable is here for easier catching of exception sources in pre-10g dbs
l_codeloc:= 'SNAP_SIDLIST_INTERNAL(P_SIDLIST): CALL ASSIGN_SNAPID';
-- allocate a new snapid if a global one hasn't been passed down from caller
-- new snapid allocation inserts a line to SAWR$SNAPSHOTS table
l_snapid := assign_snapid(p_snapid);
------------------------------------------------------------------------------------------------------------
-- insert sessions matching parameter conditions into SAWR$SESSIONS table
------------------------------------------------------------------------------------------------------------
l_codeloc := 'SNAP_SIDLIST_INTERNAL(P_SIDLIST): INSERT INTO SAWR$SESSIONS';
insert into
sawr$sessions (
snapid
, snaptime
, program
, username
, machine
, osuser
, terminal
, module
, action
, audsid
, sid
, serial#
, process
, logon_time
)
select
l_snapid
, sysdate as snaptime
, nvl(s.program , '-')
, nvl(s.username , '-')
, nvl(s.machine , '-')
, nvl(s.osuser , '-')
, nvl(s.terminal , '-')
, nvl(module , '-')
, nvl(action , '-')
, s.audsid
, s.sid
, s.serial#
, s.process
, s.logon_time
from
v$session s
where
sid in (
select * from ( table(cast(p_sidlist as sawr$SIDlist)) )
);
------------------------------------------------------------------------------------------------------------
-- insert matching session wait events into SAWR$SESSION_EVENTS table
------------------------------------------------------------------------------------------------------------
l_codeloc := 'SNAP_SIDLIST_INTERNAL(P_SIDLIST): INSERT INTO SAWR$SESSION_EVENTS';
insert into
sawr$session_events (
snapid
, snaptime
, audsid
, sid
, serial#
, event#
, total_waits
, total_timeouts
, average_wait
, max_wait
, time_waited_micro
)
select --+ ORDERED tanel9
l_snapid,
sysdate as snaptime,
s.audsid,
e.sid,
s.serial#,
en.event#,
e.total_waits,
e.total_timeouts,
e.average_wait,
e.max_wait,
-- e.time_waited_micro + ( decode(e.event||w.state, w.event||'WAITING', w.seconds_in_wait, 0) * 1000000 ) time_waited_micro
e.time_waited_micro + NVL( CASE e.event||w.state
WHEN w.event||'WAITING' THEN
CASE
WHEN w.event IN ( select event from v$system_event where total_timeouts != 0 ) THEN 0
ELSE w.seconds_in_wait
END
ELSE 0
END
* 1000000, 0 ) time_waited_micro
from
v$session s,
v$session_event e,
v$session_wait w,
v$event_name en
where
e.sid = s.sid
and s.sid = w.sid
and w.sid = e.sid
and e.event = en.name
and s.sid in (
select * from ( table(cast(p_sidlist as sawr$SIDlist)) )
);
------------------------------------------------------------------------------------------------------------
-- insert used CPU time to session events table as well
-- in 9i V$SESSTAT (CPU used by this session) is used
-- from 10g V$SESS_TIME_MODEL is used as this is more accurate and is updated every 5 seconds
-- even during database CALL
--
-- note that the installer script automatically comments out the irrelevant part depending on db version
------------------------------------------------------------------------------------------------------------
-- the line below is substituted by "/*" by sqlplus during installation onto 9i database
&version_9_enable
-- 9i version for getting session CPU usage
insert into
sawr$session_events (
snapid
, snaptime
, audsid
, sid
, serial#
, event#
, total_waits
, time_waited_micro
)
select --+ ORDERED USE_NL(s st)
l_snapid,
sysdate as snaptime,
s.audsid,
s.sid,
s.serial#,
-1, -- naming CPU usage as event# -1
1, -- setting total waits for CPU to 1 for now (this can be got from perhaps number of calls or sum(events) later on)
st.value * 10000 -- x10000 makes microseconds out of centiseconds
from
v$session s,
v$sesstat st
where
st.statistic# = (select statistic# from v$statname where name = 'CPU used by this session')
and s.sid = st.sid
and s.sid in (
select * from ( table(cast(p_sidlist as sawr$SIDlist)) )
);
-- end: version_9_enable
-- */
-- the line below is substituted by "/*" by sqlplus during installation onto 10g and above database
&version_10_enable
-- 10g+ version for getting session CPU usage
insert into
sawr$session_events (
snapid
, snaptime
, audsid
, sid
, serial#
, event#
, total_waits
, time_waited_micro
)
select --+ ORDERED USE_NL(s st)
l_snapid,
sysdate as snaptime,
s.audsid,
s.sid,
s.serial#,
-1, -- naming CPU usage as event# -1
1, -- setting total waits for CPU to 1 for now (this can be got from perhaps number of calls or sum(events) later on)
st.value -- v$sess_time_model reports times in microseconds
from
v$session s,
v$sess_time_model st
where
st.stat_name = 'DB CPU'
and s.sid = st.sid
and s.sid in (
select * from ( table(cast(p_sidlist as sawr$SIDlist)) )
);
-- end: version_10_enable
-- */
------------------------------------------------------------------------------------------------------------
-- insert matching session statistics into SAWR$SESSION_STATS table
------------------------------------------------------------------------------------------------------------
l_codeloc := 'SNAP_SIDLIST_INTERNAL(P_SIDLIST): INSERT INTO SAWR$SESSION_STATS';
insert into
sawr$session_stats (
snapid
, snaptime
, audsid
, sid
, serial#
, statistic#
, value
)
select --+ ORDERED USE_NL(s ss) INDEX(s) tanel2
l_snapid,
sysdate as snaptime,
s.audsid,
s.sid,
s.serial#,
ss.statistic#,
ss.value
from
v$session s,
v$sesstat ss
where
s.sid = ss.sid
and s.sid in (
select * from ( table(cast(p_sidlist as sawr$SIDlist)) )
)
and ss.statistic# in (
select --+ ORDERED NO_UNNEST
statistic#
from
sawr$session_stat_mode cfg,
v$statname sn
where
sn.name = cfg.statistic_name
and cfg.mode_id = p_session_stats
)
and ss.value != 0;
l_codeloc := 'SNAP_SIDLIST_INTERNAL(P_SIDLIST): END';
exception
when NO_DATA_FOUND then null; -- its ok to find no matches for snapshot query
when others then raise_application_error(-20001, 'Error '||SQLCODE||': '||SQLERRM||' : FAILED AT '|| l_codeloc) ;
end snap_sidlist_internal; -- ( p_sidlist in sawr$SIDlist )
--------------------------------------------------------------------------------------------------------------------
-- FUNCTION: snap_sidlist_internal (overloaded, p_sidlist as VARCHAR2 type)
--
-- PURPOSE: this is a procedure accepting any SQL which returns array of
-- SIDs (NUMBER format) which are then used for calling the
-- snap_sidlist_internal sister function to extract session info
-- from V$SESSION
--
-- PARAMETERS:
-- p_sidlist : sawr$SIDlist collection, this is array of SIDs to be sampled from V$SESSION
-- p_snapid : snapshot ID to be inserted into SAWR$ tables. normally this comes from
-- parent functions, but is autogenerated when the low-level function is
-- executed manually and by leaving p_snapid as NULL.
--------------------------------------------------------------------------------------------------------------------
procedure snap_sidlist_internal(p_sql in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null)
is
l_snapid number;
begin
-- allocate a new snapid if a global one hasn't been passed down from caller
l_snapid := assign_snapid(p_snapid);
-- call the overloaded snap_sidlist_internal sister-function
-- which accepts sawr$SIDlist collection as a parameter
snap_sidlist_internal( in_list(p_sql), p_session_stats, l_snapid );
end snap_sidlist_internal; -- ( p_sql in varchar2 )
--==================================================================================================================
--==================================================================================================================
--
-- External Procs to be executed by users
--
--==================================================================================================================
--==================================================================================================================
--------------------------------------------------------------------------------------------------------------------
-- procedure for snapping current session
-- useful for ad-hoc instrumentation and performance diagnosis for SQL tuning
--------------------------------------------------------------------------------------------------------------------
procedure snap_me(p_session_stats in varchar2 default 'ALL', p_snapid in number default null) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_ME: '||user;
snap_sidlist_internal( 'select sid from v$mystat where rownum = 1', p_session_stats );
commit;
end snap_me;
--------------------------------------------------------------------------------------------------------------------
-- snap session with given SID
--------------------------------------------------------------------------------------------------------------------
procedure snap_sid ( p_sid in number, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_SID: '||to_char(p_sid);
snap_sidlist_internal( 'select sid from v$session where sid in ('||to_char(p_sid)||')' , p_session_stats );
commit;
end snap_sid;
--------------------------------------------------------------------------------------------------------------------
-- snap session with given SID
--------------------------------------------------------------------------------------------------------------------
procedure snap_sid ( p_sid in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_SID: '||p_sid;
snap_sidlist_internal( 'select sid from v$session where sid in ('||p_sid||')' , p_session_stats );
commit;
end snap_sid;
--------------------------------------------------------------------------------------------------------------------
-- procedure for snapping all sessions
--------------------------------------------------------------------------------------------------------------------
procedure snap_all(p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_ALL:';
snap_sidlist_internal( 'select sid from v$session' , p_session_stats );
commit;
end snap_all;
--------------------------------------------------------------------------------------------------------------------
-- procedure for snapping all BACKGROUND sessions
--------------------------------------------------------------------------------------------------------------------
procedure snap_bg(p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_BG:';
snap_sidlist_internal( 'select sid from v$session where type = ''BACKGROUND''' , p_session_stats );
commit;
end snap_bg;
--------------------------------------------------------------------------------------------------------------------
-- procedure for snapping all USER sessions
--------------------------------------------------------------------------------------------------------------------
procedure snap_fg(p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_FG:';
snap_sidlist_internal( 'select sid from v$session where type = ''USER''' , p_session_stats );
commit;
end snap_fg;
--------------------------------------------------------------------------------------------------------------------
-- procedure for snapping all sessions estabilished by specified Oracle user
-- default value null will snap all sessions by current user
--------------------------------------------------------------------------------------------------------------------
procedure snap_orauser(p_username in varchar2 default user, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_ORAUSER: '||p_username;
snap_sidlist_internal('select sid from v$session where username like '''|| p_username ||'''' , p_session_stats );
commit;
end snap_orauser;
--------------------------------------------------------------------------------------------------------------------
-- procedure for snapping all sessions estabilished by specified OS user
--------------------------------------------------------------------------------------------------------------------
procedure snap_osuser(p_username in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_OSUSER: '||p_username;
snap_sidlist_internal('select sid from v$session where osuser like '''|| p_username ||'''' , p_session_stats );
commit;
end snap_osuser;
--------------------------------------------------------------------------------------------------------------------
-- snap all sessions by program name (v$session.program)
--------------------------------------------------------------------------------------------------------------------
procedure snap_program( p_program in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_PROGRAM: '||p_program;
snap_sidlist_internal('select sid from v$session where program like '''|| p_program ||'''' , p_session_stats );
commit;
end snap_program;
--------------------------------------------------------------------------------------------------------------------
-- snap all sessions by terminal (v$session.terminal)
--------------------------------------------------------------------------------------------------------------------
procedure snap_terminal( p_terminal in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_TERMINAL: '||p_terminal;
snap_sidlist_internal('select sid from v$session where terminal like '''|| p_terminal ||'''' , p_session_stats );
commit;
end snap_terminal;
--------------------------------------------------------------------------------------------------------------------
-- snap all sessions by machine (v$session.machine)
--------------------------------------------------------------------------------------------------------------------
procedure snap_machine( p_machine in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_MACHINE: '||p_machine;
snap_sidlist_internal('select sid from v$session where machine like '''|| p_machine ||'''' , p_session_stats);
commit;
end snap_machine;
--------------------------------------------------------------------------------------------------------------------
-- snap the session being served by SPID (v$process.spid)
--------------------------------------------------------------------------------------------------------------------
procedure snap_spid( p_spid in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_CPID: '||p_spid;
snap_sidlist_internal('select sid from v$session where paddr in ( select addr from v$process where spid in ('''|| p_spid ||'''))' , p_session_stats );
commit;
end snap_spid;
--------------------------------------------------------------------------------------------------------------------
-- snap the session being served by SPID (v$process.spid)
--------------------------------------------------------------------------------------------------------------------
procedure snap_spid( p_spid in number, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_CPID: '||p_spid;
snap_sidlist_internal('select sid from v$session where paddr in ( select addr from v$process where spid in ('''|| to_char(p_spid) ||'''))' , p_session_stats );
commit;
end snap_spid;
--------------------------------------------------------------------------------------------------------------------
-- snap all sessions by client PID (v$session.process)
--------------------------------------------------------------------------------------------------------------------
procedure snap_cpid( p_cpid in varchar2, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_CPID: '||p_cpid;
snap_sidlist_internal('select sid from v$session where process in ('''|| p_cpid ||''')' , p_session_stats );
commit;
end snap_cpid;
--------------------------------------------------------------------------------------------------------------------
-- snap all sessions by client PID (v$session.process)
--------------------------------------------------------------------------------------------------------------------
procedure snap_cpid( p_cpid in number, p_session_stats in varchar2 default 'TYPICAL', p_snapid in number default null ) is
pragma autonomous_transaction;
begin
g_snap_mode:='SNAP_CPID: '||to_char(p_cpid);
snap_sidlist_internal('select sid from v$session where process in ('''|| to_char(p_cpid) ||''')' , p_session_stats );
commit;
end snap_cpid;
--------------------------------------------------------------------------------------------------------------------
-- purge repository data over a certain age threshold
--------------------------------------------------------------------------------------------------------------------
procedure purge_data ( p_days_threshold in number default 7, p_snapid in number default null ) is
type sawr$TableList is table of varchar2(30);
l_tables sawr$TableList := sawr$TableList('SAWR$SNAPSHOTS',
'SAWR$SESSIONS',
'SAWR$SESSION_EVENTS',
'SAWR$SESSION_STATS');
l_snaptime date := trunc(sysdate)-nvl(p_days_threshold,7);
l_codeloc varchar2(200) := 'PURGE_DATA: BEGIN';
l_ddl varchar2(200);
pragma autonomous_transaction;
begin
l_codeloc := 'PURGE_DATA: DELETE DATA';
for i in 1 .. l_tables.count loop
l_codeloc := 'PURGE_DATA: DELETE ' || l_tables(i);
execute immediate ' delete from ' || l_tables(i) ||
' where snaptime < :snaptime ' ||
' and (snapid = :snapid or :snapid is null)'
using l_snaptime, p_snapid, p_snapid;
end loop;
l_codeloc := 'PURGE_DATA: REBUILD_TABLES';
for i in 1 .. l_tables.count loop
l_ddl := case l_tables(i)
when 'SAWR$SNAPSHOTS'
then 'alter index SAWR$SNAPSHOTS_PK coalesce'
else 'alter table ' || l_tables(i) || ' move online'
end;
l_codeloc := 'PURGE_DATA: REBUILD ' || l_tables(i);
execute immediate l_ddl;
end loop;
l_codeloc := 'PURGE_DATA: END';
exception
when others then
raise_application_error(-20001, 'Error '||SQLCODE||': '||SQLERRM||' : FAILED AT '|| l_codeloc) ;
end purge_data;
end sesspack;
/
show errors