Different scenerios on restoring spfile
Restore spfile through RMAN, here are some scenerios/examples.
CONTROLFILE AUTOBACKUP explained here. In the case you don’t have “CONTROLFILE AUTOBACKUP” turned on you would need to specify the backup peice which contains the spfile.
1) In this scenerio there is a autobackup is present of spfile, database is no mount state so to use AUTOBACKUP DBID needs to be set before restoring spfile
$ $ORACLE_HOME/bin/rman
Recovery Manager: Release 10.2.0.1.0 – Production on Sun Nov 28 14:40:05 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
RMAN> connect target /
connected to target database: TEST (not mounted)
RMAN> set dbid 1992878807
executing command: SET DBID
RMAN> restore spfile from autobackup;
Starting restore at 28-NOV-10
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=37 devtype=DISK
channel ORA_DISK_1: looking for autobackup on day: 20101128
channel ORA_DISK_1: autobackup found: c-1992878807-20101128-00
channel ORA_DISK_1: SPFILE restore from autobackup complete
Finished restore at 28-NOV-10
In this scenerio there spfile is restored from autobackup, database is in nomount state and if the filename of backup piece is known so it can be passed to restore spfile
$ $ORACLE_HOME/bin/rman
Recovery Manager: Release 10.2.0.1.0 – Production on Mon Nov 29 17:45:04 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
RMAN> connect target /
connected to target database: TEST (not mounted)
– restoring spfile from a specific backup piece using full path or can just be the name
RMAN> restore spfile from '/u01/oracle/product/10.2.0/db_1/dbs/c-1992878807-20101128-00';
– RMAN> restore spfile from 'c-1992878807-20101128-00';
Starting restore at 28-NOV-10
using channel ORA_DISK_1
channel ORA_DISK_1: autobackup found: /u01/oracle/product/10.2.0/db_1/dbs/c-1992878807-20101128-00
channel ORA_DISK_1: SPFILE restore from autobackup complete
Finished restore at 28-NOV-10
Scenerio where the SPFILE is already there and database was started with spfile, Oracle will not allow to overwrite the file unless “TO” clause is specified to overwite with a different name, RMAN will generate the following RMAN-06564 error
$ $ORACLE_HOME/bin/rman
Recovery Manager: Release 10.2.0.1.0 – Production on Sun Nov 28 14:40:05 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
RMAN> connect target /
connected to target database: TEST (not mounted)
RMAN> set dbid 1992878807
executing command: SET DBID
RMAN> restore spfile from autobackup;
Starting restore at 28-NOV-10
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=35 devtype=DISK
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/28/2010 15:06:34
RMAN-06564: must use the TO clause when the instance is started with SPFILE
Scenerio where there is no autobackup of control file so Oracle goes to last 7 days by default and does not find it
$ $ORACLE_HOME/bin/rman
Recovery Manager: Release 10.2.0.1.0 – Production on Sun Nov 28 14:40:05 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
RMAN> connect target /
connected to target database: TEST (not mounted)
RMAN> set dbid 1992878807
executing command: SET DBID
RMAN> restore spfile from autobackup;
If you want to tell RMAN to look for spfile more than 7 days so using maxdays RMAN will look for spfile from current date to currentday – maxdays “RMAN> restore spfile from autobackup maxdays 200;”
Starting restore at 28-NOV-10
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=35 devtype=DISK
channel ORA_DISK_1: looking for autobackup on day: 20101128
channel ORA_DISK_1: looking for autobackup on day: 20101127
channel ORA_DISK_1: looking for autobackup on day: 20101126
channel ORA_DISK_1: looking for autobackup on day: 20101125
channel ORA_DISK_1: looking for autobackup on day: 20101124
channel ORA_DISK_1: looking for autobackup on day: 20101123
channel ORA_DISK_1: looking for autobackup on day: 20101122
channel ORA_DISK_1: no autobackup in 7 days found
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/28/2010 15:08:30
RMAN-06172: no autobackup found or specified handle is not a valid copy or piece
Restoring spfile but not connected to the target database it will return error RMAN-06171
$ $ORACLE_HOME/bin/rman
Recovery Manager: Release 10.2.0.1.0 – Production on Sun Nov 28 14:40:05 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
RMAN> set dbid 1992878807;
executing command: SET DBID
RMAN> restore spfile from autobackup;
Starting restore at 28-NOV-10
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/28/2010 15:08:17
RMAN-12010: automatic channel allocation initialization failed
RMAN-06171: not connected to target database
Scenerio where restoring spfile through autobackup and DBID is not set, it generates an error message RMAN-06495
$ $ORACLE_HOME/bin/rman
Recovery Manager: Release 10.2.0.1.0 – Production on Mon Nov 29 17:55:13 2010
Copyright (c) 1982, 2005, Oracle. All rights reserved.
RMAN> connect target /
connected to target database: TEST (not mounted)
RMAN> restore spfile from autobackup;
Starting restore at 29-NOV-10
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: sid=39 devtype=DISK
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/29/2010 17:55:25
RMAN-06495: must explicitly specify DBID with SET DBID command
CONTROLFILE AUTOBACK explained
Filed under: Backup & Recovery, Control file, General DBA, RMAN
When AUTOBACKUP is turned on Oracle will create backup of controlfile and spfile. When one runs backup of the database or archive log, changes in tablespace occur like creating a new tablespace, add a new datafile and dropping tablespace Oracle create a separate backup piece for the control file and spfile.
Shows how to turn on CONTROLFILE AUTOBACKUP
RMAN> connect target /
connected to target database: TEST (DBID=1992878807)
RMAN> configure CONTROLFILE AUTOBACKUP ON;
old RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP OFF;
new RMAN configuration parameters:
CONFIGURE CONTROLFILE AUTOBACKUP ON;
new RMAN configuration parameters are successfully stored
to turn on controlfile autobackup through PL/SQL and setting the format
SQL> VARIABLE RECNO NUMBER;
SQL> EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('CONTROLFILE AUTOBACKUP','ON');
SQL> EXECUTE :RECNO := SYS.DBMS_BACKUP_RESTORE.SETCONFIG('CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE','DISK TO ''T_%F''');
The default format of the filename of autoback is '%F' which is “c—” which is controlled “CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '%F'”
The following link shows the format options http://ss64.com/ora/rman_format_string.html
Shows when a new tablespace is created, new datafile is added or tablespace dropped in the alert log it will record full path to the backup piece of control file and spfile.
Mon Nov 29 19:30:14 2010
create tablespace test datafile '/u01/oradata/TEST/test.dbf' size 100M
Mon Nov 29 19:30:17 2010
Starting control autobackup
Control autobackup written to DISK device
handle '/u01/oracle/product/10.2.0/db_1/dbs/c-1992878807-20101129-01'
Completed: create tablespace test datafile '/u01/oradata/TEST/test.dbf' size 100M
…
Mon Nov 29 19:33:35 2010
alter tablespace test rename to test1
Mon Nov 29 19:33:35 2010
Tablespace 'TEST' is renamed to 'TEST1'.
Starting control autobackup
Control autobackup written to DISK device
handle '/u01/oracle/product/10.2.0/db_1/dbs/c-1992878807-20101129-02'
Completed: alter tablespace test rename to test1
Mon Nov 29 19:43:29 2010
alter tablespace test1 add datafile '/u01/oradata/TEST/test2.dbf' SIZE 100M
Mon Nov 29 19:43:32 2010
Starting control autobackup
Control autobackup written to DISK device
handle '/u01/oracle/product/10.2.0/db_1/dbs/c-1992878807-20101129-03'
Completed: alter tablespace test1 add datafile '/u01/oradata/TEST/test2.dbf' SIZE 100M
Mon Nov 29 20:07:10 2010
drop tablespace test1 including contents and datafiles
Mon Nov 29 20:07:11 2010
Deleted file /u01/oradata/TEST/test.dbf
Deleted file /u01/oradata/TEST/test2.dbf
Starting control autobackup
Control autobackup written to DISK device
handle '/u01/oracle/product/10.2.0/db_1/dbs/c-1992878807-20101129-04'
Completed: drop tablespace test1 including contents and datafiles
How to restore and recover tablespace?
Steps if you need to restore all datafiles in a tablespace is corrupted so you would need to restore the table space. The following example shows the steps
– Shows cannot restore the tablespace yet till the tablespace is online
RMAN> run {
2> restore tablespace ts_something;
3> }
Starting restore at 27-NOV-10
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00004 to /u01/oradata/TEST/test_01.dbf
channel ORA_DISK_1: reading from backup piece /u01/oracle/product/10.2.0/db_1/dbs/1nlu32je_1_1
RMAN-00571: ===========================================================
RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
RMAN-00571: ===========================================================
RMAN-03002: failure of restore command at 11/27/2010 20:33:45
ORA-19870: error reading backup piece /u01/oracle/product/10.2.0/db_1/dbs/1nlu32je_1_1
ORA-19573: cannot obtain exclusive enqueue for datafile 4
– tablespace cannot be made offline as it will try to flush all blocks in the datafiles but as the datafile is corrupted.
SQL> alter tablespace ts_something offline;
alter tablespace ts_something offline
*
ERROR at line 1:
ORA-01116: error in opening database file 4
ORA-01110: data file 4: '/u01/oradata/TEST/test_01.dbf'
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
– So using table space offline immediate Oracle will not check if the file exists and does not perform checkpoint and we need to do media recovery on the table space
SQL> alter tablespace ts_something offline immediate;
Tablespace altered.
– Display datafile status
SQL> select d.name, d.status
from v$datafile d, v$tablespace t
where t.name = 'TS_SOMETHING'
and t.ts# = d.ts#;
NAME STATUS
—————————————————————— ——-
/u01/oradata/TEST/test_01.dbf RECOVER
– restore the table space
RMAN> run {
2> restore tablespace ts_something;
3> }
Starting restore at 27-NOV-10
using channel ORA_DISK_1
channel ORA_DISK_1: starting datafile backupset restore
channel ORA_DISK_1: specifying datafile(s) to restore from backup set
restoring datafile 00004 to /u01/oradata/TEST/test_01.dbf
channel ORA_DISK_1: reading from backup piece /u01/oracle/product/10.2.0/db_1/dbs/1nlu32je_1_1
channel ORA_DISK_1: restored backup piece 1
piece handle=/u01/oracle/product/10.2.0/db_1/dbs/1nlu32je_1_1 tag=TAG20101127T202206
channel ORA_DISK_1: restore complete, elapsed time: 00:00:15
Finished restore at 27-NOV-10
– table space has be restored but not recovered so shows it cannot be made online till table space is recovered
SQL> alter tablespace ts_something online;
alter tablespace ts_something online
*
ERROR at line 1:
ORA-01113: file 4 needs media recovery
ORA-01110: data file 4: '/u01/oradata/TEST/test_01.dbf'
– recover table space
SQL> recover tablespace ts_something;
Media recovery complete.
– Tablespace can be made online
SQL> alter tablespace ts_something online;
Tablespace altered.
Shows data file status
SQL> select d.name, d.status
from v$datafile d, v$tablespace t
where t.name = 'TS_SOMETHING'
and t.ts# = d.ts#;
NAME STATUS
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— ——-
/u01/oradata/TEST/test_01.dbf ONLINE
How does cascade work in cases of delete/update for foreign key?
The following example shows how to setup and cascade works when deleting record from parent table.
1) Shows how DELETE cascade works
– Parent table
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50),
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
– children table with foreign key
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE CASCADE
);
Records inserted in the table:
insert into supplier values ( 1, ‘supplier 1′, ‘contact name 1′ );
insert into products values ( 1, 1 );
– shows when record is deleted from parent table it deletes from the table that has foreign key due to “ON DELETE CASCADE”
SQL> delete from supplier where supplier_id = 1;
1 row deleted.
– record was deleted from products table
SQL> select * from products;
no rows selected
SQL> select * from supplier;
no rows selected
SQL> commit;
Commit complete.
2) Shows how DELETE cascade SET NULL works, it sets foreign key to NULL when primary key is deleted
– Parent table
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50),
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
– children table with foreign key
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE SET NULL
);
Records inserted in the table:
insert into supplier values ( 1, ‘supplier 1′, ‘contact name 1′ );
insert into products values ( 1, 1 );
– shows when record is deleted from parent table it sets NULL values to foreign keydue to “ON DELETE SET NULL”
SQL> delete from supplier where supplier_id = 1;
1 row deleted.
SQL> select * from supplier;
no rows selected
– SUPPLIER_ID is set to NULL when the primary key record is deleted
SQL> select * from products;
PRODUCT_ID SUPPLIER_ID
———- ———–
1
SQL> commit;
Commit complete.
3) The following example shows if a table doesn’t have ON DELETE CASCADE OR INITIALLY DEFERRED
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50),
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
);
Records inserted in the table:
insert into supplier values ( 1, ‘supplier 1′, ‘contact name 1′ );
insert into products values ( 1, 1 );
– when record from parent is being deleted due to foreign key constraint it can’t be deleted till all children are deleted
SQL> delete from supplier where supplier_id = 1;
delete from supplier where supplier_id = 1
*
ERROR at line 1:
ORA-02292: integrity constraint (SCOTT.FK_SUPPLIER) violated – child record
found
– If you don’t have ON DELETE CASECADE set on the foreign key, it can be done as follows record from tables that have foreign key is deleted first then the record from parent can be deleted.
SQL> delete from products where supplier_id = 1;
1 row deleted.
SQL> delete from supplier where supplier_id = 1;
1 row deleted.
SQL> commit;
Commit complete.
4)In case when one wants updating the primary key and cascade the change to foreign key, this can be done through a trigger.
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50),
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10),
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE SET NULL
);
– The following trigger updates records that are foreign key
create or replace trigger cascade_supplier_update
after update of supplier_id on supplier
for each row
begin
update products
set supplier_id = :new.supplier_id
where supplier_id = :old.supplier_id;
end;
/
Records inserted in the table:
insert into supplier values ( 1, ‘supplier 1′, ‘contact name 1′ );
insert into products values ( 1, 1 );
– Following test shows updating the parent table, the trigger updates the dependent table and Oracle doesn’t throw exception
SQL> update supplier set supplier_id = 2 where supplier_id = 1;
1 row updated.
– shows supplier_id in products table was updated
SQL> select * FROM products;
PRODUCT_ID SUPPLIER_ID
———- ———–
1 2
5)In case when one wants updating the primary key using “INITIALLY DEFERRED”, updating the primary key Oracle will check constraint when commit is executed and check constraints during COMMIT
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50),
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);
CREATE TABLE products
( product_id numeric(10) not null,
supplier_id numeric(10),
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
ON DELETE SET NULL INITIALLY DEFERRED
);
Records inserted in the table:
insert into supplier values ( 1, ‘supplier 1′, ‘contact name 1′ );
insert into products values ( 1, 1 );
– Shows when UPDATE primary key oracle will allow the UPDATE to occur on the parent table and then one can UPDATE the foreign key
SQL> update supplier set supplier_id = 2 where supplier_id = 1;
1 row updated.
SQL> update products set supplier_id = 2 where supplier_id = 1;
1 row updated.
SQL> commit;
Commit complete.
– If one were to update the PRIMARY key but not the foreign key and COMMIT, Oracle will generate “ORA-02292″ error and the transaction will be rolled back
SQL> update supplier set supplier_id = 2 where supplier_id = 1;
1 row updated.
SQL> commit;
commit
*
ERROR at line 1:
ORA-02091: transaction rolled back
ORA-02292: integrity constraint (SCOTT.FK_SUPPLIER) violated – child record found
How to view the security status and OMS port information?
Displays OMS port information and security status
/u01/weblogic/oms11g/bin $ ./emctl status oms -details
Oracle Enterprise Manager 11g Release 1 Grid Control
Copyright (c) 1996, 2010 Oracle Corporation. All rights reserved.
Enter Enterprise Manager Root (SYSMAN) Password :
Console Server Host : calora4db01
HTTP Console Port : 7788
HTTPS Console Port : 7799
HTTP Upload Port : 4889
HTTPS Upload Port : 1159
OMS is not configured with SLB or virtual hostname
Agent Upload is unlocked.
OMS Console is unlocked.
Active CA ID: 1
So this indicates the console can be accessed via https://hostname:7799/em or htpp://hostname:7788/em
How to start/stop Oracle Enterprise Manager (OEM)/ Grid Control (GC)?
To start the Oracle Enterprise Manager/Grid Control components in version 11g (11.1.0.1)
$ cd /bin; ./emctl start oms;
$ cd /bin; ./emctl start agent
Where OMS_HOME is /u01/weblogic/oms11g and AGENT_HOME is /u01/weblogic/agent11g
Note: This will also start weblogic too
To stop Oracle Enterprise Manager version 11g (11.1.0.1)
$ cd /bin; ./emctl stop agent
$ cd /bin; ./emctl stop oms -all
Where OMS_HOME is /u01/weblogic/oms11g and AGENT_HOME is /u01/weblogic/agent11g
Note: This will also stop weblogic too
Check if any process not terminated and still running, if any found terminate the process
$ ps -ef | grep emagent
$ ps -ef | grep oms11g
$ ps -ef | grep gc_inst
Clear procedure cache (execution plan)
For SQLServer:
– Clears all the procedure cache (works on SQLServer 2005 and SQLServer 2008)
DBCC FREEPROCCACHE http://msdn.microsoft.com/en-us/library/ms174283%28v=SQL.90%29.aspx
– On SQL Server 2008 one can clear cache for a specific plan/sql by specifying the handle
[ ( { plan_handle | sql_handle | pool_name } ) ]
DBCC FREEPROCCACHE [ ( { plan_handle | sql_handle ) ] http://msdn.microsoft.com/en-us/library/ms174283%28v=SQL.105%29.aspx
For Oracle
Shared pool is where the execution plan of SQL statements are stored. Shared pool also contains parsed SQL statements, packages which will get cleared too.
SQL> alter system flush shared_pool;
How to install Oracle Enterprise Manager Grid Control 11GR1?
The following document lists step how to install EM GridControl 11GR1
http://www.whopatooli.com/disquisitivedba/library/Joe_Johnson_Installing_EM_Grid_11gR1_11.1.0.1.pdf
SQL Statement in cache, it’s execution time
– Display SQL statement (SQLServer 2005 and SQLServer 2008), it displays creation time, last exeuction time, max time it ran for in microseconds, average execution time, # of times sql statement executed
SELECT j.plan_handle
, sqltext
, deqs.creation_time
, deqs.last_execution_time
, deqs.
, deqs.total_worker_time/deqs.execution_count avg_exec_time
, deqs.execution_count
FROM (SELECT decp.plan_handle, st.text sqltext
FROM sys.dm_exec_cached_plans decp
CROSS APPLY sys.dm_exec_sql_text(plan_handle) AS st) j,
sys.dm_exec_query_stats deqs
WHERE deqs.plan_handle = j.plan_handle
Output:
Plan handle sqltext creation_time last_execution_time max_worker_time avg_exec_time execution_count
—————— ———— ——————— ————————– ———————— ——————— ———————-
0x060001006F639935B8C1E480000000000000000000000000 select * from sys.dm_exec_cached_plans 2010-11-15 21:30:28.183 2010-11-15 21:35:16.700 35 32 4
…………
How does one check DGMGRL status for database
$/home/oracle>dgmgrl sys/xxxxx
DGMGRL for Linux: Version 11.2.0.2.0 – 64bit Production
Copyright (c) 2000, 2009, Oracle. All rights reserved.
Welcome to DGMGRL, type “help” for information.
Connected.
DGMGRL> show configuration
Configuration – ELSII.FLIMATECH
Protection Mode: MaxPerformance
Databases:
ELSII_DR – Primary database
ELSII – Physical standby database
Fast-Start Failover: DISABLED
Configuration Status:
SUCCESS
DGMGRL> show database ‘ELSII’;
Database – ELSII
Enterprise Manager Name: ELSII.FLIMATECH
Role: PHYSICAL STANDBY
Intended State: APPLY-ON
Transport Lag: 0 seconds
Apply Lag: 0 seconds
Real Time Query: OFF
Instance(s):
ELSII1 (apply instance)
ELSII2
ELSII3
Database Status:
SUCCESS
DGMGRL> show database ‘ELSII_DR’;
Database – ELSII_DR
Enterprise Manager Name: ELSII_DR.FLIMATECH
Role: PRIMARY
Intended State: TRANSPORT-ON
Instance(s):
ELSII1
ELSII2
ELSII3
Database Status:
SUCCESS
DGMGRL>


