Oracle Streams - Step by Step
Oracle Streams - Step by Step
Introduction
Recently, I had a project to migrate data from an Oracle 10g EE database to an Oracle 9i EE database. The data to be
migrated was real-time and required fairly large data transformation. The basic use of the migration was to keep an older
production 9i database in sync with the newer 10g database for several months while applications dependant on this data
were remediated to use the new database. This paper and the accompanying presentation are based on the proof of concept
(POC) we performed to prove this functionality.
The POC would use Oracle streams, Change Data Capture (CDC) specifically, to capture the data. That data was then
available to other 10g databases but not to 9i databases. To enable us to send the data to 9i, we enqueued the captured data
into an AQ queue that was then propagated to a 9i database. Once the data was in 9i, it was a simple process to dequeue the
data and apply it.
Below is the short list for setting up Change Data Capture using Oracle Streams. These steps are mostly from the docs with a
few tweaks I have added. Following the setup of CDC is a discussion of using AQ to move the data from 10g to 9i. The
paper concludes with a detailed discussion of creating your own manual Logical Change Record (LCR).
Create the streams tablespace (change the name and/or location to suit):
create tablespace streams_tbs datafile 'c:\temp\stream_tbs.dbf' size 25M
reuse autoextend on maxsize unlimited;
I haven't quite figured out why, but we need to grant our administrator DBA privs. I think this is a bad thing. There is
probably a work around where I could do some direct grants instead but I haven't had time to track those down.
grant dba to strmadmin;
BEGIN
DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE(
grantee => 'strmadmin',
We also need to create the employee_audit table. Note that I am adding three columns in this table that do not exist in the
employee table.
Grant all access to the audit table to the streams admin user:
grant all on hr.employee_audit to strmadmin;
We can create a logging table. You would NOT want to do this in a high-volume production system. I am doing this to
illustrate user defined monitoring and show how you can get inside the capture process.
CREATE TABLE streams_monitor (
date_and_time TIMESTAMP(6) DEFAULT systimestamp,
txt_msg CLOB );
Here we create the queue. Unlike AQ, where you have to create a separate table, this step creates the queue and the
underlying ANYDATA table.
BEGIN
DBMS_STREAMS_ADM.SET_UP_QUEUE(
queue_table => 'strmadmin.streams_queue_table',
queue_name => 'strmadmin.streams_queue');
END;
/
This just defines that we want to capture DML and not DDL.
BEGIN
DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name => 'hr.employees',
streams_type => 'capture',
streams_name => 'capture_emp',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => false,
inclusion_rule => true);
END;
/
Tell the capture process that we want to know who made the change:
BEGIN
DBMS_CAPTURE_ADM.INCLUDE_EXTRA_ATTRIBUTE(
capture_name => 'capture_emp',
attribute_name => 'username',
include => true);
END;
/
We also need to tell Oracle where to start our capture. Change the source_database_name to match your database.
DECLARE
iscn NUMBER;
BEGIN
iscn := DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER();
DBMS_APPLY_ADM.SET_TABLE_INSTANTIATION_SCN(
source_object_name => 'hr.employees',
source_database_name => 'ORCL',
instantiation_scn => iscn);
END;
/
And the fun part! This is where we define our capture procedure. I'm taking this right from the docs but I'm adding a couple
steps.
-- I am inserting the XML equivalent of the LCR into the monitoring table.
insert into streams_monitor (txt_msg)
values (command ||
DBMS_STREAMS.CONVERT_LCR_TO_XML(in_any) );
-- Set the new values to the old values for update and delete
IF command IN ('DELETE', 'UPDATE') THEN
-- Get the old values in the row LCR
old_values := lcr.GET_VALUES('old');
-- Set the old values in the row LCR to the new values in the row LCR
lcr.SET_VALUES('new', old_values);
-- Set the old values in the row LCR to NULL
lcr.SET_VALUES('old', NULL);
END IF;
Create the apply rule. This tells streams, yet again, that we in fact do want to capture changes. The second calls tells streams
where to put the info. Change the source_database_name to match your database.
DECLARE
emp_rule_name_dml VARCHAR2(30);
emp_rule_name_ddl VARCHAR2(30);
BEGIN
DBMS_STREAMS_ADM.ADD_TABLE_RULES(
table_name => 'hr.employees',
streams_type => 'apply',
streams_name => 'apply_emp',
queue_name => 'strmadmin.streams_queue',
include_dml => true,
include_ddl => false,
source_database => 'ORCL',
dml_rule_name => emp_rule_name_dml,
ddl_rule_name => emp_rule_name_ddl);
DBMS_APPLY_ADM.SET_ENQUEUE_DESTINATION(
rule_name => emp_rule_name_dml,
destination_queue_name => 'strmadmin.streams_queue');
END;
/
BEGIN
DBMS_APPLY_ADM.SET_PARAMETER(
apply_name => 'apply_emp',
parameter => 'disable_on_error',
value => 'n');
END;
/
It takes a few seconds for the data to make it to the logs and then back into the system to be applied. Run this query until you
see data (remembering that it is not instantaneous):
Then you can log back into the streams admin account:
sqlplus strmadmin/strmadmin
View the XML LCR that we inserted during the capture process:
set long 9999
set pagesize 0
select * from streams_monitor;
That's it! It's really not that much work to capture and apply changes using Oracle 10g. Of course, it's a little bit more work to
cross database instances, but it's not that much.
One of the things that amazes me is how little code is required to accomplish this. The less code I have to write, the less code
I have to maintain.
AQ 10g to 9i
NOTE: Streams is not available with Oracle 10g XE. Download and install EE. If you have 1 GB or more of RAM on your
PC, you can download EE and use the DBCA to run two database instances. You do not physically need two machines to get
this to work.
You need two Oracle instances for this. I used a 10g instance as my source (required) and a 9i database as my target (you
could also use a 10g instance here). I called my first instance ORCL (how creative!) and I called my second instance
SECOND. It works for me!
As I said, ORCL will be my source instance and SECOND will be my target instance. You should already have the CDC code
from the last article running in ORCL. If you are not using the same names for your databases and you are not sure of the
exact name of your databases (including domain), use global_name, i.e. select * from global_name;
ORCL must be in archivelog mode to run CDC. SECOND does not need archivelog mode. Having two databases running on
a single PC in archivelog mode can really beat up a poor IDE drive.
You already created your streams admin user in ORCL so now do the same thing in SECOND. The code below is mostly the
same code that you ran on ORCL. I made a few minor changes in case you are running both instances on a single PC:
sqlplus / as sysdba
Connect as strmadmin. You need to create an AQ table, AQ queue and then start the queue. That's what the code below does.
BEGIN
DBMS_AQADM.CREATE_QUEUE_TABLE(
queue_table => 'lrc_emp_t',
queue_payload_type => 'sys.anydata',
multiple_consumers => TRUE,
compatible => '8.1');
DBMS_AQADM.CREATE_QUEUE(
queue_name => 'lrc_emp_q',
queue_table => 'lrc_emp_t');
DBMS_AQADM.START_QUEUE (
queue_name => 'lrc_emp_q');
END;
/
You also need to create a database link. You have to have one from ORCL to SECOND but for debugging, I like a link in
both. So, while you're in SECOND, create a link:
CREATE DATABASE LINK orcl.world
CONNECT TO strmadmin
IDENTIFIED BY strmadmin
USING 'orcl.world';
Log into ORCL as strmadmin and run the exact same command there. Most of the setup for this is exactly the same between
the two instances.
Ok, now we have running queues in ORCL and SECOND. While you are logged into ORCL, you will create a propagation
schedule. You DO NOT need to run this in SECOND.
BEGIN
DBMS_STREAMS_ADM.ADD_TABLE_PROPAGATION_RULES(
table_name => 'hr.employees',
streams_name => 'orcl_2_second',
source_queue_name => 'strmadmin.lrc_emp_q',
destination_queue_name => '[email protected]',
include_dml => true,
include_ddl => FALSE,
source_database => 'orcl.world');
END;
/
This tells the database to take the data in the local lrc_emp_q and send it to the named destination queue.
We're almost done with the propagation now. We are going to modify the EMP_DML_HANDLER (from above) so that we
get an enqueue block just above the execute statement:
DECLARE
enqueue_options DBMS_AQ.enqueue_options_t;
message_properties DBMS_AQ.message_properties_t;
message_handle RAW(16);
recipients DBMS_AQ.aq$_recipient_list_t;
BEGIN
recipients(1) := sys.aq$_agent(
'anydata_subscriber',
'[email protected]',
NULL);
message_properties.recipient_list := recipients;
DBMS_AQ.ENQUEUE(
queue_name => 'strmadmin.lrc_emp_q',
enqueue_options => enqueue_options,
message_properties => message_properties,
payload => anydata.convertObject(lcr),
msgid => message_handle);
EXCEPTION
WHEN OTHERS THEN
insert into streams_monitor (txt_msg)
values ('Anydata: ' || DBMS_UTILITY.FORMAT_ERROR_STACK );
END;
The declaration section above created some variables required for an enqueue. We created a subscriber (that's the name of the
consumer). We will use that name to dequeue the record in the SECOND instance.
I put the exception handler there in case there are any problems with our enqueue.
That's all it takes. Insert some records into the HR.employees table and commit them. Then log into strmadmin@second and
select * from the lrc_emp_t table. You should have as many records there as you inserted.
There are not a lot of moving parts so there aren't many things that will go wrong. Propagation is where I have the most
troubles. You can query DBA_PROPAGATION to see if you have any propagation errors.
Anyway, I (and a coworker) searched the web and looked through various documentation but was not able to find a concise
description of how to go about creating an LCR manually. So I decided to write one.
NOTE: Everything below is specifically for a ROW type LCR as opposed to a DDL type LCR. The concepts would be the
same but the specific code would change.
First a little definition. An LCR format is the format of data that Oracle uses in the redo logs and is used for Oracle Streams
(and probably data guard although I am guessing about that). The LCR has information about what the object is as well as the
old and new values. The old and new values are exactly the same as :old and :new in a trigger.
You can see the definition of the LCR format by viewing the LCR XML Schema. http://download-
east.oracle.com/docs/cd/B14117_01/server.101/b10727/ap_xmlsc.htm
An LCR is an object type. Actually, it is two table collections of an object type embedded within another object type. You can
get the details of that from the documentation in the supplied PL/SQL Packages and Types documentation.
http://www.oracle.com/pls/db102/to_toc?pathname=appdev.102%2Fb14258%2Ftoc.htm&remark=portal+%28Application+d
evelopment%29
The short story is that an LCR stored object level information (database, object_name, owner, and command type) at the top
level. Beneath that is column and data information in a name/value pair collection.
I tend to think of things like this in a relational format. If I put it in database terms, it might look something like:
You can do a describe to see the methods as well as view the documentation. The important thing to note is the constructor.
Normally, a constructor has the same name as the object type. In this case, they chose to name it CONSTRUCT.
SQL> desc sys.lcr$_row_record
METHOD
------
STATIC FUNCTION CONSTRUCT RETURNS LCR$_ROW_RECORD
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
SOURCE_DATABASE_NAME VARCHAR2 IN
COMMAND_TYPE VARCHAR2 IN
OBJECT_OWNER VARCHAR2 IN
OBJECT_NAME VARCHAR2 IN
TAG RAW IN DEFAULT
TRANSACTION_ID VARCHAR2 IN DEFAULT
SCN NUMBER IN DEFAULT
OLD_VALUES LCR$_ROW_LIST IN DEFAULT
NEW_VALUES LCR$_ROW_LIST IN DEFAULT
Just for your info, the type LCR$_ROW_LIST is a collection of LCR$_ROW_UNIT. Those types are also documented in the
reference guide I mentioned above. You will not want to access those directly though. You can use the built-in
LCR$_ROW_RECORD methods to populate those fields.
One thing to remember is that the data values that you are dealing with are sys.AnyData data types. That type has it's own
rules and deserves a blog entry all to itself.
Here is a function that will create an empty LCR for you automatically for any table. Once you have the LCR you can modify
the values to suit.
-- You can override the values in the constructor by calling these methods
v_lcr.set_command_type(p_command);
v_lcr.set_object_name(p_table_name);
v_lcr.set_object_owner(p_table_owner);
v_lcr.set_source_database_name(v_database);
-- Loop through the columns and add new and old values
FOR c1 IN (
SELECT column_name, data_type
FROM all_tab_columns
WHERE owner = p_table_owner
AND table_name = p_table_name
ORDER BY column_id )
LOOP
RETURN v_lcr;
END;
To call this function and manipulate it, you might write something like the following:
DECLARE
v_lcr sys.lcr$_row_record;
BEGIN
v_lcr := create_lcr( 'HR', 'EMPLOYEES', 'INSERT' );
DBMS_OUTPUT.PUT_LINE(
'New First Name: ' || sys.anyData.accessVarchar2(v_lcr.get_value('new', 'first_name')) ||
', Old First Name: ' || sys.anyData.accessVarchar2(v_lcr.get_value('old', 'first_name')) );
END;
Thank you for taking the time to read this paper. You can contact me in the future at [email protected] or come by and
read my blog, An Expert's Guide to Oracle Technology, http://blogs.ittoolbox.com/oracle/guide.
LewisC