Thursday, February 28, 2013

ORACLE Data Guard : Query

select NAME, DB_UNIQUE_NAME, OPEN_MODE, PROTECTION_MODE, DATABASE_ROLE from v$database;

select THREAD#, max(sequence#) from v$log_history group by  THREAD# order by THREAD# ;
SELECT PROCESS, STATUS FROM V$MANAGED_STANDBY; (if no RFS process then add tnsnames.ora)

Set linesize 1000
col DESTINATION for a40 
col ERROR for a80
select dest_id, archived_seq#, status,destination,error from v$archive_dest_status;

Following query will identify all archivelogs applied to the standby:
select a.thread#, a.sequence#, a.applied from v$archived_log a, v$database d where a.activation# = d.activation#  and a.applied='YES';
select a.thread#, a.sequence#, a.applied from v$archived_log a, v$database d where a.activation# = d.activation#  and a.applied='NO';

SELECT * FROM V$ARCHIVE_GAP;
SELECT NAME FROM V$ARCHIVED_LOG WHERE THREAD#=1 AND DEST_ID=1 AND  SEQUENCE# BETWEEN 443 AND 446;

SELECT SWITCHOVER_STATUS FROM V$DATABASE;

ORACEL - Data Guard : DGMGRL Setup

=======================================================================  
Setup DGMGRL
=======================================================================
1) dg_broker_config_file1 and dg_broker_config_file2 on all instances of primary and standby:
alter system set  DG_BROKER_CONFIG_FILE1 = '+DG_HMWHIST_01/ATEST/DGBROKER/dr1ATEST.dat' scope=both sid='*';
alter system set  DG_BROKER_CONFIG_FILE2 = '+DG_HMWHIST_01/ATEST/DGBROKER/dr2exitATEST.dat' scope=both sid='*';
(default location of broker configuration file is $ORACLE_HOME/dbs )

Create directory for DG_BROKER in ASM:
amscmd
cd +DG_HMWHIST_01/ATEST
mkdir DGBROKER

For STBY database:
alter system set  DG_BROKER_CONFIG_FILE1 = '+DG_HMWHIST_01/ATEST_STBY/DGBROKER/dr1ATEST.dat' scope=both sid='*';
alter system set  DG_BROKER_CONFIG_FILE2 = '+DG_HMWHIST_01/ATEST_STBY/DGBROKER/dr2exitATEST.dat' scope=both sid='*';

2) Enable broker on both primary and standby:
alter system set DG_BROKER_START=TRUE scope=both sid='*';

ps -ef | grep dmon  ==> Check dgmgrl is running or not

3) Create configuration on primary:(Use DB_UNIQUE NAME):
dgmgrl> connect sys/password
dgmgrl> CREATE CONFIGURATION 'PROD_CONF' AS PRIMARY DATABASE IS 'pri_db_UNIQUE_NAME' CONNECT IDENTIFIER IS PRI_DB_CONNECT_STRING;
Example:
CREATE CONFIGURATION 'TEST_CONF' AS PRIMARY DATABASE IS 'ATEST' CONNECT IDENTIFIER IS ATEST;

4) Add standby in the configuration (Use DB_UNIQUE NAME):
dgmgrl> ADD DATABASE 'stby_db_UNIQUE_NAME' AS CONNECT IDENTIFIER IS STBY_DB_CONNECT_STRING MAINTAINED AS PHYSICAL;
Example:
ADD DATABASE 'ATEST_STBY' AS CONNECT IDENTIFIER IS ATEST_STBY MAINTAINED AS PHYSICAL;

5) Enable Configuration:
dgmgrl> ENABLE CONFIGURATION;
Any error: Check dmon log file:
/u01/app/oracle/diag/rdbms/atest_stby/ATEST/trace> tail -f drcATEST.log

6) Use the SHOW command to verify that the configuration and its databases were successfully enabled:
dgmgrl> SHOW CONFIGURATION;
SHOW DATABASE VERBOSE 'ATEST'
SHOW DATABASE VERBOSE 'ATEST_STBY'
show database 'ATEST' statusreport;
show database 'ATEST_STBY' statusreport;
show database 'ATEST' InconsistentProperties
show database 'ATEST__STBY' InconsistentProperties

ORACLE - Data Guard - STARTUP - MOUNT - OPEN READ ONLY

Let check the different scenarios to open a data guard:
========================================================================
1. STARTUP will open with READ ONLY WITH APPLY
========================================================================
SQL> shutdown immediate
ORA-01109: database not open

Database dismounted.
ORACLE instance shut down.
SQL> STARTUP
ORACLE instance started.
Total System Global Area 1043886080 bytes
Fixed Size                  2234960 bytes
Variable Size             729810352 bytes
Database Buffers          306184192 bytes
Redo Buffers                5656576 bytes
Database mounted.
Database opened.

SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
READ ONLY WITH APPLY MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY

Note: It will take few seconds to change status from READ ONLY to READ ONLY WITH STANDBY

SQL>select * from jtable;
1000

Using DGMGRL:
DGMGRL> show database verbose 'ATEST_STBY';
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: ON
  Instance(s):   ATEST

