This action might not be possible to undo. Are you sure you want to continue?
The data to bemigrated 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 datawere 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 thenavailable to other 10g databases but not to 9i databases. To enable us to send the data to 9i, we enqueued the captured datainto an AQ queue that was then propagated to a 9i database. Once the data was in 9i, it was a simple process to dequeue thedata 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 afew 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). Change Data Capture First the set up: we will use the HR account's Employee table. We'll capture all changes to the Employee table and insert theminto an audit table. I'm not necessarily saying this is the way you should audit your database but it makes a nice example.I'll also add a monitoring piece to capture process. I want to be able to see exactly what is being captured when it is beingcaptured.You will need to have sysdba access to follow along with me. Your database must also be in archivelog mode. The changesare picked up from the redo log.So, away we go!The first step is to create our streams administrator. I will follow the guidelines from the oracle docs exactly for this:Connect as sysdba: sqlplus / as sysdba 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; Create our streams administrator: create user strmadmin identified by strmadmindefault tablespace streams_tbsquota unlimited on streams_tbs; 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; We also want to grant streams admin privs to the user.
streams_name => 'capture_emp'.department_id NUMBER(4). Here we create the queue.commission_pct NUMBER(2.2).streams_queue'.queue_name => 'strmadmin. END.streams_type => 'capture'./ The next steps we'll run as the HR user.upd_date DATE.txt_msg CLOB ).2)./ This just defines that we want to capture DML and not DDL. I am doing this to illustrate user defined monitoring and show how you can get inside the capture process. conn hr/hr Grant all access to the employee table to the streams admin: grant all on hr.employee_audit to strmadmin. CREATE TABLE employee_audit(employee_id NUMBER(6).SET_UP_QUEUE(queue_table => 'strmadmin. where you have to create a separate table. We also need to create the employee_audit table. this step creates the queue and theunderlying ANYDATA table. BEGIN DBMS_STREAMS_ADM.include_dml => true. grant_privileges => true). . manager_id NUMBER(6).salary NUMBER(8. You would NOT want to do this in a highvolume production system.streams_queue').user_name VARCHAR2(30).email VARCHAR2(25).employees'. BEGIN DBMS_STREAMS_ADM.BEGIN DBMS_STREAMS_AUTH.GRANT_ADMIN_PRIVILEGE(grantee => 'strmadmin'. phone_number VARCHAR2(20). We connect as the streams admin user: conn strmadmin/strmadmin We can create a logging table. Note that I am adding three columns in this table that do not exist in theemployee table.employees to strmadmin.job_id VARCHAR2(10).inclusion_rule => true).action VARCHAR2(30)). END.queue_name => 'strmadmin.first_name VARCHAR2(20). Unlike AQ.ADD_TABLE_RULES(table_name => 'hr.streams_queue_table'. CREATE TABLE streams_monitor (date_and_time TIMESTAMP(6) DEFAULT systimestamp.hire_date DATE.include_ddl => false. Grant all access to the audit table to the streams admin user: grant all on hr.last_name VARCHAR2(25).
I am inserting the XML equivalent of the LCR into the monitoring table.commit.lcr./ We also need to tell Oracle where to start our capture.SET_VALUES('old'.Set the old values in the row LCR to NULLlcr.insert into streams_monitor (txt_msg) values (command ||DBMS_STREAMS.ConvertDate(SYSDATE)). ANYDATA.Set the old values in the row LCR to the new values in the row LCR lcr.-.Add a user columnlcr..ConvertVarChar2(command)).-.BEGIN-.-.LCR$_ROW_RECORD.END.SET_VALUES('new'.EXECUTE(true).instantiation_scn => iscn).Add an action columnlcr. 'user_name'.ADD_COLUMN('new'.CONVERT_LCR_TO_XML(in_any) ).ADD_COLUMN('new'.Get the object command typecommand := lcr.GET_SYSTEM_CHANGE_NUMBER()./ Create the DML handlers: .END IF.-. NULL).-.END.ADD_COLUMN('new'. I'm taking this right from the docs but I'm adding a couplesteps.-.Get the old values in the row LCR old_values := lcr.SET_OBJECT_NAME('EMPLOYEE_AUDIT'). DECLAREiscn NUMBER.command VARCHAR2(30).END.Add a SYSDATE for upd_datelcr. -.Set the new values to the old values for update and deleteIF command IN ('DELETE'.Access the LCR rc := in_any.END.SET_COMMAND_TYPE('INSERT').source_database_name => 'ORCL'. 'ACTION'. old_values).GETOBJECT(lcr).BEGINiscn := DBMS_FLASHBACK.old_values SYS.S ET_TABLE_INSTANTIATION_SCN(source_object_name => 'hr.LCR$_ROW_LIST.-.DBMS_APPLY_ADM.-.GET_EXTRA_ATTRIBUTE('USERNAME') ). 'UPD_DATE'.GET_VALUES('old').rc PLS_INTEGER. CREATE OR REPLACE PROCEDURE emp_dml_handler(in_any IN ANYDATA) ISlcr SYS.INCLUDE_EXTRA_ATTRIBUTE(capture_name => 'capture_emp'.GET_COMMAND_TYPE(). Change the source_database_name to match your database./ And the fun part! This is where we define our capture procedure./ Tell the capture process that we want to know who made the change: BEGINDBMS_CAPTURE_ADM. 'UPDATE') THEN-. ANYDATA.attribute_name => 'username'.-Set the command_type in the row LCR to INSERTlcr.include => true).employees'.Set the object_name in the row LCR to EMP_DELlcr.Make the changeslcr.
END.apply_name => NULL).apply_name => NULL).operation_name => 'INSERT'. sqlplus hr/hrINSERT INTO hr.SET_DML_HANDLER(object_name => 'hr.error_handler => false.apply_database_link => NULL.COMMIT. NULL. value => 'n')./BEGINDBMS_APPLY_ADM.object_type => 'TABLE'.SET_PARAMETER(apply_name => 'apply_emp'.employees'. 'SMITH'.error_handler => false.SET_DML_HANDLER(object_name => 'hr.START_APPLY(apply_name => 'apply_emp').START_CAPTURE(capture_name => 'capture_emp').employees SET salary=5999 WHERE . 'JOHN'.SET_DML_HANDLER(object_name => 'hr. yet again.apply_database_link => NULL.employees'.queue_name => 'strmadmin.employees'.operation_name => 'DELETE'.END. 'AC_ACCOUNT'.operation_name => 'UPDATE'./ Turn on the capture process: BEGINDBMS_CAPTURE_ADM.streams_queue').emp_dml_handler'.COM'. that we in fact do want to capture changes./ Create the apply rule.END./BEGINDBMS_APPLY_ADM.END.SET_ENQUEUE_DESTINATION(rule_nam e => emp_rule_name_dml.user_procedure => 'strmadmin.streams_type => 'apply'.destination_queue_name => 'strmadmin. The second calls tells streamswhere to put the info.streams_queue'. parameter => 'disable_on_error'.BEGINDBMS_STREAMS_ADM.error_handler => false.streams_name => 'apply_emp'. NULL.user_procedure => 'strmadmin./ We don't want to stop applying changes when there is an error.END.ADD_TABLE_RULES(table_name => 'hr. so: BEGINDBMS_APPLY_ADM.apply_name => NULL).employees'.ddl_rule_name => emp_rule_name_ddl).apply_database_link => NULL. '07-JUN-94'.include_dml => true.object_type => 'TABLE'.object_type => 'TABLE'.emp_dml_handler'.UPDATE hr.emp_dml_handler'. DECLAREemp_rule_name_dml VARCHAR2(30).END.source_database => 'ORCL'. 110).user_procedure => 'strmadmin.include_ddl => false.dml_rule_name => emp_rule_name_dml.emp_rule_name_ddl VARCHAR2(30). Change the source_database_name to match your database. This tells streams./ Turn on the apply process: BEGINDBMS_APPLY_ADM. NULL. 777.'JSMITH@MYCOMPANY./ Connect as HR and make some changes to Employees.employees VALUES(207.DBMS_APPLY_ADM.BEGINDBMS_APPLY_ADM.END.
employee_id=206./ Connect as HR and make some changes to Employees. '07-JUN-94'.One of the things that amazes me is how little code is required to accomplish this.COMMIT. first_name. 'AC_ACCOUNT'./ Turn on the apply process: BEGINDBMS_APPLY_ADM.COMMIT. actionFROM hr. NULL.END. It takes a few seconds for the data to make it to the logs and then back into the system to be applied.END. but it's not that much. value => 'n'). Run this query until yousee data (remembering that it is not instantaneous): SELECT employee_id. last_name. 110).UPDATE hr.SET_PARAMETER(apply_name => 'apply_emp'. the less code I have to maintain. 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 9999set pagesize 0select * from streams_monitor. parameter => 'disable_on_error'.'JSMITH@MYCOMPANY.employees VALUES(207./ Turn on the capture process: BEGINDBMS_CAPTURE_ADM.START_CAPTURE(capture_name => 'capture_emp').COMMIT. NULL.employees WHERE employee_id=207. Then you can log back into the streams admin account: . NULL. 'SMITH'. upd_Date.DELETE FROM hr. That's it! It's really not that much work to capture and apply changes using Oracle 10g.employees SET salary=5999 WHERE employee_id=206. Of course. sqlplus hr/hrINSERT INTO hr.employee_auditORDER BY employee_id. last_name. It takes a few seconds for the data to make it to the logs and then back into the system to be applied. BEGINDBMS_APPLY_ADM.COMMIT.COM'.START_APPLY(apply_name => 'apply_emp'). Run this query until yousee data (remembering that it is not instantaneous): SELECT employee_id.employee_auditORDER BY employee_id. 'JOHN'. actionFROM hr. 777.employees WHERE employee_id=207.DELETE FROM hr.COMMIT. it's a little bit more work tocross database instances. The less code I have to write. upd_Date.END. first_name.
use global_name. select * from global_name. BEGINDBMS_AQADM. Download and install EE.sqlplus strmadmin/strmadmin View the XML LCR that we inserted during the capture process: set long 9999set pagesize 0select * from streams_monitor. That's it! It's really not that much work to capture and apply changes using Oracle 10g. Of course.grant dba to strmadmin. You need to create an AQ table.One of the things that amazes me is how little code is required to accomplish this.ORCL must be in archivelog mode to run CDC.You need two Oracle instances for this. The less code I have to write.anydata'. the less code I have to maintain.multiple_consumers => TRUE.dbf' size 25M reuse autoextend on maxsize unlimited. It works for me!As I said. You do not physically need two machines to getthis to work. That's what the code below does. AQ queue and then start the queue. SECOND does not need archivelog mode. If you are not using the same names for your databases and you are not sure of theexact name of your databases (including domain). ORCL will be my source instance and SECOND will be my target instance. I made a few minor changes in case you are running both instances on a single PC: sqlplus / as sysdbacreate tablespace streams_second_tbs datafile 'c:\temp\stream_2_tbs. you can download EE and use the DBCA to run two database instances. You should already have the CDC codefrom the last article running in ORCL. I used a 10g instance as my source (required) and a 9i database as my target (youcould also use a 10g instance here). Connect as strmadmin. I called my first instance ORCL (how creative!) and I called my second instanceSECOND.compatible .You already created your streams admin user in ORCL so now do the same thing in SECOND.queue_payload_type => 'sys.e. Having two databases running ona single PC in archivelog mode can really beat up a poor IDE drive. If you have 1 GB or more of RAM on your PC. The code below is mostly thesame code that you ran on ORCL. AQ 10g to 9i NOTE: Streams is not available with Oracle 10g XE.CREATE_QUEUE_TABLE(queue_table => 'lrc_emp_t'. it's a little bit more work tocross database instances. i. but it's not that much.create user strmadmin identified by strmadmindefault tablespace streams_second_tbsquota unlimited on streams_second_tbs.
So.We're almost done with the propagation now. you will create a propagationschedule.GETOBJECT(lcr).lrc_emp_q'.world'.command VARCHAR2(30).streams_name => 'orcl_2_second'.=> '8.world'). create a link: CREATE DATABASE LINK orcl. while you're in SECOND. Log into ORCL as strmadmin and run the exact same command there.insert into streams_monitor (txt_msg) values (command ||DBMS_STREAMS. CREATE DATABASE LINK second. We are going to modify the EMP_DML_HANDLER (from above) so that weget an enqueue block just above the execute statement: CREATE OR REPLACE PROCEDURE emp_dml_handler(in_any IN ANYDATA) ISlcr SYS. now we have running queues in ORCL and SECOND.world CONNECT TO strmadminIDENTIFIED BY strmadminUSING 'orcl.source_database => 'orcl. You DO NOT need to run this in SECOND.Get the object command typecommand := lcr.DBMS_AQADM.CREATE_QUEUE(queue_name => 'lrc_emp_q'.rc PLS_INTEGER.-.END.include_ddl => FALSE. Most of the setup for this is exactly the same betweenthe two instances.-- .include_dml => true.Access the LCR rc := in_any.-.GET_COMMAND_TYPE(). You have to have one from ORCL to SECOND but for debugging.employees'.source_queue_name => 'strmadmin.LCR$_ROW_LIST.old_values SYS.START_QUEUE (queue_name => 'lrc_emp_q').DBMS_AQADM.destination_queue_name => 'strmadmin./ This tells the database to take the data in the local lrc_emp_q and send it to the named destination queue.world'.CONVERT_LCR_TO_XML(in_any) ).END. Create your link on this side also.queue_table => 'lrc_emp_t'). I like a link in both.LCR$_ROW_RECORD. Ok. While you are logged into ORCL.ADD_TABLE_PROPAGATION_RULES(table_name => 'hr.world CONNECT TO strmadminIDENTIFIED BY strmadminUSING 'email@example.com').world'. BEGINDBMS_STREAMS_ADM.I am inserting the XML equivalent of the LCR into the monitoring table.BEGIN-./ You also need to create a database link.
-.We then enqueued our LCR as an ANYDATA datatype.'strmadmin. ANYDATA.END.message_handle RAW(16). .-. 'UPD_DATE'.Add a SYSDATE value for the timestamp columnlcr.END.Set the object_name in the row LCR to EMP_DELlcr. We will use that name to dequeue the record in the SECOND instance.aq$_agent( 'anydata_subscriber'.SET_COMMAND_TYPE('INSERT'). You should have as many records there as you inserted.convertObject(lcr). You can query DBA_PROPAGATION to see if you have any propagation errors. 'UPDATE') THEN-.lcr.. old_values).msgid => message_handle).commit.FORMAT_ERROR_STACK ).recipient_list := recipients.Set the command_type in the row LCR to INSERTlcr.Set the old values in the row LCR to NULLlcr.I put the exception handler there in case there are any problems with our enqueue. 'ACTION'.payload => anydata. ANYDATA.enqueue_options => enqueue_options.SET_VALUES('old'.lrc_emp_q'.Make the changeslcr. Propagation is where I have the mosttroubles.employees table and commit them.GET_VALUES('old').There are not a lot of moving parts so there aren't many things that will go wrong. -.world'.ADD_COLUMN('new'.Add a user value for the timestamp columnlcr. Then log into strmadmin@second andselect * from the lrc_emp_t table.enqueue_options_t.GET_EXTRA_ATTRIBUTE('USERNAME') ).lrc_emp_q@second.SET_VALUES('new'.SET_OBJECT_NAME('EMPLOYEE_AUDIT').Get the old values in the row LCR old_values := lcr.-.message_properties DBMS_AQ.aq$_recipient_list_t.Set the new values to the old values for update and deleteIF command IN ('DELETE'. DECLARE enqueue_options DBMS_AQ.ConvertDate(SYSDATE)).EXECUTE(true).That's it for moving the data from 10g to 9i.DBMS_AQ.NULL).-. We created a subscriber (that's the name of theconsumer).ADD_COLUMN('new'.ENQUEUE( queue_name => 'strmadmin.-.EXCEPTION WHEN OTHERS THEN insert into streams_monitor (txt_msg)values ('Anydata: ' || DBMS_UTILITY. 'user_name'. recipients DBMS_AQ.ConvertVarChar2(command)). NULL).Add an action columnlcr.That's all it takes.message_properties_t./ The declaration section above created some variables required for an enqueue.Set the old values in the row LCR to the new values in the row LCR lcr.END IF.ADD_COLUMN('new'.message_properties => message_properties.BEGIN recipients(1) := sys.-. Insert some records into the HR.message_p roperties.
owner. it is two table collections of an object type embedded within another object type. The old and new values are exactly the same as :old and :new in a trigger. 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). Normally.102%2 Fb14258%2Ftoc. and command type) at the toplevel. The important thing to note is the constructor.http://www. you can test specific data issues a lot easier. The LCR has information about what the object is as well as theold and new values. http://downloadeast. NOTE: Everything below is specifically for a ROW type LCR as opposed to a DDL type LCR. it might look something like: Figure 1 . In this case. So I decided to write one.lcr$_row_record METHOD------STATIC FUNCTION CONSTRUCT RETURNS LCR$_ROW_RECORDArgument Name Type In/Out Default?----------------------------.I tend to think of things like this in a relational format. First a little definition.Anyway.Manual Creation of an LCR This part of the paper is not required to move data from 10g to 9i.oracle. An artiste I am not. Beneath that is column and data information in a name/value pair collection. Actually. You canget the details of that from the documentation in thesupplied PL/SQL Packages and Types documentation. object_name.LCR Structure Excuse the poor diagram.----------------------.oracle.htm&remark=portal+%28Application+development%29The short story is that an LCR stored object level information (database. If you have manual control of your LCR.htmAn LCR is an object type. The concepts would be thesame but the specificcode would change. If I put it in database terms.com/pls/db102/to_toc?pathname=appdev.You can do a describe to see the methods as well as view the documentation. It is very handy if you need to manually create test datathough.You can see the definition of the LCR format by viewingthe LCR XML Schema. I (and a coworker) searched the web and looked through various documentation but was not able to find a concisedescription of how to go about creating an LCR manually.-----. I recently had the need to create some test datain LCR format. a constructor has the same name as the object type. SQL> desc sys.101/b10727/ap_xmlsc.com/docs/cd/B14117_01/server.--------SOURCE_DATABASE_NAME VARCHAR2 IN . they chose to name it CONSTRUCT.
odtug.----------------------. populating the test LCR is relatively straight-forward.-----.Just for your info. SQL> desc sys.com9 K a l 2 0 0 O e 7 i D d T o U s G c o p e Excuse the poor diagram.--------SOURCE_DATABASE_NAME VARCHAR2 IN COMMAND_TYPE VARCHAR2 INOBJECT_OWNER VARCHAR2 INOBJECT_NAME VARCHAR2 INTAG RAW IN DEFAULTTRANSACTION_ID VARCHAR2 IN DEFAULTSCN NUMBER IN DEFAULTOLD_VALUES LCR$_ROW_LIST IN DEFAULTNEW_VALUES LCR$_ROW_LIST IN DEFAULT Based on that info. they chose to name it CONSTRUCT.lcr$_row_record METHOD------STATIC FUNCTION CONSTRUCT RETURNS LCR$_ROW_RECORDArgument Name Type In/Out Default?----------------------------.You can do a describe to see the methods as well as view the documentation. Normally. An artiste I am not. In this case.www. a constructor has the same name as the object type. . The important thing to note is the constructor. the type LCR$_ROW_LIST is a collection of LCR$_ROW_UNIT.
c1.END IF.set_command_type(p_command).Those types are also documented in thereference guide I mentioned above.lcr$_row_record.v_lcr.Construct the LCR v_lcr := sys.-.construct(source_database_name => v_database.You can override the values in the constructor by calling these methods v_lcr.AnyData. That type has it's ownrules and deserves a blog entry all to itself.data_typeWHEN 'VARCHAR2'THENv_lcr. c1.lcr$_row_record.Loop through the columns and add new and old valuesFOR c1 IN (SELECT column_name.p_table_name IN all_tables.-. c1.add_column('old'.I'm going to keep this example fairly simpleCASE c1.You would expand this for all data types -. 'DELETE')THENRETURN v_lcr.table_name%TYPE.owner%TYPE.v_lcr.set_source_database_name(v_database).This could be parameterized SELECT global_nameINTO v_databaseFROM global_name.convertVarChar2(TO_CHAR(NULL))).WHEN 'DATE'THENv_lcr.Get the database name-.column_name. . You will not want to access those directly though. 'UPDATE'.add_column('old'. c1.BEGIN-.add_column('new'.convertVarChar2(TO_CHAR(NULL))).AnyData.AnyData. data_typeFROM all_tab_columnsWHERE owner = p_table_ownerAND table_name = p_table_nameORDER BY column_id )LOOP-.One thing to remember is that the data values that you are dealing with are sys.add_column('new'.-.column_name.column_name.object_owner => p_table_owner.object_name => p_table_name ).v_lcr.column_name.-.lcr$_row_record ASv_lcr sys. You can use the builtinLCR$_ROW_RECORD methods to populate those fields.command_type => p_command. CREATE OR REPLACE FUNCTION create_lcr( p_table_owner IN all_tables. sys. sys.Create an anydata based on column data type.verify the command typeIF p_command NOT IN ('INSERT'. sys.set_object_name(p_table_name).Here is a function that will create an empty LCR for you automatically for any table.AnyData data types.p_command IN VARCHAR2 )RETURN sys.s et_object_owner(p_table_owner).v_database global_name.global_name%TYPE.v_lcr. Once you have the LCR you can modifythe values to suit.v_lcr.convertDate(TO_DATE(NULL))).
-.ittoolbox.v_lcr.accessVarchar2(v_lcr.PUT_LINE('New First Name: ' || sys. Object Owner: HR.WHEN 'NUMBER'THENv_lcr.anyData.v_lcr.AnyData. sys.com/oracle/guide. 'INSERT' ).END LOOP.Thank you for taking the time to read this paper. Old First Name: ' || sys.Display Some ValuesDBMS_OUTPUT. http://blogs.get_object_name() ||'. You can contact me in the future at firstname.lastname@example.org or come by andread my blog.set_value('new'. 'first_name'. Command: INSERT New First Name: Lewis. 'first_name')) ).convertNumber(TO_NUMBER(NULL))).anyData. Command: ' || v_lcr. An Expert's Guide to Oracle Technology.column_name.-. Object Name: EMPLOYEES. you might write something like the following: DECLAREv_lcr sys. The output from this is: Database: XE. Object Name: ' || v_lcr.END.sys. sys. Object Owner: ' || v_lcr. sys.AnyData.add_column('new'.add_column('old'.PUT_LINE('Database: ' || v_lcr.BEGIN v_lcr := create_lcr( 'HR'.convertDate(TO_DATE(NULL))). Old First Name: George And that's all there is to it.DBMS_OUTPUT.convertVarchar2('Lewis')).get_object_owner() ||'.accessVarchar2(v_lcr.convertVarchar2('George')). 'EMPLOYEES'. 'first_name'. c1. sys.END.column_name.get_command_type() ). 'first_name')) ||'. c1.Set some valuesv_lcr. To call this function and manipulate it.AnyData.get_value('old'. .RETURN v_lcr.anyData.END CASE.lcr$_row_record.get_source_database_name() ||'.anyData.set_value('old'.get_value('new'.convertNumber(TO_NUMBER(NULL))).