Saturday, August 26, 2017

Session Tracing in Oracle : trace a session in Oracle SQL trace, 10046 event


Enable before start session trace in oracle
=============================================

ALTER SESSION SET TRACEFILE_IDENTIFIER = "USER_TRACE" ';
alter session set timed_statistics = true';
alter session set statistics_level=ALL';
alter session set max_dump_file_size=UNLIMITED';


Enable trace for user
=================================

CREATE OR REPLACE TRIGGER USER_TRACE_TRG
AFTER LOGON ON DATABASE
BEGIN
    IF USER = 'HR'
  THEN
    EXECUTE IMMEDIATE 'ALTER SESSION SET TRACEFILE_IDENTIFIER = "USER_TRACE" ';
    EXECUTE IMMEDIATE 'alter session set timed_statistics = true';
    EXECUTE IMMEDIATE 'alter session set statistics_level=ALL';
    EXECUTE IMMEDIATE 'alter session set max_dump_file_size=UNLIMITED';
    execute immediate 'alter session set events ''10046 trace name context forever, level 12''';
  END IF;
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
/

* Drop trigger for disable


Enable trace for session
=======================================

ALTER SESSION SET EVENTS '10046 trace name context forever, level 8';


Disabling trace
==============================================

alter system set events '10046 trace name context off';

Find its OS pid from the following
================================================
select p.PID,p.SPID,s.SID from v$process p,v$session s where s.paddr = p.addr and s.sid = &SESSION_ID;

SQL> select sid,serial# from v$session where username='HR';

       SID    SERIAL#
---------- ----------
        42        293


Trace session useing pl/SQL package(DBMS_MONITOR)
==================================================================

Enable tracing
------------------------------------
EXEC DBMS_MONITOR.session_trace_enable(session_id =>42, serial_num=>293, waits=>TRUE, binds=>TRUE);

Disable Tracing
-------------------------------
EXEC DBMS_MONITOR.session_trace_disable(session_id=>42, serial_num=>293);



Identify trace file using  SID
==========================================

SELECT p.tracefile
FROM   v$session s
       JOIN v$process p ON s.paddr = p.addr
WHERE  s.sid = 42;


Example
----------------

SQL> SELECT p.tracefile
FROM   v$session s
       JOIN v$process p ON s.paddr = p.addr
WHERE  s.sid = 42;  2    3    4

TRACEFILE
--------------------------------------------------------------------------------
/u01/app/oracle/diag/rdbms/hr/hr/trace/hr_ora_10369.trc

SQL>



check own SID and trace file
=================================

SET LINESIZE 100
COLUMN trace_file FORMAT A60

SELECT s.sid,
       s.serial#,
       pa.value || '/' || LOWER(SYS_CONTEXT('userenv','instance_name')) ||  
       '_ora_' || p.spid || '.trc' AS trace_file
FROM   v$session s,
       v$process p,
       v$parameter pa
WHERE  pa.name = 'user_dump_dest'
AND    s.paddr = p.addr
AND    s.audsid = SYS_CONTEXT('USERENV', 'SESSIONID');

=====================================================================
Oracle provides event 10046 that can also aid us in collecting extended SQL trace data.

There are four levels available when setting up a trace with Event 10046:

Level 1: this cause tracing of sql activities and is similar to 'alter session set sql_trace=true'
Level 4: provides level 1 tracing + displays the values for all bind variables. It is equivalent to dbms_support.start_trace(waits=>false,binds=>true);

Level 8: provides level 1 tracing and displays a list of all database wait events. It is equivalent to dbms_support.start_trace(waits=>true,binds=>false);

Level 12 provides level 1 tracing in addition to both bind variable substitution and database wait events. It is equivalent to dbms_support.start_trace(waits=>true,binds=>true);

Note that Level 0 disables tracing.

1 is same as a regular trace.
4 means also dump bind variables
8 means also dump wait information
12 means dump both bind and wait information

Tracing Enhancements Using DBMS_MONITOR (In 10g, 11g and Above) (Doc ID 293661.1)

#########################################
Fron oracle Support
##########################################
DBMS_MONITOR.session_trace_enable in SQL*Plus Current Session or Profile Option Method

It is not recommended to use DBMS_MONITOR.session_trace_enable for tracing the current session in SQL*Plus or in the Profile Option “Initialization SQL Statement - Custom”.

The APPS user is used in both of these scenarios and it may not have the correct privilege to run DBMS_MONITOR.

2.19 Obtaining trace for another database session prior to 10g