========================================================================
2. STARTUP MOUNT will open with MOUNTED and REDO APPLY ON
========================================================================
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 1043886080 bytes
Fixed Size                  2234960 bytes
Variable Size             729810352 bytes
Database Buffers          306184192 bytes
Redo Buffers                5656576 bytes
Database mounted.

SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
MOUNTED              MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY

SQL> select * from jtable2;
select * from jtable
              *
ERROR at line 1:
ORA-01219: database not open: queries allowed on fixed tables/views only
Using DGMGRL:

DGMGRL> show database verbose 'ATEST_STBY';
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: OFF
  Instance(s):    ATEST

========================================================================
3. Oracle 11gR2 : alter database open read only will make DG READONLY with REDO APPLY
========================================================================
SQL>  alter database open read only;
Database altered.

SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
READ ONLY            MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY

SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
READ ONLY WITH APPLY MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY

Using DGMGRL:
show database verbose 'ATEST_STBY';
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: ON
  Instance(s):   ATEST

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

ORACLE - Setup Data Guard 11gR2 - Step-By-Step

======================================================================
Step 1) Listener.ora, tnsnames.ora, password files :Both side (primary  and standby)
======================================================================
Listener.ora
Note: I used default listener and grid home for listener
SID_LIST_LISTENER =
(SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = ATEST)
      (ORACLE_HOME = /u01/app/oracle/product/11.2.0/db_1)
      (SID_NAME = ATEST)
    )
  )
tnsnames.ora
ATEST =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = hpmidb02)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = ATEST)
    )
  )
ATEST_STBY =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = npmidb02)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SID = ATEST)
    )
  )

Create password file using like:
orapwd file=$ORACLE_HOME/dbs/orapwATEST password=<password>

On Primary side:
check :
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
show parameter db_unique_name
archive log list;
alter database force logging;
alter system set standby_file_management = 'AUTO';
alter system set log_archive_config='dg_config=(ATEST,ATEST_STBY)'
alter system set fal_server = 'ATEST_STBY'; # fal_client is deprecated in 11g
alter system set log_archive_dest_1 = 'location=/bkup/ATEST/arch valid_for=(all_logfiles, all_roles) DB_UNIQUE_NAME=ATEST' scope=both ;
alter system set log_archive_dest_2 = 'service=ATEST_STBY ASYNC valid_for=(online_logfile,primary_role) db_unique_name=ATEST_STBY';
# Add standby database like:
ALTER DATABASE ADD STANDBY LOGFILE THREAD 1 SIZE 100m;

Best Practice for STandby logs:
1.Create standby redo logs.
2.Logs must be the same size as on-line redo logs and must have a least one extra group to accommodate lag.
3.Do not multiplex standby redo logs.

Optional: (db_recovery_file_dest, db_recovery_file_dest_size for FLASHBACK database)

========================================================================
Step 2) Create init.ora with ONLY with db_Name  And create a audit directory (if does not exists)
and STARTUP database with NOMOUNT Note:Do not use spfile
========================================================================
On the $ORACLE_HOME/dbs of the standby database, create a dummy init.ora and a password file
echo 'DB_NAME = ATEST' > $ORACLE_HOME/dbs/initATEST.ora
orapwd file=$ORACLE_HOME/dbs/orapwATEST password=<same as sys for Primary> 

Create dir ATEST at
/u01/app/oracle/admin/ATEST/adump
To avoid following error:
RMAN-04014: startup failed:
ORA-09925: Unable to create audit trail file

Check other directtories also.

STARTUP with NOMOUNT option
sqlplus / as sysdba
startup nomount
sqlplus / as sysdba
SQL*Plus: Release 11.2.0.3.0 Production on Wed Feb 20 09:14:13 2013
Copyright (c) 1982, 2011, Oracle.  All rights reserved.
Connected to an idle instance.
SQL> startup nomount
ORACLE instance started.
Total System Global Area  329895936 bytes
Fixed Size                  2228024 bytes
Variable Size             255852744 bytes
Database Buffers           67108864 bytes
Redo Buffers                4706304 bytes

Verify:
show parameter db_name

========================================================================
Step 3) Duplicate target database for standby from active database
========================================================================
rman                 
connect target sys/pssword@ATEST
connect auxiliary sys/password@ATEST_STBY
run                  
{                    
   allocate channel p1 type disk;
   allocate channel p2 type disk;
   allocate auxiliary channel a1 type disk;
   duplicate target database for standby from active database
   spfile            
      parameter_value_convert 'ATEST','ATEST'
      set db_unique_name='ATEST_STBY'
      set control_files='+DG_HMWHIST_01'                       
      set db_create_file_dest='+DG_HMWHIST_01'
      set log_archive_config='dg_config=(ATEST,ATEST_STBY)'
      set log_archive_dest_1='LOCATION=/bkup/ATEST/arch VALID_FOR=(ALL_LOGFILES,ALL_ROLES) DB_UNIQUE_NAME=ATEST_STBY'
      set log_archive_dest_2='service=ATEST ASYNC db_unique_name=ATEST valid_for=(ONLINE_LOGFILES,PRIMARY_ROLES)'
      set fal_server = 'ATEST'
   nofilenamecheck;   
}

