Oracle Replication
For Decision Support At IU
6/1999, Mike Riley
Oracle provides several mechanisms to support replication between Oracle Database sites. These mechanisms provide replication for database objects such as tables, views, database triggers, packages, indexes, and even synonyms. This document addresses only the mechanisms that support data replication: Multi-master Replication (MMR), and Materialized Views. Materialized Views are covered in detail since they apply most directly to Data-warehousing applications. Although Oracle's Procedural Replication can be used to support data replication, it is only briefly mentioned here due to its complex, low-level nature.
Hyperlinks in this document link to online documentation on Oracle's Tech-Net web site at: http://technet.oracle.com, which requires a registered username and password. You may obtain a username and password by selecting the "Membership" link from the home page of the site.
The material in this document was gleaned from the following sources:
Oracle's Multi-master Replication (MMR) allows multiple sites, acting as equal peers, to maintain replicated "master" copies of database objects. Each participating site maintains a complete updateable copy of the replicated object (defined in a "Replication Group" - discussed below), where replication among the participating sites may be specified as either Synchronous, or Non-Synchronous. Synchronous Replication updates are performed within the bounds of the originating transaction via. Two-phase commits. Non-Synchronous replication updates are propagated sometime after the originating transaction, and are termed "deferred transactions". Mechanisms exist for handling conflicts that may occur when using Non-Synchronous, multi-site updates.
Because it is not permissible, nor desirable to allow updates to base-table OLTP data from within a Data-warehouse (DW), and because DW applications often require only subsets of the base-table data, MMR is not a good fit for general usage in a DW architecture. For this reason, this type of replication is not discussed in any further detail here.
Materialized Views
MVs provide a distributed, point-in-time copy, of one or more remote "master" tables, and may include row restrictions, column selections, aggregations, and sub-selections. MVs support updateable and/or read-only copies that may be based on Synchronous or Non-Synchronous replication. In addition, Non-Synchronous replication may be scheduled, or on-demand. Concerning replication, MVs are useful for:
The term MV is used synonymously with the term Snapshot in Oracle documentation. Indeed, even the PL/SQL command: "create materialized view..." can be used interchangeably with the command: "create snapshot...". Oracle has apparently chosen to replace the term Snapshot with MV in all future documentation releases (see "Note" in Replication Objects, Groups, and Sites, Oracle8i Tuning manual, chap 28).
Also, note that since MVs are updated through an efficient batch process, they are more efficient than MMR.
Updateable MVs
Always based on a single "master" table, updateable MVs may be defined as all or a subset of the base-table's rows and columns. Although there must be a one-to-one correspondence between the updateable MV and the table it is based upon, SQL subselects may be used in the definition of the MV to qualify the participating rows.
Read-only MVs
Read-only MVs can support simple or complex views of the "master" table(s) on which it is defined. A complex MV is one that contains: aggregates, joins, set operations (e.g. sum, count), or a CONNECT BY clause.
Procedural Replication
Oracle's Procedural Replication replicates simple stored procedure calls to distributed sites, and can be used for data replication. Essentially, a simple Remote Procedure Call, Procedural Replication is a "programming focused" solution. It should be noted, though, that Procedural Replication might be useful for minimizing network data transfer if the data (or even a large portion of it) used by the replicated procedure call is pre-available at the replication site (probably a rare circumstance in reality, but possible, I suppose).
Replication Tools
Oracle provides the following tools for managing replication objects:
Besides replication, MVs are also useful for pre-calculated joins and aggregations, even within the same Oracle database (although, this is not a requirement). Similar to traditional indexes, Oracle's query-optimizer automatically (transparently) rewrites a query against base tables to use a MV where possible, avoiding the overhead time required to perform the actual join or aggregation in realtime, thus significantly reducing query response time. Such MVs are defined by the DBA, and then created and maintained by Oracle. MVs may be queried directly, but Oracle recommends against doing so to allow the DBA freedom to drop/create/change MVs without affecting the related [query] applications - much like an index. Any number of joins as well as aggregates may be used; the underlying disadvantage is the cpu time and disk-space required to maintain the MV.
Materialized Views Classes
There are three classes of MVs in Oracle: Rowid, Primary Key, and Complex. Rowid MVs are provided only for backward compatibility with Oracle7, are based on a physical row identifier assigned internally by Oracle, and are not recommended for Oracle8+ implmentations. Primary Key MVs are the usual (default) type of MV. Oracle maintains Primary Key MVs by logging the primary-key of each row affected by all DML statements against the base table upon which the MV is defined, and then at some later point-in-time, using the log to replicate the affected rows to the MV.
A MV is classed as Complex if it contains: an aggregate (count, sum,...), a distinct clause, a set operation (UNION, INTERSECT, MINUS,...), or a connect-by clause, or if it does not meet certain specific subquery restrictions (see Table 3-1, "Restrictions for Snapshots with Subqueries"). It is most important to note that complex MVs cannot be incrementally refreshed ("fast refreshed") by Oracle, but rather must be completely refreshed (rebuilt) when a new "point-in-time-view" is required (/triggered).
Examples:
An Oracle Replication architecture consists of: a base-table, a trigger, and an update log, at the "master site", and a MV, a trigger, and update log at the snapshot site. A Readonly MV does not have/need a log or trigger at the "snapshot site", and the log and trigger at the Master site are only needed if fast (incremental) refresh is employed. See Figure 3-5 Snapshot ReplicationMechanisms for a pictorial view.
As mentioned above, Oracle maintains Primary Key MVs by logging the primary-key of the affected row of the table upon which the MV is defined. This logging is performed by an internal trigger on the master base-table that fires each time a row is added, updated, or deleted; the Primary Key of the affected row, and any "filter columns" are written to a log file named MLOG$_master_table_name. If the MV is updateable, an internal trigger is defined on it also, and the primary key and filter column of the affected row is written to a corresponding local log file named USLOG$_Materialized_View_Name.
A MV has at least one index, I_SNAP$_Materialized_View_Name, corresponding to the Primary Key. Additional indexes may be created to support fast refreshes that involve subqueries.
Certain subqueries of updateable MVs require "filter columns", in addition to the Primary Key columns of the base table. See "Using Filter Columns" for more information.
Some datatypes, such as LONG, BFILES, and user defined datatypes are not supported by MVs, and there are some restrictions on the use of LOBs. See Datatype considerations.
Replication Group Mechanisms
An Oracle Replication Group (RG) construct facilitates the administration of a defined set of replication objects. Typically, RGs are used to group together objects by application area. A RG may have objects from several DB schemas, and a DB schema may have objects in several RGs, but a replication object can be a member of only one group. While MMR and updateable MV objects must participate in an RG, Read-only MVs are not required to, but often do (by choice) for administrative convenience.
An RG at a site used for MVs ( Snapshots) is referred to in the Oracle Replication manual as a Snapshot Group, and a MMR RG is referred to as a Master Group. While an MMR site must contain all objects that participate in the Master Group (see Fig 34-1, Oracle8I Replication manual), a Snapshot (MV) site may contain all or a subset of the objects that participate in a Snapshot Group (see Fig 34-2, Oracle8I Replication manual). Multiple Snapshot Groups can be defined on a target Master Group.
Refresh Groups can be used to keep a set of replication objects synchronized to a specific transactionally-consistent point-in-time. preserving referential data integrity. MVs from multiple Snapshot Groups can participate in a Refresh Group (see Figure 3-8, Oracle8I Replication manual). Replication performance efficiencies can be gained by grouping multiple replication objects into a single Refresh Group; up to 400 MVs can be contained in a single Refresh Group. Using muliple Refresh Groups for a given RG can introduce referential inconsistencies.
Refresh Mechanisms
Three different methods are employed by Oracle for refreshing MVs: Complete Refresh, Fast Refresh, Force Refresh. Complete Refresh executes the complete defining query of the MV to rebuild it. Fast Refresh replicates only newly affected rows, using the primary key stored in the update log; updateable MVs logs are next processed and applied to the master table, and then afterwards, when the master table update log is processed, updates are pushed from there to other/all dependent MVs. Force Refresh attempts a Fast Refresh first, and if unsuccessful, performs a Complete Refresh. A refresh may be scheduled via. a specific "refresh interval", or performed on-demand.
The following steps are required to setup Replication:
A nice, simple flowchart depicting these steps with hyper-links to sub-diagrams, their substeps, etc., is given in Figure 1-1. The lowest level sub-steps in the example charts are hyper-linked to example PL/SQL statements, including example calls to the Replication API. Chapter 7, "Manage Replicated Environment with APIs", in the Replication API manual, contains a good description of the tasks required to manage a replication environment. See also Chapter 8, "Replication Management API Reference", Chapter 9, "Data Dictionary Views", and "Advanced Management of Master and Snapshot Groups".
Going GUI
The following sections of the Oracle8I Replication manual provide an
overview of how to use GUI-based Replication Manager to accomplish these
same tasks: Prepare
for Snapshots, Create
a Snapshot Log, Create
Snapshot Environment.