Oracle® Database Advanced Replication Management API Reference 11g Release 2 (11.2) Part Number E10707-01 |
|
|
View PDF |
This chapter illustrates how to manage the replication objects and queues in your replication environment using the replication management API.
This chapter contains these topics:
As your database needs change, you might need to modify the characteristics of your replicated objects. It is important that you do not directly execute DDL to alter your replicated objects. Doing so might cause your replication environment to fail.
Use the ALTER_MASTER_REPOBJECT
procedure in the DBMS_REPCAT
package to alter the characteristics of your replicated objects in a quiesced master group. From the example following, notice that you simply include the necessary DDL within the procedure call (see the ddl_text
parameter).
Meet the following requirements to complete these actions:
Executed As: Replication Administrator
Executed At: Master Definition Site
Replication Status: Quiesced
Complete the following steps to alter a replicated object in a quiesced master group.
Note:
If you are viewing this document online, then you can copy the text from the "BEGINNING OF SCRIPT" line after this note to the "END OF SCRIPT" line into a text editor and then edit the text to create a script for your environment./************************* BEGINNING OF SCRIPT ******************************
Refresh the updatable materialized views to push any changes from them to the master table. See "Refreshing Materialized Views" for instructions.
*/ PAUSE Press <RETURN> to continue when all of the updatable materialized views that are based on the master table have been refreshed. /*
*/
SET ECHO ON SPOOL alter_rep_object.out CONNECT repadmin@orc1.example.com /*
See the "ALTER_MASTER_REPOBJECT Procedure" for information about when quiesce is not required.
*/ BEGIN DBMS_REPCAT.SUSPEND_MASTER_ACTIVITY ( gname => 'hr_repg'); END; / /*
Do not proceed until the group's status is QUIESCED
.
To check the status, run the following query:
SELECT GNAME, STATUS FROM DBA_REPGROUP; */ PAUSE Press <RETURN> to continue when the master group's status is QUIESCED. /*
*/
BEGIN DBMS_REPCAT.ALTER_MASTER_REPOBJECT ( sname => 'hr', oname => 'employees', type => 'TABLE', ddl_text => 'ALTER TABLE hr.employees ADD (timestamp DATE)'); END; / /*
*/
BEGIN DBMS_REPCAT.GENERATE_REPLICATION_SUPPORT ( sname => 'hr', oname => 'employees', type => 'TABLE', min_communication => TRUE); END; / /*
Do not proceed until this view is empty.
Execute the following SELECT
statement in another SQL*Plus session to monitor the DBA_REPCATLOG
view:
SELECT * FROM DBA_REPCATLOG WHERE GNAME = 'HR_REPG'; */ PAUSE Press <RETURN> to continue when DBA_REPCATLOG is empty. /*
If you altered a master table with fast-refreshable materialized views based on it, then the materialized view log must be dropped and re-created if any of the following conditions are met:
The materialized view log must contain one or more new columns that were added to the master table. In this case, you can choose to alter the materialized view log to add the columns. See "Altering a Materialized View Log to Add Columns".
The materialized view log contains one or more columns in the master table that were altered.
The materialized view log contains one more columns that were deleted from the master table.
*/ PAUSE Press <RETURN> to continue after the materialized view logs are re-created. /*
If you altered a master table with updatable materialized views based on it, then all of these updatable materialized views must be re-created.
If you altered a master table with read-only materialized views based on it, then these read-only materialized views must be re-created if any of the following conditions are met:
The read-only materialized view must reference one or more columns that were added to the master table.
The read-only materialized view references one or more columns in the master table that were altered.
The read-only materialized view references one or more columns that were deleted from the master table.
*/ PAUSE Press <RETURN> to continue after the materialized views are re-created. /*
*/
BEGIN DBMS_REPCAT.RESUME_MASTER_ACTIVITY ( gname => 'hr_repg'); END; / SET ECHO OFF SPOOL OFF /************************* END OF SCRIPT **********************************/
You might have a situation in which you need to modify a replicated object, but you do not want this modification replicated to the other sites in the replication environment. For example, you might want to disable replication in the following situations:
When you are using procedural replication to propagate a change, always disable row-level replication at the start of your procedure.
You might need to disable replication in triggers defined on replicated tables to avoid replicating trigger actions multiple times. See "Ensuring that Replicated Triggers Fire Only Once".
Sometimes when you manually resolve a conflict, you might not want to replicate this modification to the other copies of the table.
You might need to do this, for example, if you need to correct the state of a record at one site so that a conflicting replicated update will succeed when you reexecute the error transaction. Or, you might use an unreplicated modification to undo the effects of a transaction at its origin site because the transaction could not be applied at the destination site. In this example, you can use the Advanced Replication interface in Oracle Enterprise Manager to delete the conflicting transaction from the destination site.
To modify tables without replicating the modifications, use the REPLICATION_ON
and REPLICATION_OFF
procedures in the DBMS_REPUTIL
package. These procedures take no arguments and are used as flags by the generated replication triggers.
Note:
To enable and disable replication, you must have theEXECUTE
privilege on the DBMS_REPUTIL
package.The DBMS_REPUTIL.REPLICATION_OFF
procedure sets the state of an internal replication variable for the current session to FALSE
. Because all replicated triggers check the state of this variable before queuing any transactions, modifications made to the replicated tables that use row-level replication do not result in any queued deferred transactions.
Caution:
Turning replication on or off affects only the current session. That is, other users currently connected to the same server are not restricted from placing committed changes in the deferred transaction queue.If you are using procedural replication, then call REPLICATION_OFF
at the start of your procedure, as shown in the following example. This ensures that the replication facility does not attempt to use row-level replication to propagate the changes that you make.
CREATE OR REPLACE PACKAGE update_objects AS PROCEDURE update_emp(adjustment IN NUMBER); END; / CREATE OR REPLACE PACKAGE BODY update_objects AS PROCEDURE update_emp(adjustment IN NUMBER) IS BEGIN --turn off row-level replication for set update DBMS_REPUTIL.REPLICATION_OFF; UPDATE emp . . .; --reenable replication DBMS_REPUTIL.REPLICATION_ON; EXCEPTION WHEN OTHERS THEN . . . DBMS_REPUTIL.REPLICATION_ON; END; END; /
After resolving any conflicts, or at the end of your replicated procedure, be certain to call DBMS_REPUTIL.REPLICATION_ON
to resume normal replication of changes to your replicated tables or materialized views. This procedure takes no arguments. Calling REPLICATION_ON
sets the internal replication variable to TRUE
.
If you have defined a replicated trigger on a replicated table, then you might need to ensure that the trigger fires only once for each change that you make. Typically, you only want the trigger to fire when the change is first made, and you do not want the remote trigger to fire when the change is replicated to the remote site.
You should check the value of the DBMS_REPUTIL.FROM_REMOTE
package variable at the start of your trigger. The trigger should update the table only if the value of this variable is FALSE
.
Alternatively, you can disable replication at the start of the trigger and reenable it at the end of the trigger when modifying rows other than the one that caused the trigger to fire. Using this method, only the original change is replicated to the remote sites. Then the replicated trigger fires at each remote site. Any updates performed by the replicated trigger are not pushed to any other sites.
Using this approach, conflict resolution is not invoked. Therefore, you must ensure that the changes resulting from the trigger do not affect the consistency of the data.
LOB columns using BASICFILE
storage can be replicated, but LONG
columns cannot be replicated. You can convert the data type of a LONG
column to a CLOB
column and the data type of a LONG_RAW
column to a BLOB
column.
Converting a LONG
column to a LOB column can result in increased network bandwidth requirements because the data in such a column is replicated after conversion. Ensure that you have adequate network bandwidth before completing the procedure in this section.
Note:
LOB columns usingSECUREFILE
storage cannot be replicated.See Also:
Oracle Database SecureFiles and Large Objects Developer's Guide for more information about applications andLONG
to LOB conversionComplete the following steps to convert a LONG
column to a LOB column in a replicated table:
If a table containing a LONG
column is configured as a master table, then Oracle does not replicate changes to the data in the LONG
column. Therefore, the data in the LONG
column might not match at all of your replication sites. You must ensure that the data in the LONG
column matches at all master sites before proceeding.
CONNECT repadmin@orc1.example.com
BEGIN
DBMS_REPCAT.SUSPEND_MASTER_ACTIVITY ( gname => 'sales_mg'); END; /
BEGIN
DBMS_REPCAT.ALTER_MASTER_REPOBJECT ( sname => 'staff', oname => 'positions', type => 'TABLE', ddl_text => 'ALTER TABLE staff.positions MODIFY (job_desc CLOB)'); END; /
A LONG_RAW
column can be converted to a BLOB
column using a similar ALTER
TABLE
statement.
BEGIN
DBMS_REPCAT.GENERATE_REPLICATION_SUPPORT ( sname => 'staff', oname => 'positions', type => 'TABLE', min_communication => TRUE); END; /
BEGIN
DBMS_REPCAT.RESUME_MASTER_ACTIVITY ( gname => 'sales_mg'); END; /
Rebuild materialized views if necessary.
It is possible for the differences to arise in replicated tables. When administering a replication environment, you might want to check, periodically, whether the contents of two replicated tables are identical. The following procedures in the DBMS_RECTIFIER_DIFF
package let you identify, and optionally rectify, the differences between two tables.
Note:
You can also determine differences between database objects and converge them using theDBMS_COMPARISON
package.See Also:
Oracle Database PL/SQL Packages and Types Reference for information about the DBMS_COMPARISON
package
Oracle Database 2 Day + Data Replication and Integration Guide and Oracle Streams Replication Administrator's Guide for information about using the DBMS_COMPARISON
package
The DIFFERENCES
procedure compares two replicas of a table, and determines all rows in the first replica that are not in the second and all rows in the second that are not in the first. The output of this procedure is stored in two user-created tables. The first table stores the values of the missing rows, and the second table is used to indicate which site contains each row.
The RECTIFY
procedure uses the information generated by the DIFFERENCES
procedure to rectify the two tables. Any rows found in the first table and not in the second are inserted into the second table. Any rows found in the second table and not in the first are deleted from the second table.
To restore equivalency between all copies of a replicated table, complete the following steps:
This copy will be used to update all other replicas of the table as needed.
For example, it might not be necessary to check rows that have not been updated since the last time that you checked for differences. Although it is not necessary to check all columns, your column list must include all columns that make up the primary key (or that you designated as a substitute identity key) for the table.
You must create one table that can hold the data for the columns being compared. For example, if you decide to compare the employee_id
, salary
, and department_id
columns of the employees
table, then your CREATE
statement would need to be similar to the following:
CREATE TABLE hr.missing_rows_data ( employee_id NUMBER(6), salary NUMBER(8,2), department_id NUMBER(4));
You must also create a table that indicates where the row is found. This table must contain three columns with the data types shown in the following example:
CREATE TABLE hr.missing_rows_location ( present VARCHAR2(128), absent VARCHAR2(128), r_id ROWID);
Although suspending replication activity for the group is not a requirement, rectifying tables that were not quiesced first can result in inconsistencies in your data.
CONNECT repadmin BEGIN DBMS_REPCAT.SUSPEND_MASTER_ACTIVITY ( gname => 'hr_repg'); END; /
For example, if you wanted to compare the employees
tables at the New York and San Francisco sites, then your procedure call would look similar to the following:
BEGIN DBMS_RECTIFIER_DIFF.DIFFERENCES ( sname1 => 'hr', oname1 => 'employees', reference_site => 'ny.example.com', sname2 => 'hr', oname2 => 'employees', comparison_site => 'mv4.example.com', where_clause => '', column_list => 'employee_id,salary,department_id', missing_rows_sname => 'hr', missing_rows_oname1 => 'missing_rows_data', missing_rows_oname2 => 'missing_rows_location', missing_rows_site => 'ny.example.com', max_missing => 500, commit_rows => 50); END; /
Figure 9-1 shows an example of two replicas of the employees
table and what the resulting missing rows tables would look like if you executed the DIFFERENCES
procedure on these replicas.
Figure 9-1 Determining Differences Between Replicas
Notice that the two missing rows tables are related by the ROWID
and r_id
columns.
BEGIN
DBMS_RECTIFIER_DIFF.RECTIFY ( sname1 => 'hr', oname1 => 'employees', reference_site => 'ny.example.com', sname2 => 'hr', oname2 => 'employees', comparison_site => 'mv4.example.com', column_list => 'employee_id,salary,department_id', missing_rows_sname => 'hr', missing_rows_oname1 => 'missing_rows_data', missing_rows_oname2 => 'missing_rows_location', missing_rows_site => 'ny.example.com', commit_rows => 50); END; /
The RECTIFY
procedure temporarily disables replication at the "comparison" site while it performs the necessary insertions and deletions, as you would not want to propagate these changes. RECTIFY
first performs all of the necessary DELETE
operations and then performs all of the INSERT
operations. This ensures that there are no violations of a PRIMARY
KEY
constraint.
After you have successfully executed the RECTIFY
procedure, your missing rows tables should be empty.
Caution:
If you have any additional constraints on the "comparison" table, then you must ensure that they are not violated when you callRECTIFY
. You might need to update the table directly using the information in the missing rows table. If so, then be sure to DELETE
the appropriate rows from the missing rows tables.Remember to use the same "reference" table each time to ensure that all copies are identical when you complete this procedure.
BEGIN DBMS_REPCAT.RESUME_MASTER_ACTIVITY ( gname => 'hr_repg'); END; /
Typically, Advanced Replication is configured to push and purge the deferred transaction queue automatically. At times, however, you might need to push or purge the deferred transaction queue manually. The process for pushing the deferred transaction queue is the same at master sites and materialized view sites.
Master sites are configured to push the deferred transaction queue automatically at set intervals. At materialized view sites, if you do not automatically propagate the transactions in your deferred transaction queue during the refresh of your materialized view, then you must complete the following steps to propagate changes made to the updatable materialized view to its master table or master materialized view.
This example illustrates pushing the deferred transaction queue at a materialized view site, but the process is the same at master sites and materialized view sites.
Executed As: Materialized View Administrator
Executed At: Materialized View Site
Complete the following steps:
CONNECT mviewadmin@mv1.example.com
Propagation of the deferred transaction queue is based on the destination of the transaction. Each distinct destination and the number of transactions pending for the destination will be displayed.
SELECT DISTINCT(dblink), COUNT(deferred_tran_id) FROM deftrandest GROUP BY dblink;
DECLARE temp INTEGER; BEGIN temp := DBMS_DEFER_SYS.PUSH ( destination => 'orc1.example.com', stop_on_error => FALSE, delay_seconds => 0, parallelism => 0); END; /
Run the PUSH
procedure for each destination that was returned in the SELECT
statement you ran in Step 2.
If your system is not set to automatically purge the successfully propagated transactions in your deferred transaction queue periodically, then you must complete the following steps to purge them manually.
This example illustrates purging the deferred transaction queue at a materialized view site, but the process is the same at master sites and materialized view sites.
Executed As: Materialized View Administrator
Executed At: Materialized View Site
Complete the following steps:
CONNECT mviewadmin@mv1.example.com
DECLARE
temp INTEGER; BEGIN temp := DBMS_DEFER_SYS.PURGE ( purge_method => DBMS_DEFER_SYS.PURGE_METHOD_QUICK); END; /
Note:
If you use thepurge_method_quick
parameter, deferred transactions and deferred procedure calls that have been successfully pushed can remain in the DEFTRAN
and DEFCALL
data dictionary views for longer than expected before they are purged. See the "Usage Notes" for DBMS_DEFER_SYS.PURGE
for details.If you are using column objects, collections, or REF
s in a replicated table, then you can use the GET_ANYDATA_ARG
function in the DBMS_DEFER_QUERY
package to determine the value of an argument in a deferred call that involves one of these user-defined types.
The following example illustrates how to use the GET_ANYDATA_ARG
function. This example uses the following user-defined types in the oe
sample schema.
CREATE TYPE phone_list_typ AS VARRAY(5) OF VARCHAR2(25); / CREATE TYPE warehouse_typ AS OBJECT (warehouse_id NUMBER(3), warehouse_name VARCHAR2(35), location_id NUMBER(4) ); / CREATE TYPE inventory_typ AS OBJECT (product_id NUMBER(6), warehouse warehouse_typ, quantity_on_hand NUMBER(8) ); / CREATE TYPE inventory_list_typ AS TABLE OF inventory_typ; /
The following procedure retrieves the argument value for collection, object, and REF
instances of calls stored in the deferred transactions queue. This procedure assumes that the call number and transaction id are available.
The user who creates the procedure must have EXECUTE
privilege on the DBMS_DEFER_QUERY
package and must have CREATE
PROCEDURE
privilege. This example uses the oe
sample schema. Therefore, to run the example, you must grant the oe
user these privileges. Connect as an administrative user and enter the following:
GRANT EXECUTE ON DBMS_DEFER_QUERY TO oe; GRANT CREATE PROCEDURE TO oe; CONNECT oe@orc1.example.com CREATE OR REPLACE PROCEDURE get_userdef_arg AS call_no NUMBER := 0; txn_id VARCHAR2(128) := 'xx.xx.xx'; anydata_val ANYDATA; t ANYTYPE; data_pl phone_list_typ; -- varray data_ntt inventory_list_typ; -- nested table type data_p warehouse_typ; -- object type ref1 REF inventory_typ; -- REF type rval PLS_INTEGER; -- return value tc PLS_INTEGER; -- return value prec PLS_INTEGER; -- precision scale PLS_INTEGER; -- scale len PLS_INTEGER; -- length csid PLS_INTEGER; -- character set id csfrm PLS_INTEGER; -- character set form cnt PLS_INTEGER; -- count of varray elements or number of -- object attributes sname VARCHAR2(35); -- schema name type_name VARCHAR2(35); -- type name version VARCHAR2(35); BEGIN FOR i IN 1 .. 5 LOOP anydata_val := DBMS_DEFER_QUERY.GET_ANYDATA_ARG(call_no, i, txn_id); -- Get the type information, including type name. tc := anydata_val.GetType(t); tc := t.GetInfo(prec, scale, len, csid, csfrm, sname, type_name, version, cnt); -- Based on the type name, convert the anydata value to the appropriate -- user-defined types. IF type_name = 'PHONE_LIST_TYP' THEN -- The anydata_val contains phone_list_typ varray instance. rval := anydata_val.GetCollection(data_pl); -- Do something with data_pl. ELSIF type_name = 'INVENTORY_LIST_TYP' THEN -- anydata_val contains inventory_list_typ nested table instance. rval := anydata_val.GetCollection(data_ntt); -- Do something with data_ntt. ELSIF type_name = 'WAREHOUSE_TYP' THEN -- The anydata_val contains warehouse_typ object instance. rval := anydata_val.GetObject(data_p); -- Do something with data_p. ELSIF type_name = 'INVENTORY_TYP' THEN -- The anydata_val contains a reference to inventory_typ object instance. rval := anydata_val.GetRef(ref1); -- Do something with ref1. END IF; END LOOP; END; /
See Also:
Oracle Database SQL Language Reference, Oracle Database Object-Relational Developer's Guide, and Oracle Database PL/SQL Packages and Types Reference for more information about the ANYDATA
data type
As an administrator of a replication environment, you should regularly monitor the error queue to determine if any deferred transactions were not successfully applied at the target master site.
To check the error queue, issue the following SELECT
statement (as the replication administrator) when connected to the target master site:
SELECT * FROM deferror;
If the error queue contains errors, then you should resolve the error condition and reexecute the deferred transaction. You have two options when reexecuting a deferred transaction: you can reexecute in the security context of the user who received the deferred transaction, or you can reexecute the deferred transaction with an alternate security context.
Caution:
If you have multiple error transactions and you want to ensure that they are reexecuted in the correct order, then you can specifyNULL
for the deferred_tran_id
parameter in the procedures in the following sections. If you do not specify NULL
, then reexecuting individual transactions in the wrong order can cause conflicts.
The following procedure reexecutes a specified deferred transaction in the security context of the user who received the deferred transaction. This procedure should not be executed until the error situation has been resolved.
Meet the following requirements to complete these actions:
Executed As: Replication Administrator
Executed At: Site Containing Errors
Replication Status: Normal
Complete the following steps:
See Oracle Database Administrator's Guide for information about connecting to a database in SQL*Plus.
BEGIN
DBMS_DEFER_SYS.EXECUTE_ERROR ( deferred_tran_id => '1.12.2904', destination => 'orc2.example.com'); END; /
The following procedure reexecutes a specified deferred transaction in the security context of the currently connected user. This procedure should not be executed until the error situation has been resolved.
Meet the following requirements to complete these actions:
Executed As: Connected User
Executed At: Site Containing Errors
Replication Status: Normal
Complete the following steps:
See Oracle Database Administrator's Guide for information about connecting to a database in SQL*Plus.
BEGIN
DBMS_DEFER_SYS.EXECUTE_ERROR_AS_USER ( deferred_tran_id => '1.12.2904', destination => 'orc2.example.com'); END; /