Prior to 10g the DBMS_MONITOR package is not available. In this case the DBMS_SYSTEM.set_sql_trace_in_session or
DBMS_SUPPORT.start_trace_in_session procedures can be used to trace another session.

The DBMS_SUPPORT and DBMS_SYSTEM packages are not documented So, they should be avoided by customers, unless specifically
instructed by either Oracle Support or Oracle Development.

The DBMS_SUPPORT package is not installed by default. The package is supplied in files dbmssupp.sql and prvtsupp.plb (these are in $ORACLE_HOME/rdbms/admin).

The DBMS_SYSTEM package is usually already installed (but it can be installed using catproc.sql).

Public synonyms are not created (by default) for either package and no privileges are granted. So use SYS to run these procedures.

See My Oracle Support documents:

The DBMS_SUPPORT Package (Document 62294.1)

For both of these methods the Session ID (SID) and Serial Number will be needed. These can be obtained by querying the v$session and v$process views.

DBMS_SYSTEM

To enable SQL Trace run the following:

EXEC DBMS_SYSTEM.set_sql_trace_in_session(sid=><sid>,
                                          serial#=><serial#>,
                                          sql_trace=>TRUE);
To disable SQL Trace run the following:

EXEC DBMS_SYSTEM.set_sql_trace_in_session(sid=><sid>,
                                          serial#=><serial#>,
                                          sql_trace=>FALSE);
DBMS_SUPPORT

To enable SQL Trace run the following :

EXEC DBMS_SUPPORT.start_trace_in_session(sid=><sid>,
                                         serial=><serial#>,
                                         waits=>true,
                                         binds=>false);
Note that with waits, but not binds is recommended (and the default in this case). But other options can be chosen.

To disable SQL Trace run the following :