check:
SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
MOUNTED              MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY


 ---- Done

ORACLE - Data Guard : Real-Time Query

Real-time Query Restrictions:
The following additional restrictions apply if STANDBY_MAX_DATA_DELAY is set to 0 or if the ALTER SESSION SYNC WITH PRIMARY SQL statement is used:

1.The standby database must receive redo data via the SYNC transport
2.The redo transport status at the standby database must be SYNCHRONIZED and the primary database must be running in either maximum protection mode or maximum availability mode.
3.Real-time apply must be enabled.

Configuring Apply Lag Tolerance in a Real-time Query:
The STANDBY_MAX_DATA_DELAY session parameter can be used to specify a session-specific apply lag tolerance, measured in seconds, for queries issued by non-administrative users to a physical standby database that is in real-time query mode.

If STANDBY_MAX_DATA_DELAY is set to the default value of NONE, queries issued to a physical standby database will be executed regardless of the apply lag on that database.

If STANDBY_MAX_DATA_DELAY is set to a non-zero value, a query issued to a physical standby database will be executed only if the apply lag is less than or equal to STANDBY_MAX_DATA_DELAY. Otherwise, an ORA-3172 error is returned to alert the client that the apply lag is too large.

If STANDBY_MAX_DATA_DELAY is set to 0, a query issued to a physical standby database is guaranteed to return the exact same result as if the query were issued on the primary database, unless the standby database is lagging behind the primary database, in which case an ORA-3172 error is returned.

Forcing Redo Apply Synchronization in a Real-time Query Environment:
ALTER SESSION SYNC WITH PRIMARY;This statement will block until all redo data received by the standby database at the time that this command is issued has been applied to the physical standby database. An ORA-3173 error is returned immediately, and synchronization will not occur, if the redo transport status at the standby database is not SYNCHRONIZED or if Redo Apply is not active.

or

For example, you could create the following trigger that would execute the ALTER SESSION SYNC WITH PRIMARY statement for a specific user connection at logon:
CREATE TRIGGER adg_logon_sync_trigger
 AFTER LOGON ON user.schema
  begin
    if (SYS_CONTEXT('USERENV', 'DATABASE_ROLE')  IN ('PHYSICAL STANDBY')) then
      execute immediate 'alter session sync with primary';
    end if;
  end;


Ref. ORACLE Docs/Metalink



Wednesday, February 27, 2013

ORACLE - Active Data Guard Enable/Disable

======================================================================
Open Read only mode - Enable Active data guard :  Using SQL Plus or Broker (11gR1) But for
11gR2 : alter database open read only; is replace folloing 3 steps.
======================================================================
1) Stop the redo apply:
DGMGRL> edit database 'ATEST_STBY' set state='APPLY-OFF';
DGMGRL> show database verbose 'ATEST_STBY'
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-OFF
  Transport Lag:   0 seconds
  Apply Lag:       9 seconds
  Real Time Query: OFF
  Instance(s):    ATEST

2. Open the database as read-only
sql> alter database open read only;
SELECT database_role, open_mode FROM v$database;
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY WITH APPLY

3.Restart the redo apply:
DGMGRL> edit database ATEST_STBY set state='APPLY-ON';
show database verbose 'ATEST_STBY'
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: ON
  Instance(s):     ATEST

Note: 11gR2 : alter database open read only; is replaced avove 3 steps.
======================================================================
Using SQL PLUS : Enable Active data guard
======================================================================
1. Stop the redo and open the database as read-only :
recover managed standby database cancel;
alter database open read only;

2.Restart the redo apply:
recover managed standby database disconnect using current logfile;

==================================================================
Disable Active Data Guard
==================================================================
SQL> SELECT database_role, open_mode FROM v$database;
DATABASE_ROLE    OPEN_MODE
---------------- --------------------
PHYSICAL STANDBY READ ONLY WITH APPLY

SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount;
ORACLE instance started.
Total System Global Area 1043886080 bytes
Fixed Size                  2234960 bytes
Variable Size             729810352 bytes
Database Buffers          306184192 bytes
Redo Buffers                5656576 bytes
Database mounted.

SQL> alter database recover managed standby database using current logfile disconnect;
alter database recover managed standby database using current logfile disconnect
*
ERROR at line 1:
ORA-01153: an incompatible media recovery is active

DGMGRL> show database verbose 'ATEST_STBY'
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: OFF
  Instance(s):    ATEST

================================================================
If DGMGRL setup: Shutdown and STARTUP will open with read only REDO APPLY
===============================================================
SQL> shutdown immediate
ORA-01109: database not open

Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.
Total System Global Area 1043886080 bytes
Fixed Size                  2234960 bytes
Variable Size             729810352 bytes
Database Buffers          306184192 bytes
Redo Buffers                5656576 bytes
Database mounted.
Database opened.

SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
READ ONLY WITH APPLY MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY

SQL>select * from jtable;
1000

Using DGMGRL:
DGMGRL> show database verbose 'ATEST_STBY';
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: ON
  Instance(s):   ATEST

