Articles
SQL & PL/SQL
![]() | Oracle Database 11g: by Arup Nanda |
Learn how Active Data Guard will make your investment in a standby environment worthwhile via real time queries while applying the archived logs, converting the physical standby database to snapshot standby, and a host of new improvements in infrastructure.
See Series TOC
There are enough Data Guard enhancements in Oracle Database 11g to fill a book, so covering every possible enhancement in detail is not possible. Rather I will cover the ones I consider most interesting here.
Let's start at the beginning: the creation of a physical standby database. In Oracle Database 11g, that process has become a whole lot easier, with just one RMAN command that does it all. Previously, you could use the Grid Control wizard interface to build a Data Guard setup between two machines. As of this writing, Oracle Enterprise Manager Grid Control 11g is not yet available and the Database Control does not have a wizard for Data Guard. But regardless of your experience in using SQL commands, you will find setting up a Data Guard environment in Oracle Database 11g a breeze. It's so simple that I can show you all the steps right here.
Suppose your primary database is called prolin11 running on a server called prolin1. You want to set up the standby database on a server named prolin2. The name of the standby database instance should be pro11sb. Here are the steps:
SQL> create spfile from pfile;
SQL> alter database add standby logfile group 4 2> (‘+DG1/sby_redo01.rdo') size 50M; SQL> alter database add standby logfile group 5 2> (‘+DG1/sby_redo02.rdo') size 50M; SQL> alter database add standby logfile group 6 2> (‘+DG1/sby_redo03.rdo') size 50M; SQL> alter database add standby logfile group 7 2> (‘+DG1/sby_redo04.rdo') size 50M;
SID_LIST_LISTENER = (SID_LIST = (SID_DESC = (GLOBAL_DBNAME = pro11sb) (ORACLE_HOME = /opt/oracle/product/11g/db1) (SID_NAME = pro11sb) ) ) LISTENER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = prolin2)(PORT = 1521)) )
PRO11SB = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = prolin2)(PORT = 1521)) ) (CONNECT_DATA = (SID = pro11sb) ) )
db_name=prolin11
$ sqlplus / as sysdba SQL> startup nomount
connect target sys/oracle123@prolin11
connect auxiliary sys/oracle123@pro11sb
run {
allocate channel c1 type disk;
allocate auxiliary channel s1 type disk;
duplicate target database
for standby
from active database
dorecover
spfile
parameter_value_convert 'prolin11','pro11sb'
set db_unique_name='pro11sb'
set db_file_name_convert='/prolin11/','/pro11sb/'
set log_file_name_convert='/prolin11/','/pro11sb/'
set control_files='/oradata/pro11sb/control01.ctl'
set fal_client='pro11sb'
set fal_server='prolin11'
set standby_file_management='AUTO'
set log_archive_config='dg_config=(prolin11,pro11sb)'
set log_archive_dest_2='service=prolin11 LGWR ASYNC valid_for=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=pro11sb'
set log_archive_dest_state_2='enable'
set log_archive_format='pro11sb_%t_%s_%r.arc'
;
sql channel c1 "alter system archive log current";
sql channel s1 "alter database recover managed standby database using current logfile disconnect";
}
The two lines below connect to the primary and the standby instances:
connect target sys/oracle123@prolin11; connect auxiliary sys/oracle123@pro11sb;
duplicate target database for standby from active database spfile parameter_value_convert 'prolin11','pro11sb' set 'db_unique_name'='pro11sb' set 'db_file_name_convert'='/prolin11/','/pro11sb/' ... and so on ...
One of the traditional objections to building a Data Guard environment using physical standby database is the passiveness of the standby database. In Oracle Database 10g and below you could open the physical standby database for read-only activities (say, to offload some reporting), but only after stopping the recovery process. With these releases, If Data Guard is a part of your DR solution, you really can't afford to pause the recovery process for a long duration for fear of falling behind, so the physical standby database is essentially useless for any read-only activity.
With Oracle Database 11g, that situation changes: You can open the physical standby database in read-only mode and restart the recovery process. This means you can continue to be in sync with primary but can use the standby for reporting. (As in previous versions, you can take the backup from the standby as well.) Let's see how it is done.
First, cancel the managed standby recovery:
SQL> alter database recover managed standby database cancel; Database altered.
SQL> alter database open read only; Database altered.
SQL> alter database recover managed standby database disconnect; Database altered.
SQL> alter system switch logfile; System altered. SQL> select max(Sequence#) from v$log; MAX(SEQUENCE#) -------------- 79The log switch occurred while the standby was opened in read only mode. Check the maximum log sequence in standby:
SQL> select max(Sequence#) from v$log; MAX(SEQUENCE#) -------------- 79
SQL> create table test2 (col1 number); Table created.
SQL> desc test2 Name Null? Type ----------------------------------------- -------- --------------------------- COL1 NUMBER
Remember, we could have used Real Time Apply in this case, which causes the changes made to the primary appear instantly on the standby, provided the network is available? RTA is not an absolute necessity for ADG but makes the ADG even more useful as you can expect to see the latest changes on the primary.
Security conscious readers might be little concerned however. The database is in read only mode, so nothing can be written to it. If the audit_trail parameter is set to DB on the primary (the default in Oracle Database 11g), it will be the same on standby as well, but the audit trails can't be written to the database since it's read only. So where do they go?
Note a line that shows up innocuously in alert log:
AUDIT_TRAIL initialization parameter is changed to OS, as DB is NOT compatible for database opened with read-only access
Here's a typical scenario: Say a new application is being deployed on the database and you are wondering about the impact on database performance. In Oracle Database 11g, there is that perfect tool ( Database Replay), which captures the SQL statements and replays them, but there is a caveat: you have to run them to see the impact. You capture from a test system but replaying on the production system is not feasible. First, it's not deployed; and second, even if it were deployed, you couldn't afford to have the app making changes to other tables. So what do you do to see the impact of the app?
The perfect answer awaits you in Oracle Database 11g, where the physical standby database can be temporarily converted into an up dateable one called Snapshot Standby Database. In that mode, you can run your app—which may modify a lot of tables—and gauge its impact. Once the impact is assessed, you can convert the database into a standby undergoing the normal recovery. This is accomplished by creating a restore point in the database, using the Flashback database feature to flashback to that point and undo all the changes. Let's see how it is done:
First, start recovery on the standby, if not going on already:
SQL> alter database recover managed standby database disconnect; Database altered.
SQL> alter database recover managed standby database cancel; Database altered.
ORA-38784: Cannot create restore point 'SNAPSHOT_STANDBY_REQUIRED_01/12/2008 00:23:14'. ORA-38786: Flash recovery area is not enabled.
SQL> alter system set db_recovery_file_dest_size = 2G; System altered. SQL> alter system set db_recovery_file_dest= '/db_recov'; System altered.
SQL> alter database convert to snapshot standby; Database altered.
SQL> shutdown immediate ORA-01507: database not mounted ... ORACLE instance shut down. SQL> startup ORACLE instance started.
SQL> select open_mode, database_role 2 from v$database; OPEN_MODE DATABASE_ROLE ---------- ---------------- READ WRITE SNAPSHOT STANDBY
After your testing is completed, you would want to convert the snapshot standby database back to a regular physical standby database. Just follow the steps shown below:
SQL> connect / as sysdba Connected. SQL> shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount ORACLE instance started. ... Database mounted. SQL> alter database convert to physical standby; Database altered.
SQL> shutdown ORA-01507: database not mounted ORACLE instance shut down. SQL> startup mount ORACLE instance started. ... Database mounted.
SQL> alter database recover managed standby database disconnect;
Snapshot standby database allows you to use the standby database to accurately predict changes to production database before they were made. But that's not it; there is another advantage as well. Remember, we could have used RTA in this case, which causes the changes made to the primary appear instantly on the standby, provided the network is available? Well, what if someone makes a mistake on the primary database, such as running a massive update or changing some code? In previous versions we deliberately use a delay in the standby database to stop these errors propagating to the standby. But that delay also means the standby can't be activated properly or be used as an active copy of production.
Not anymore. Since you can flashback the standby database, you need not keep the delay. If there is a problem, you can always flashback to a previous state.
You can now easily convert a physical standby database to a logical one. Here are the steps:
SQL> begin 2 dbms_logstdby.build; 3 end; 4 / PL/SQL procedure successfully completed.
SQL> alter database recover managed standby database cancel; Database altered.
SQL> alter database recover to logical standby pro11sb; Database altered.
SQL> alter system switch logfile; System altered.
RFS[12]: Identified database type as 'logical standby'
SQL> shutdown ORA-01507: database not mounted ORACLE instance shut down. SQL> startup mount ORACLE instance started. Total System Global Area 1071333376 bytes ... Database mounted. SQL> alter database open resetlogs; Database altered.
SQL> alter database start logical standby apply immediate;
It's no secret that one of the pain points in a DBA's job is to justify the need to shut down the database for a reasonably long period of time to perform upgrades. In Oracle Database 11g, this has become considerably easier if you have a standby database of any type via this rolling upgrade process:
However, many standby databases are physical in nature for ease of use and management. If the standby is not logical but physical, then the steps are pretty much the same, with a slight difference: you need to convert the standby to logical temporarily and then convert back to physical standby. The keyword is temporarily, not permanently; therefore you issue the conversion command with the new clause "keep identity", as shown below:
SQL> alter database recover to logical standby keep identity; Database altered.
There are some significant improvements in the Data Guard process itself:
Data Guard is premised on shipping the archived logs from the primary to the standby database server and applying them to the database. One of the key components of the time lag between the primary and standby is the time to transport the archived logs. This can be somewhat expedited If the redo stream is compressed.
In Oracle Database 11g you can compress the redo stream that goes across to the standby server via SQL*Net using a parameter compression set to true. This works only for the logs shipped during the gap resolution. Here is the command you can use to enable compression in the example shown in the beginning of this installment.
alter system set log_archive_dest_2 = 'service=pro11sb LGWR ASYNC valid_for=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=pro11sb compression=enable'
The Data Guard environment works by sending the redo data to the standby server by connecting to the database instance there. If the instance does not respond in time, the log shipping service will wait for a specified timeout value and then give up. This timeout value can be set in Oracle Database, using a parameter called net_timeout. In maximum protection mode, the log shipping service will retry for 20 times before giving up.
But first you have to know who much delay is currently present in the log shipping. A new view v$redo_dest_resp_histogram shows that time in histograms of values:
SQL> desc v$redo_dest_resp_histogram Name Null? Type ---------------------- ------- -------------- DEST_ID NUMBER TIME VARCHAR2(20) DURATION NUMBER FREQUENCY NUMBER
alter system set log_archive_dest_2 = 'service=pro11sb LGWR ASYNC valid_for=(ONLINE_LOGFILES,PRIMARY_ROLE) db_unique_name=pro11sb compression=enable net_timeout=20'
In the process of operating a logical standby database environment, you will need to tune the process and tweak some parameter values. In Oracle Database 11g, most of these parameters can be updated online. You can find them by querying the view dba_logstdby_parameters.
col name format a30 col value format a10 col unit format a10 col setting a6 col setting format a6 col dynamic format a7 select * from dba_logstdby_parameters order by name / NAME VALUE UNIT SETTIN DYNAMIC ------------------------------ ---------- ---------- ------ ------- APPLY_SERVERS 5 SYSTEM YES EVENT_LOG_DEST DEST_EVENT SYSTEM YES S_TABLE LOG_AUTO_DELETE TRUE SYSTEM YES LOG_AUTO_DEL_RETENTION_TARGET 1440 MINUTE SYSTEM YES MAX_EVENTS_RECORDED 10000 SYSTEM YES MAX_SERVERS 9 SYSTEM YES MAX_SGA 30 MEGABYTE SYSTEM YES PREPARE_SERVERS 1 SYSTEM YES PRESERVE_COMMIT_ORDER TRUE SYSTEM NO RECORD_APPLIED_DDL FALSE SYSTEM YES RECORD_SKIP_DDL TRUE SYSTEM YES RECORD_SKIP_ERRORS TRUE SYSTEM YES RECORD_UNSUPPORTED_OPERATIONS FALSE SYSTEM YES
SQL> begin
2 dbms_logstdby.apply_set('APPLY_SERVERS',2);
3 end;
4 /
In Oracle Database 10g, the events related to SQL Apply are written to the alert log, which is not very useful since you may want to write scripts to check them for alerts or reporting. In Oracle Database 11g, the events are by default written to a new table called LOGSTDBY$EVENTS in the SYSTEM schema. Here is a sample query:
select event_time, error from system.logstdby$events order by 1;
EVENT_TIME ERROR ----------------------------- ------------------------------------------------- 13-JAN-08 11.24.14.296807 PM ORA-16111: log mining and apply setting up 13-JAN-08 11.24.14.320487 PM Apply LWM 2677727, HWM 2677727, SCN 2677727 14-JAN-08 07.22.10.057673 PM APPLY_SET: APPLY_SERVERS changed to 2 14-JAN-08 07.22.11.034029 PM APPLY_SERVERS changed to 2 14-JAN-08 07.45.15.579761 PM APPLY_SET: EVENT_LOG_DEST changed to DEST_ALL 14-JAN-08 07.45.16.430027 PM EVENT_LOG_DEST changed to DEST_ALL
begin
dbms_logstdby.apply_set('EVENT_LOG_DEST','DEST_ALL');
end;
LOGSTDBY: APPLY_SET: EVENT_LOG_DEST changed to DEST_ALL LOGSTDBY status: EVENT_LOG_DEST changed to DEST_ALL
First, you saw how trivial it is to build a physical standby database from an active primary database. Also, you saw how easy it is to convert that physical standby to a logical one. The biggest advantage comes from the fact that the standby database can now be used productively to support the business in some way. The Active Data Guard feature allows you to open the standby database allowing queries while the archived logs are still being applied. Snapshot standby database allows you to run production database loads on the database and then flash back to the point it started to resume the normal manager recovery process. These two features allow you to leverage the processing capabilities available in the standby server and should be a great catalyst for the upgrade to 11g.