EXEC DBMS_SUPPORT.stop_trace_in_session(sid=><sid>,
                                        serial=><serial#>);
Serial Number can be set to NULL (or 0) to trace the current session with the supplied SID.

SERIAL only needs to be specified to ensure that a specific session is traced.
If the session ends and the SID is reused before the START_TRACE_IN_SESSION call is made then including
the SERIAL will cause an error to be raised (rather than the trace being started against a different session to the one intended).

Script to Show Objects That are Missing Statistics

Script to Show Objects That are Missing Statistics:-
================================================


SELECT 'TABLE' object_type,owner, table_name object_name, last_analyzed, stattype_locked, stale_stats
FROM all_tab_statistics
WHERE (last_analyzed IS NULL OR stale_stats = 'YES') and stattype_locked IS NULL
and owner NOT IN ('ANONYMOUS', 'CTXSYS', 'DBSNMP', 'EXFSYS','LBACSYS','MDSYS','MGMT_VIEW','OLAPSYS','OWBSYS','ORDPLUGINS','ORDSYS','OUTLN','SI_INFORMTN_SCHEMA','SYS', 'SYSMAN','SYSTEM' )
AND owner NOT LIKE 'FLOW%'
UNION ALL
SELECT 'INDEX' object_type,owner, index_name object_name,  last_analyzed, stattype_locked, stale_stats
FROM all_ind_statistics
WHERE (last_analyzed IS NULL OR stale_stats = 'YES') and stattype_locked IS NULL
AND owner NOT IN ('ANONYMOUS', 'CTXSYS', 'DBSNMP', 'SYS' )
AND owner NOT LIKE 'FLOW%'
ORDER BY object_type desc, owner, object_name
/


Script to Show Objects That are Missing Statistics (Doc ID 957993.1)


Tuesday, August 15, 2017

ORA-20782: Oracle GoldenGate DDL Replication Error: Code :ORA-20782: Cannot DROP object used in Oracle GoldenGate replication while trigger is enabled

ORA-00604: error occurred at recursive SQL level 2
ORA-20782: Oracle GoldenGate DDL Replication Error: Code :ORA-20782: Cannot
DROP object used in Oracle GoldenGate replication while trigger is enabled.
Consult Oracle GoldenGate documentation and/or call Oracle GoldenGate Technical
Support if you wish to do so., error stack: ORA-06512: at line 310
ORA-06512: at line 1272

SQL> select * from v$tablespace;

       TS# NAME                           INC BIG FLA ENC
---------- ------------------------------ --- --- --- ---
         0 SYSTEM                         YES NO  YES
         1 SYSAUX                         YES NO  YES
         2 UNDOTBS1                       YES NO  YES
         4 USERS                          YES NO  YES
         3 TEMP                           NO  NO  YES
         6 EXAMPLE                        YES NO  YES
         7 TEST                           YES NO  YES
         8 GG                             YES NO  YES


SQL> drop tablespace gg including contents and datafiles;
drop tablespace gg including contents and datafiles
*
ERROR at line 1:
ORA-00604: error occurred at recursive SQL level 2
ORA-20782: Oracle GoldenGate DDL Replication Error: Code :ORA-20782: Cannot
DROP object used in Oracle GoldenGate replication while trigger is enabled.
Consult Oracle GoldenGate documentation and/or call Oracle GoldenGate Technical
Support if you wish to do so., error stack: ORA-06512: at line 310
ORA-06512: at line 1272

[oracle@pri ~]$ sqlp

SQL*Plus: Release 11.2.0.4.0 Production on Tue Aug 15 10:09:43 2017

Copyright (c) 1982, 2013, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> drop trigger GGS_DDL_TRIGGER_BEFORE;

Trigger dropped.

SQL> drop tablespace gg including contents and datafiles;

Tablespace dropped.

SQL>

Sunday, July 30, 2017

Apply patch 18706472 using Opatch auto in RAC

OPatch:

Opatch apply is used to apply single patch
Opatch napply is used to apply multiple patch at the same time.

==========================================================


export GRID_HOME=/u01/app/11.2.0/grid_1
export PATH=$PATH:$GRID_HOME/OPatch:$GRID_HOME/bin


[grid@rac2 OPatch]$
[grid@rac2 OPatch]$ export PATH=$PATH:$GRID_HOME/OPatch:$GRID_HOME/bin
[grid@rac2 OPatch]$
[grid@rac2 OPatch]$ opatch version
OPatch Version: 11.2.0.3.15

OPatch succeeded.
[grid@rac2 OPatch]$
[grid@rac2 OPatch]$
[grid@rac2 OPatch]$


[grid@rac2 bin]$
[grid@rac2 bin]$
[grid@rac2 bin]$ ./emocmrsp -no_banner -output /u01/oracle_software/grid_patch.rsp
Provide your email address to be informed of security issues, install and
initiate Oracle Configuration Manager. Easier for you if you use your My
Oracle Support Email address/User Name.
Visit http://www.oracle.com/support/policies.html for details.
Email address/User Name:

You have not provided an email address for notification of security issues.
Do you wish to remain uninformed of security issues ([Y]es, [N]o) [N]:  y
The OCM configuration response file (/u01/oracle_software/grid_patch.rsp) was successfully created.
[grid@rac2 bin]$
[grid@rac2 bin]$
[grid@rac2 bin]$



[grid@rac2 oracle_software]$
[grid@rac2 oracle_software]$ opatch prereq CheckConflictAgainstOHWithDetail -phBaseDir ./18706472/
Oracle Interim Patch Installer version 11.2.0.3.15
Copyright (c) 2017, Oracle Corporation.  All rights reserved.

PREREQ session

Oracle Home       : /u01/app/11.2.0/grid_1
Central Inventory : /u01/app/oraInventory
   from           : /u01/app/11.2.0/grid_1/oraInst.loc
OPatch version    : 11.2.0.3.15
OUI version       : 11.2.0.4.0
Log file location : /u01/app/11.2.0/grid_1/cfgtoollogs/opatch/opatch2017-07-30_03-15-27AM_1.log

Invoking prereq "checkconflictagainstohwithdetail"

Prereq "checkConflictAgainstOHWithDetail" passed.

OPatch succeeded.
[grid@rac2 oracle_software]$
[grid@rac2 oracle_software]$
[grid@rac2 oracle_software]$


[root@rac2 ~]# cd /u01/oracle_software/
[root@rac2 oracle_software]#
[root@rac2 oracle_software]#
[root@rac2 oracle_software]# opatch prereq CheckConflictAgainstOHWithDetail -phBaseDir ./18706472/
The user is root. OPatch cannot continue if the user is root.

OPatch failed with error code 255
[root@rac2 oracle_software]#



[grid@rac2 oracle_software]$
[grid@rac2 oracle_software]$ opatch lspatches
18031731;ACFS Patch Set Update : 11.2.0.4.2 (18031731)
18031740;OCW Patch Set Update : 11.2.0.4.2 (18031740)
18031668;Database Patch Set Update : 11.2.0.4.2 (18031668)

OPatch succeeded.
[grid@rac2 oracle_software]$




[root@rac2 oracle_software]#
[root@rac2 oracle_software]# opatch auto /u01/oracle_software/18706472 -ocmrf /u01/oracle_software/grid_patch.rsp
Executing /u01/app/11.2.0/grid_1/perl/bin/perl /u01/app/11.2.0/grid_1/OPatch/crs/patch11203.pl -patchdir /u01/oracle_software -patchn 18706472 -ocmrf /u01/oracle_software/grid_patch.rsp -paramfile /u01/app/11.2.0/grid_1/crs/install/crsconfig_params

This is the main log file: /u01/app/11.2.0/grid_1/cfgtoollogs/opatchauto2017-07-30_03-20-01.log

This file will show your detected configuration and all the steps that opatchauto attempted to do on your system:
/u01/app/11.2.0/grid_1/cfgtoollogs/opatchauto2017-07-30_03-20-01.report.log

2017-07-30 03:20:01: Starting Clusterware Patch Setup
Using configuration parameter file: /u01/app/11.2.0/grid_1/crs/install/crsconfig_params

Stopping RAC /u01/app/oracle/product/11.2.0/dbhome_1 ...
Stopped RAC /u01/app/oracle/product/11.2.0/dbhome_1 successfully

patch /u01/oracle_software/18706472/18522509  apply successful for home  /u01/app/oracle/product/11.2.0/dbhome_1
patch /u01/oracle_software/18706472/18522515/custom/server/18522515  apply successful for home  /u01/app/oracle/product/11.2.0/dbhome_1

Stopping CRS...
Stopped CRS successfully

patch /u01/oracle_software/18706472/18522509  apply successful for home  /u01/app/11.2.0/grid_1
patch /u01/oracle_software/18706472/18522515  apply successful for home  /u01/app/11.2.0/grid_1
patch /u01/oracle_software/18706472/18522514  apply successful for home  /u01/app/11.2.0/grid_1

Starting CRS...
Installing Trace File Analyzer
CRS-4123: Oracle High Availability Services has been started.

Starting RAC /u01/app/oracle/product/11.2.0/dbhome_1 ...
Started RAC /u01/app/oracle/product/11.2.0/dbhome_1 successfully

opatch auto succeeded.
[root@rac2 oracle_software]#


[grid@rac2 oracle_software]$ opatch lspatches
18522514;ACFS PATCH SET UPDATE : 11.2.0.4.3 (18522514)
18522515;OCW Patch Set Update : 11.2.0.4.3 (18522515)
18522509;Database Patch Set Update : 11.2.0.4.3 (18522509)

OPatch succeeded.
[grid@rac2 oracle_software]$





opatch log 
============================

[grid@rac2 cfgtoollogs]$ cat opatchauto2017-07-30_03-20-01.report.log
***********  Configuration Data  ***********
* It shows only those targets that will be patched in this session *


crs_home=/u01/app/11.2.0/grid_1      owner=grid      opatch_ver=11.2.0.3.15
rac_home=/u01/app/oracle/product/11.2.0/dbhome_1      owner=oracle      opatch_ver=11.2.0.3.15

*********** Steps to be executed as owner unless specified as root ***********


1: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch prereq CheckComponents -ph /u01/oracle_software/18706472/18522509 -oh /u01/app/oracle/product/11.2.0/dbhome_1

2: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch prereq CheckComponents -ph /u01/oracle_software/18706472/18522515/custom/server/18522515 -oh /u01/app/oracle/product/11.2.0/dbhome_1

3: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch prereq CheckConflictAgainstOH -ph /u01/oracle_software/18706472/18522509 -oh /u01/app/oracle/product/11.2.0/dbhome_1

4: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch prereq CheckConflictAgainstOH -ph /u01/oracle_software/18706472/18522515/custom/server/18522515 -oh /u01/app/oracle/product/11.2.0/dbhome_1

5: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckComponents -ph /u01/oracle_software/18706472/18522509 -oh /u01/app/11.2.0/grid_1

6: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckComponents -ph /u01/oracle_software/18706472/18522515 -oh /u01/app/11.2.0/grid_1

7: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckComponents -ph /u01/oracle_software/18706472/18522514 -oh /u01/app/11.2.0/grid_1

8: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckConflictAgainstOH -ph /u01/oracle_software/18706472/18522509 -oh /u01/app/11.2.0/grid_1

9: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckConflictAgainstOH -ph /u01/oracle_software/18706472/18522515 -oh /u01/app/11.2.0/grid_1

10: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckConflictAgainstOH -ph /u01/oracle_software/18706472/18522514 -oh /u01/app/11.2.0/grid_1

11: /u01/app/oracle/product/11.2.0/dbhome_1/bin/emctl stop dbconsole

12: /u01/app/oracle/product/11.2.0/dbhome_1/bin/emctl stop agent

13: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch prereq CheckApplicable -ph /u01/oracle_software/18706472/18522509 -oh /u01/app/oracle/product/11.2.0/dbhome_1

14: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch prereq CheckApplicable -ph /u01/oracle_software/18706472/18522515/custom/server/18522515 -oh /u01/app/oracle/product/11.2.0/dbhome_1

15: /u01/app/oracle/product/11.2.0/dbhome_1/bin/srvctl stop home -o /u01/app/oracle/product/11.2.0/dbhome_1 -s /u01/app/oracle/product/11.2.0/dbhome_1/srvm/admin/stophome.txt -n rac2 -f

16: /u01/oracle_software/18706472/18522515/custom/server/18522515/custom/scripts/prepatch.sh -dbhome /u01/app/oracle/product/11.2.0/dbhome_1

17: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch napply /u01/oracle_software/18706472/18522509 -local -silent -ocmrf /u01/oracle_software/grid_patch.rsp -oh /u01/app/oracle/product/11.2.0/dbhome_1 -invPtrLoc /u01/app/oracle/product/11.2.0/dbhome_1/oraInst.loc

18: /u01/app/oracle/product/11.2.0/dbhome_1/OPatch/opatch napply /u01/oracle_software/18706472/18522515/custom/server/18522515 -local -silent -ocmrf /u01/oracle_software/grid_patch.rsp -oh /u01/app/oracle/product/11.2.0/dbhome_1 -invPtrLoc /u01/app/oracle/product/11.2.0/dbhome_1/oraInst.loc

19: /u01/oracle_software/18706472/18522515/custom/server/18522515/custom/scripts/postpatch.sh -dbhome /u01/app/oracle/product/11.2.0/dbhome_1

20: /u01/app/11.2.0/grid_1/crs/install/rootcrs.pl -unlock : run as root

21: /sbin/fuser -k /u01/app/11.2.0/grid_1/bin/crsctl.bin : run as root

22: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckApplicable -ph /u01/oracle_software/18706472/18522509 -oh /u01/app/11.2.0/grid_1

23: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckApplicable -ph /u01/oracle_software/18706472/18522515 -oh /u01/app/11.2.0/grid_1

24: /u01/app/11.2.0/grid_1/OPatch/opatch prereq CheckApplicable -ph /u01/oracle_software/18706472/18522514 -oh /u01/app/11.2.0/grid_1

25: /u01/app/11.2.0/grid_1/OPatch/opatch napply /u01/oracle_software/18706472/18522509 -local -silent -ocmrf /u01/oracle_software/grid_patch.rsp -oh /u01/app/11.2.0/grid_1 -invPtrLoc /u01/app/11.2.0/grid_1/oraInst.loc

26: /u01/app/11.2.0/grid_1/OPatch/opatch napply /u01/oracle_software/18706472/18522515 -local -silent -ocmrf /u01/oracle_software/grid_patch.rsp -oh /u01/app/11.2.0/grid_1 -invPtrLoc /u01/app/11.2.0/grid_1/oraInst.loc

27: /u01/app/11.2.0/grid_1/OPatch/opatch napply /u01/oracle_software/18706472/18522514 -local -silent -ocmrf /u01/oracle_software/grid_patch.rsp -oh /u01/app/11.2.0/grid_1 -invPtrLoc /u01/app/11.2.0/grid_1/oraInst.loc

28: /u01/app/11.2.0/grid_1/bin/emctl start dbconsole

29: /u01/app/11.2.0/grid_1/rdbms/install/rootadd_rdbms.sh : run as root

30: /u01/app/11.2.0/grid_1/crs/install/rootcrs.pl -patch : run as root

31: /u01/app/oracle/product/11.2.0/dbhome_1/bin/emctl start dbconsole

32: /u01/app/oracle/product/11.2.0/dbhome_1/bin/emctl start agent

33: /u01/app/oracle/product/11.2.0/dbhome_1/bin/srvctl start home -o /u01/app/oracle/product/11.2.0/dbhome_1 -s /u01/app/oracle/product/11.2.0/dbhome_1/srvm/admin/stophome.txt -n rac2

[grid@rac2 cfgtoollogs]$