=========================================================
If DGMGRL setup: Shutdown and STARTUP MOUNT
=========================================================
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 1043886080 bytes
Fixed Size                  2234960 bytes
Variable Size             729810352 bytes
Database Buffers          306184192 bytes
Redo Buffers                5656576 bytes
Database mounted.
SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
MOUNTED              MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY
SQL> SELECT PROCESS, STATUS FROM V$MANAGED_STANDBY  ;
PROCESS   STATUS
--------- ------------
ARCH      CONNECTED
ARCH      CONNECTED
ARCH      CONNECTED
ARCH      CONNECTED
RFS       IDLE
RFS       IDLE
6 rows selected.

SQL> select * from jtable2;
select * from jtable
              *
ERROR at line 1:
ORA-01219: database not open: queries allowed on fixed tables/views only
Using DGMGRL:
DGMGRL> show database verbose 'ATEST_STBY';
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: OFF
  Instance(s):    ATEST

=====================================================================
11gR2 : alter database open read only will make DG READONLY with APPLY
=====================================================================
SQL>  alter database open read only;
Database altered.
SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
READ ONLY            MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY
SQL> SELECT OPEN_MODE, PROTECTION_MODE, PROTECTION_LEVEL, DATABASE_ROLE ROLE FROM V$DATABASE;
OPEN_MODE            PROTECTION_MODE      PROTECTION_LEVEL     ROLE
-------------------- -------------------- -------------------- ----------------
READ ONLY WITH APPLY MAXIMUM PERFORMANCE  MAXIMUM PERFORMANCE  PHYSICAL STANDBY
SQL> SELECT PROCESS, STATUS FROM V$MANAGED_STANDBY  ;
PROCESS   STATUS
--------- ------------
ARCH      CONNECTED
ARCH      CONNECTED
ARCH      CONNECTED
ARCH      CLOSING
RFS       IDLE
RFS       IDLE
MRP0      APPLYING_LOG
RFS       IDLE
8 rows selected.
Using DGMGRL:
show database verbose 'ATEST_STBY';
Database - ATEST_STBY
  Role:            PHYSICAL STANDBY
  Intended State:  APPLY-ON
  Transport Lag:   0 seconds
  Apply Lag:       0 seconds
  Real Time Query: ON
  Instance(s):   ATEST

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






ORACLE - Data Guard and NOLOGGING

At Primary:alter database no force logging;
select force_logging from v$database;
select tablespace_name from dba_tablespaces where logging = 'NOLOGGING';

create table jtable (num number);
insert /*+ APPEND */ into jtable select 1000 from dual nologging;
commit;

select * from jtable;
1000

select file#, unrecoverable_change#, to_char(unrecoverable_time,'hh:mm:ss') from v$datafile
where UNRECOVERABLE_CHANGE# > 0;

     FILE# UNRECOVERABLE_CHANGE# TO_CHAR(
---------- --------------------- --------
         1               2280040 03:02:43


At Stabdby database:
Note: Convert into READ ONLY mode:

select * from jtable;
ERROR:ORA-01578: ORACLE data block corrupted (file # 1, block # 90387)
ORA-01110: data file 1:
'+DG_HMWHIST_01/atest_stby/datafile/system.274.808333075'
ORA-26040: Data block was loaded using the NOLOGGING option

Alert log:Wed Feb 27 15:10:20 2013
Errors in file /u01/app/oracle/diag/rdbms/atest_stby/ATEST/trace/ATEST_ora_21019.trc  (incident=9873):
ORA-01578: ORACLE data block corrupted (file # 1, block # 90387)
ORA-01110: data file 1: '+DG_JDATA_01/atest_stby/datafile/system.274.808333075'
ORA-26040: Data block was loaded using the NOLOGGING option
Incident details in: /u01/app/oracle/diag/rdbms/atest_stby/ATEST/incident/incdir_9873/ATEST_ora_21019_i9873.trc
Wed Feb 27 15:10:22 2013


Solution:ENABLE DATABASE LEVEL LOGGING
DROP and RE-CREATE TABLE WITH DATA

Tuesday, February 26, 2013

ORACLE - Locks, Blocking Sesssion and Kill

How to find dead locks ?

select s1.username || '@' || s1.machine || ' ( SID=' || s1.sid || ' )  is blocking '
|| s2.username || '@' || s2.machine || ' ( SID=' || s2.sid || ' ) ' AS blocking_status
from v$lock l1, v$session s1, v$lock l2, v$session s2
where s1.sid=l1.sid and s2.sid=l2.sid
and l1.BLOCK=1 and l2.request > 0
and l1.id1 = l2.id1
and l2.id2 = l2.id2 ;

How to find information regarding BLOCKING SESSION ?

select sid, event, username,machine , program from v$session
where sid in (select blocking_session from gv$session where blocking_session is not null);

Find SQL using SID ?

select s.sid, q.sql_text from gv$sqltext q, gv$session s
where q.address = s.sql_address and s.sid = 130 order by piece;

How to kill a seesion ?

select SID, SERIAL# from v$session;
alter system kill session '29, 63' immediate;

ORACLE - Instance Caging

Instance Caging allows the DBA to limit the CPU usage of an Oracle instance by setting the CPU_COUNT initialization parameter and enabling CPU resource management.With Instance Caging, users can partition CPU resources among multiple instances running on a server to ensure predictable performance.

ORACLE - DROP DATABASE

How to drop database in Oracle ?

SQL> startup mount restrict;ORACLE instance started.
Total System Global Area 1043886080 bytes
Fixed Size                  2234960 bytes
Variable Size             717227440 bytes
Database Buffers          318767104 bytes
Redo Buffers                5656576 bytes
Database mounted.
SQL> drop database;
Database dropped.

Tuesday, February 19, 2013

Configure SSH for Oracle Equivalence

SSH connectivity configuration provides connection among nodes without using password :

1) Generate RSA key:
As oracle:cd $HOME (mkdir ~/.ssh if does not exists)
chmod 755 ~/.ssh[oracle@jdb01 ~]$ /usr/bin/ssh-keygen -t rsaGenerating public/private rsa key pair.
Enter file in which to save the key (/home/oracle/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):                                                   << NO PASSWORD
Enter same passphrase again:                                                                          << NO PASSWORD
Your identification has been saved in /home/oracle/.ssh/id_rsa.
Your public key has been saved in /home/oracle/.ssh/id_rsa.pub.
The key fingerprint is:
4f:13:93:db:06:af:f8:c3:fb:6d:ce:cf:b3:bd:bb:91 oracle@jdb
The key's randomart image is:
+--[ RSA 2048]----+
|                           |
|           .               |
|          =               |
|           B              |
|        S + +           |
|         + +    .        |
|        ..o    E         |
|         .o  o.oo      |
|          o+.o+*X   |
+-----------------+

$ pwd
/home/oracle/.ssh
-rw------- 1 oracle dba 1675 Feb 19 11:57 id_rsa-rw-r--r-- 1 oracle dba  397 Feb 19 11:57 id_rsa.pub
-rw-r--r-- 1 oracle dba  404 Feb 19 11:37 known_hosts

2) Generate DSA key:
oracle@jdb01 ~]$ /usr/bin/ssh-keygen -t dsaGenerating public/private dsa key pair.
Enter file in which to save the key (/home/oracle/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):                                                 << NO PASSWORD
Enter same passphrase again:                                                                        << NO PASSWORD
Your identification has been saved in /home/oracle/.ssh/id_dsa.
Your public key has been saved in /home/oracle/.ssh/id_dsa.pub.
The key fingerprint is:
12:dc:f8:09:43:b1:b5:6d:4f:00:b6:a0:bf:89:c3:18 oracle@jdb
The key's randomart image is:
+--[ DSA 1024]----+
|      +.+..                |
|     + B + .             |
|    . B + o .            |
|     . = o o             |
|  E   o S   .            |
|   + . +                  |
|  . + o                   |
|     .                      |
|                            |
+-----------------+
$ pwd
/home/oracle/.ssh
-rw------- 1 oracle dba  668 Feb 19 12:01 id_dsa-rw-r--r-- 1 oracle dba  605 Feb 19 12:01 id_dsa.pub

Primary or RAC Node1:
cat ~/.ssh/id_rsa.pub >> authorized-keys
cat ~/.ssh/id_dsa.pub >> authorized-keys

scp authorized-keys node2:/home/oracle/.ssh

Stabdby DB or RAC Node2:
Repeate the rsa and dsa ket generation step
create a authorization-keys file and append all keys info

3) Test connection:
ssh  hostname1
or
ssh hostname2


Wednesday, February 13, 2013

ORACLE - Client Issue - While loading shared libraries: libsqlplus.so

Issue : We are getting following error messages while using Oracle’s SQL*Plus client on Linux.
To avoid these  messages, we need to set correct environment variables :


Error messages: 
servername01 sql $ sqlplus jdb1/password@sconnect_string
sqlplus: error while loading shared libraries: libsqlplus.so: cannot open shared object file: No such file or directory


Verify:
servername01 ~ $ locate libsqlplus.so/u01/app/oracle/product/11.1.0/client/lib/libsqlplus.so

Solution:
1) Add following env variable into oracle .profile or .bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
# User specific environment and startup programs
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/client
export LD_LIBRARY_PATH=/u01/app/oracle/product/11.1.0/client/lib
export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/lib:$PATH:$HOME/.local/bin:$HOME/bin

OR

2) Linux server:  For all User - Add following environment variable into /etc/profile:
 export LD_LIBRARY_PATH=/u01/app/oracle/product/11.1.0/client/lib
export ORACLE_BASE=/u01/app/oracle
export PATH=/u01/app/oracle/product/11.1.0/client/bin:/u01/app/oracle/product/11.1.0/client/lib:/u01/app/oracle/product/11.1.0/client/jdk/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/usr/java/jdk1.5.0_05/bin:/opt/dell/srvadmin/bin:/home/oracle/bin:$PATH:$HOME/bin
export ORACLE_HOME=/u01/app/oracle/product/11.1.0/client

