How to DBMS_SYSTEM Package

DBMS_SYSTEM package contains a number of procedures that can be useful. Oracle clearly state that these procedures are not supported so use at your own risk.

The package DBMS_SYSTEM is installed by running the script
$ORACLE_HOME/rdbms/admin/dbmsutil.sql
This script is internally called by
$ORACLE_HOME/rdbms/admin/catproc.sql
By default only the SYS user can execute procedures and functions in this package.

To allow other users to execute this package, run the following commands as SYS
GRANT EXECUTE ON dbms_system TO PUBLIC;
CREATE PUBLIC SYNONYM dbms_system FOR dbms_system;

Commonly used procedures or may required for DBA

ksdwrt
SET_SQL_TRACE_IN_SESSION
SET_EV
READ_EV

ksdwrt:

Used to write messages to the alertlog and/or trace files.

EXEC DBMS_System.ksdwrt(n, message);
EXEC DBMS_System.ksdwrt(2, ‘My Test Alertlog Message’);
Where the value of “n” indicates the destination.

1 – Write to trace file.
2 – Write to alertlog.
3 – Write to both.

Example:
To write a message to the trace file use
EXECUTE dbms_system.ksdwrt (1, ‘Output written to trace file’);
To write a message to the alert log use
EXECUTE dbms_system.ksdwrt (2, ‘Output written to alert log’);
To write a message to both the trace file and the alert log use
EXECUTE dbms_system.ksdwrt (3, ‘Output written to both trace file and alert log’);

SET_SQL_TRACE_IN_SESSION:

Used to set trace on or off in another users session.

EXEC DBMS_SYSTEM.set_sql_trace_in_session(sid, serial#, true );
EXEC DBMS_SYSTEM.set_sql_trace_in_session(31, 97, true );

This is the equivalent to enabling event 10046 level 1.

For example to enable trace in a session with SID 9, use:
EXECUTE dbms_system.set_sql_trace_in_session (9, 29, true);
To disable trace in the same session use:
EXECUTE dbms_system.set_sql_trace_in_session (9, 29, false);
The SID and serial# for the session can be obtained from V$SESSION e.g.:
DECLARE
l_sid NUMBER;
l_serial NUMBER;
BEGIN
SELECT sid, serial#
INTO l_sid, l_serial
FROM v$session
WHERE username = ‘User1’;

dbms_system.set_sql_trace_in_session (l_sid, l_serial, TRUE);
END;
/

SET_EV:

Used to set trace on for a specific event.

EXEC DBMS_SYSTEM.set_ev(sid, serial#, event, level, name);
EXEC DBMS_SYSTEM.set_ev(31, 97, 10046, 4, ”);

Where level indicates the following levels of trace.

1 – Standard SQL_TRACE functionality.
4 – As level 1 plus tracing of bind variables.
8 – As level 1 plus wait events.
12 – As level 1 plus bind variables and wait events.

READ_EV:

Used to check if a specific event is currently being traced.

EXEC DBMS_SYSTEM.read_ev(event, output);

If output = 1 the event is being traced.