Monday, February 4, 2013

ORACLE - ADR

What is ADR ?
The Automatic Diagnostic Repository (ADR) is a file-based repository for storing diagnostic data.
Because this repository is stored outside the database, the diagnostic data is available even when the database is down. As of Release 11g, the alert log, all trace and dump files, and other diagnostic data are also stored in the ADR.

[JDB01] oracle@jdbdb01:/home/oracle> adrci
ADRCI: Release 11.2.0.3.0 - Production on Mon Feb 4 09:44:36 2011
Copyright (c) 1982, 2011, Oracle and/or its affiliates.  All rights reserved.
ADR base = "/u01/app/oracle"
adrci> help
adrci> show incident
ADR Home = /u01/app/oracle/diag/tnslsnr/jdbdb01/listener_asm:
*************************************************************************
0 rows fetched
ADR Home = /u01/app/oracle/diag/tnslsnr/jdbdb01/listener:
*************************************************************************
0 rows fetched
ADR Home = /u01/app/oracle/diag/tnslsnr/jdbdb01/listener_JDB01:
*************************************************************************
0 rows fetched
ADR Home = /u01/app/oracle/diag/asm/+asm/+ASM:
*************************************************************************
0 rows fetched
ADR Home = /u01/app/oracle/diag/rdbms/JDB01/JDB01:
*************************************************************************
INCIDENT_ID          PROBLEM_KEY                                                 CREATE_TIME
-------------------- ----------------------------------------------------------- ----------------------------------------
31641     ORA 600 [kkdlGetBaseUser2:authIdType]         2012-12-17 16:35:42.023000 -05:00
31649     ORA 600 [kkdlGetBaseUser2:authIdType]         2012-12-17 16:35:42.023000 -05:00
31697     ORA 600 [kkdlGetBaseUser2:authIdType]         2012-12-17 16:35:42.023000 -05:00
31673     ORA 600 [kkdlGetBaseUser2:authIdType]         2012-12-17 16:35:42.023000 -05:00
31705     ORA 600 [kkdlGetBaseUser2:authIdType]         2012-12-17 16:35:42.048000 -05:00
31637     ORA 600 [kokuxpout3]                                        2011-01-10 12:27:33.189000 -05:00
31638     ORA 600 [kokuxpout3]                                        2011-01-10 12:35:01.472000 -05:00
31639     ORA 600 [kokuxpout3]                                        2011-01-10 13:25:15.446000 -05:00
31640      ORA 600 [kokuxpout3]                                        2011-01-10 13:27:28.127000 -05:00
35203      ORA 600 [kokuxpout3]                                        2011-01-10 13:29:28.606000 -05:00
10 rows fetched

adrci> show homes
ADR Homes:
diag/tnslsnr/jdbdb01/listener_asm
diag/tnslsnr/jdbdb01/listener
diag/tnslsnr/jdbdb01/listener_JDB01
diag/asm/+asm/+ASM
diag/rdbms/JDB01/JDB01

adrci> set home diag/rdbms/JDB01/JDB01

adrci> ips create package
Created package 1 without any contents, correlation level typical

adrci> ips add incident 31641 package 1
Added incident 31641 to package 1

adrci> show incident -mode detail -P "incident_id=31641"

adrci> ips generate package 1 in /tmpGenerated package 1 in file /tmp/IPSPKG_20130204100436_COM_1.zip, mode complete
 >>> send to ORACLE SUPPORT

adrci> show problemADR Home = /u01/app/oracle/diag/rdbms/jdb01/JDB01:
*************************************************************************
PROBLEM_ID           PROBLEM_KEY                    LAST_INCIDENT        LASTINC_TIME           
-------------------- ----------------------------------------------------------- -------------------- ----------------------------
1         ORA 353 [67584] [68050001]        20994                2013-01-22 08:10:08.191000 -05:00
2         ORA 353 [104448] [68159717]      20995                2013-01-22 09:10:53.548000 -05:00
3         ORA 353 [79872] [68172859]        20996                2013-01-22 10:10:13.847000 -05:00
4         ORA 353 [118784] [68177937]      20997                2013-01-22 11:10:14.386000 -05:00
5         ORA 353 [258048] [71472299]      20999                2013-01-23 06:10:03.496000 -05:00
5 rows fetched


adrci> show problemADR Home = /u01/app/oracle/diag/rdbms/jdb01/JDB01:
*************************************************************************
PROBLEM_ID           PROBLEM_KEY                      LAST_INCIDENT        LASTINC_TIME           
-------------------- ----------------------------------------------------------- -------------------- ----------------------------1         ORA 353 [67584] [68050001]            20994                2013-01-22 08:10:08.191000 -05:00
2         ORA 353 [104448] [68159717]          20995                2013-01-22 09:10:53.548000 -05:00
3         ORA 353 [79872] [68172859]            20996                2013-01-22 10:10:13.847000 -05:00
4         ORA 353 [118784] [68177937]          20997                2013-01-22 11:10:14.386000 -05:00
5         ORA 353 [258048] [71472299]          20999                2013-01-23 06:10:03.496000 -05:00
5 rows fetched


adrci> IPS CREATE PACKAGE PROBLEM 1;Created package 2 based on problem id 1, correlation level typical
or
adrci> IPS CREATE PACKAGE PROBLEMKEY "ORA 353 [67584] [68050001]"Created package 1 based on problem key ORA 353 [67584] [68050001], correlation level typical


adrci> show control
ADR Home = /u01/app/oracle/diag/rdbms/qntp01/QNTP01:
*************************************************************************
ADRID                SHORTP_POLICY        LONGP_POLICY         LAST_MOD_TIME                            LAST_AUTOPRG_TIME                        LAST_MANUPRG_TIME                        ADRDIR_VERSION       ADRSCHM_VERSION      ADRSCHMV_SUMMARY     ADRALERT_VERSION     CREATE_TIME
-------------------- -------------------- -------------------- ---------------------------------------- ---------------------------------------- ---------------------------------------- -------------------- -------------------- -------------------- -------------------- ----------------------------------------
1807762298           720                  8760                 2012-12-01 23:14:57.437655 -05:00        2013-01-30 15:12:42.603362 -05:00                                                 1                    2                    80                   1                    2012-12-01 23:14:57.437655 -05:00
1 rows fetched

LONGP_POLICY (long term) defaults to 365 (8760 hrs) days for  Incidents and Health Monitor warnings
SHORTP_POLICY (short term) defaults to 30 (720 hrs) days for trace and core dump files


Setup auto purge - Purge after 60 Hrs:
adrci> set control (SHORTP_POLICY = 60)
adrci> set control (LONGP_POLICY = 60)

Manual Purge:
purge -age 129600 -type ALERT ( purging ALERT older than 90 days)
purge -age 43200 -type INCIDENT (purging INCIDENT older than 30 days)
purge -age 43200 -type TRACE ( purging TRACE older than 30 days)
purge -age 43200 -type CDUMP ( purging CDUMP older than 30 days)
purge -age 43200 -type HM ( purging HM older than 30 days)

or
$ORACLE_HOME/bin/adrci exec="set base /data/$DB;set homepath $file_line;purge -age 129600 -type ALERT"

select * from v$diag_info ;   INST_ID NAME                  VALUE
---------- -----------------------------------------------------------------------
         1 Diag Enabled          TRUE
         1 ADR Base                 /u01/app/oracle
         1 ADR Home               /u01/app/oracle/diag/rdbms/hmwhist/JDB
         1 Diag Trace               /u01/app/oracle/diag/rdbms/JDB/JDB/trace
         1 Diag Alert                /u01/app/oracle/diag/rdbms/JDB/JDB/alert
         1 Diag Incident           /u01/app/oracle/diag/rdbms/JDB/JDB/incident
         1 Diag Cdump            /u01/app/oracle/diag/rdbms/JDB/JDB/cdump
         1 Health Monitor        /u01/app/oracle/diag/rdbms/JDB/JDB/hm
         1 Default Trace File   /u01/app/oracle/diag/rdbms/JDB/JDB/trace/JDB_ora_13742.trc
         1 Active Problem Count    0
         1 Active Incident Count     0
11 rows selected.


SELECT run_id, name, check_name, run_mode, status, error_number FROM gv$hm_run;
RUN_ID NAME       CHECK_NAME              RUN_MODE STATUS      ERROR_NUMBER
---------- -------------------------------- -------------------------------- -------- ----------- ------------
     35161 HM_RUN_35161          Redo Integrity Check             REACTIVE COMPLETED              0
     35201 HM_RUN_35201          Redo Integrity Check             REACTIVE COMPLETED              0
     35941 HM_RUN_35941          Redo Integrity Check             REACTIVE COMPLETED              0
     35981 HM_RUN_35981          Redo Integrity Check             REACTIVE COMPLETED              0
.....................
2380 rows selected.



ORACLE - FDA : Flashback Data Archive

FDA holds historical data. It is "ORACLE TOTAL RECALL" option.

Requirements:
1)UNDO management must be automatic.
2)Tablespace for FDA must be ASSM (Automatic Segment Space Management).
3) Enable the FDA for a table (Oracle will create a internal history table for that table)
4) FBDA - new background process will determined intervals (default 5 min) and copied the UNDO data into history tables. Thus UNDO data marked for archiving are not re-used by the database until it stored by the FBDA into history table.

Setup the FDA:
1) Grant FDA role to user:
select * from dba_sys_privs where privilege like '%FLASHBACK ARC%';
grant flashback archive on j_table to  jay;

2) Create FDA table-space:
create FLASHBACK ARCHIVE  [DEFAULT] j_tbs
Tablespace fda_archive  [mandatory - must be ASSM]
Quota 5G [optional]
RETENTION 24 monthl [mandatory - year, month, day]

3)Enable and alter FDA on table level:
create table j_table (col number) FLASHBACK ARCHIVE;
alter table j_table FLASHBACK ARCHIVE; [deault archiving ]
alter table j_table FLASHBACK ARCHIVE j_tbs; [ non-deault archiving ]
alter table j_table NO FLASHBACK ARCHIVE; [disable archiving]

alter FLASHBACK ARCHIVE jflash SET DEFAULT;
alter FLASHBACK ARCHIVE jflash ADD[REMOVE] TABLESPACE j_tbs2;[Add/remove tablespace]
alter FLSHBACK ARCHIVE jflash MODIFY TABLESPACE QUOTA 8G;
alter FLSHBACK ARCHIVE jflash MODIFY TABLESPACE j_tbs; [Unlimited tablespace]
alter FLSHBACK ARCHIVE jflash MODIFY RETENTION 2 YEAR;
alter FLSHBACK ARCHIVE jflash PURGE BEFORE TIMESTAMP (SYSTIMESTAMP - INTERVAL '3' DAY);
alter FLSHBACK ARCHIVE jflash  PURGE ALL:

4) Query the FDA:
select salary from jay.j_table AS OF TIMESTAMP TO_TIMESTAMP ()
select salary from jay.j_table AS OF TIMESTAMP (SYSTIMESTAMP - INTERVAL '3' MONTH);
FALSHBACK TABLE j_table to TIMESTAMP (SYSTIMESTAMP - INTERVAL '3' MONTH);

5) Query thd FDA dba tables:
select * from DBA_FLASHBACK_ARCHIVE;
select * from DBA_FLASHBACK_ARCHIVE_TS;
select table_name, owner_name, flashback_archive_name from DBA_FLASHBACK_ARCHIVE_TABLES;

6) Most of the Limitation for FDA table has been removed in 11gR2:
Oracle Database 11g Release 2 (11.2) users can now use most DDL commands on tables that are being tracked with Flashback Data Archive. This includes:
  • Add, Drop, Rename, Modify Column
  • Drop, Truncate Partition
  • Rename, Truncate Table
  • Add, Drop, Rename, Modify Constraint



Sunday, February 3, 2013

ORACLE - Standby Redo logfile corrupte


Issue: (  ORA-00261 and ORA-00313 ):
At standby database  dba found that standby logfile group 56 was corrupted.
RFS[11]: Assigned to RFS process 14642
RFS[11]: Identified database type as 'physical standby'
Wed Nov 07 15:31:34 2012
Errors in file /u02/oracle/jdb/logs/bdump/JDBdr3_arc2_12269.trc:
ORA-00313: open failed for members of log group 56 of thread 3
ORA-00312: online log 56 thread 3: '+JDB_DATA/JDBdr/onlinelog/group_56.268.720898269'
ORA-17503: ksfdopn:2 Failed to open file +JDB_DATA/JDBdr/onlinelog/group_56.268.720898269
ORA-15012: ASM file '+JDB_DATA/JDBdr/onlinelog/group_56.268.720898269' does not exist
Wed Nov 07 15:31:35 2012
Errors in file /u02/oracle/jdb/logs/bdump/JDBdr3_arc2_12269.trc:
ORA-00314: log 56 of thread 3, expected sequence# 56253 doesn't match 56242
ORA-00312: online log 56 thread 3: '+JDB_FLASH/JDBdr/onlinelog/group_56.3289.720898269'
Primary database is in MAXIMUM PERFORMANCE mode
Wed Nov 07 15:31:35 2012

or


Errors in file /u02/oracle/jdb/logs/udump/fipsldr1_rfs_12914.trc:
ORA-00261: log 34 of thread 3 is being archived or modified
ORA-00312: online log 34 thread 3: '+JDATA_DATA'
ORA-00312: online log 34 thread 3: '+JDATA1_FLASH'
Mon Aug  8 02:02:20 2011
Errors in file /u02/oracle/jdb/logs/udump/fipsldr1_rfs_12914.trc:
ORA-00261: log 34 of thread 3 is being archived or modified
ORA-00312: online log 34 thread 3: '+JATA1_DATA/fipsldr/onlinelog/group_34.297.758599339'
ORA-00312: online log 34 thread 3: '+JDATA1_FLASH'

Solution:
On Standby Database

SQL> alter system set standby_file_management = 'MANUAL';
System altered.

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL;
Database altered.

SQL> ALTER DATABASE ADD STANDBY LOGFILE THREAD 1 SIZE 100m;
Database altered.

SQL> ALTER DATABASE ADD STANDBY LOGFILE THREAD 2 SIZE 100m;
Database altered.

SQL> ALTER DATABASE ADD STANDBY LOGFILE THREAD 3 SIZE 100m;
Database altered.

SQL> alter database drop standby logfile group 56;
alter database drop standby logfile group 56
*
ERROR at line 1:
ORA-00261: log 56 of thread 3 is being archived or modified
ORA-00312: online log 56 thread 3:
'+JDATA/mipsldr/onlinelog/group_56.268.720898269'
ORA-00312: online log 56 thread 3:
'+JFLASH/mipsldr/onlinelog/group_56.3289.720898269'


Note: alter system log switch at primary  
You can not drop a logfile group with status is
CLEARING_CURRENT

SQL>  alter database drop standby logfile group 56; 
Database altered.

SQL> alter system set standby_file_management = 'AUTO';
System altered.

SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE using current logfile disconnect from session;
Database altered.