This is the final part of four blogs which discuss some of the methods that can be used to copy tablespaces between databases. Part 1 worked through an example using export/import and part 2 used Datapump.
Part 3 discussed the use of RMAN (Recovery Manager) and in the final part we’ll be using DBMS_STREAMS_TABLESPACE_ADM to clone a tablespace.
For all of this work, our local database is called ORCL and our remote database is ORCL2.
If you have carried out the procedures in parts 1, 2 or 3 then run the commands below on the local and remote databases, otherwise skip these drop commands.
Drop tablespace TTS_TEST including contents and datafiles;
Drop tablespace TTS_TEST_IDX including contents and datafiles;
Drop user tts_user cascade;
Create Example Tablespaces in the local database
We’ll begin by creating some tablespaces in our local database. Run the commands below as SYSTEM.
CREATE TABLESPACE TTS_TEST DATAFILE
‘/u02/oradata/ORCL/TTS_TEST01.dbf’ size 10M autoextend on maxsize 100M;
CREATE TABLESPACE TTS_TEST_IDX DATAFILE
‘/u02/oradata/ORCL/TTS_TEST_IDX01.dbf’ size 10M autoextend on maxsize 100M;
Create an Example user in the local and remote databases
Now we’ll create an example user in our local and remote databases.
CREATE USER TTS_USER IDENTIFIED BY tester1 DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP;
GRANT CREATE SESSION, CREATE TABLE TO TTS_USER;
Next - just on the local database – we’ll run the commands:
ALTER USER TTS_USER DEFAULT TABLESPACE TTS_TEST;
ALTER USER TTS_USER QUOTA UNLIMITED ON TTS_TEST;
ALTER USER TTS_USER QUOTA UNLIMITED ON TTS_TEST_IDX;
Create an Example Table in the local database
We can then create a very basic table with an associated index. The table and index will be in separate tablespaces and will use the default storage options for the tablespace.
CREATE TABLE TTS_USER.T1 (C1 NUMBER(10) , C2 VARCHAR2(10) )
TABLESPACE TTS_TEST ;
ALTER TABLE TTS_USER.T1 ADD (CONSTRAINT t1_pk PRIMARY KEY(C1) USING INDEX TABLESPACE TTS_TEST_IDX);
INSERT INTO TTS_USER.T1 VALUES (1,’TESTROW1′);
INSERT INTO TTS_USER.T1 VALUES (2,’TESTROW2′);
COMMIT;
Using DBMS_STREAMS_TABLESPACE_ADM to clone tablespaces
You can create a clone of an existing tablespace and then attach it to a remote database. (These methods would normally be used for tablespace versioning and are available from 10g Release 1).
Note: It is also possible to detach and then re-attach a tablespace, but as we wish to keep our original tablespace in this scenario, the DETACH option will not be discussed.
(You should be aware that you can’t use these procedures for objects owned by SYS).
CLONE TABLESPACE
(i) Create an operating system and corresponding database directory for the new version of the tablespace.
e.g. mkdir /u01/oradata/ORCL/copy_datafiles
Then login to the database as SYS as sysdba and run the commands:
CREATE OR REPLACE DIRECTORY OBJ_COPY AS ‘/u01/oradata/ORCL/copy_datafiles’;
This will be used as the setting for the TABLESPACE_DIRECTORY_OBJECT parameter later on.
(ii) Create a clone of the tablespace. (These commands also add a new version of the tablespace to a filegroup repository called TTS_TEST_FG, which is created automatically.
DECLARE
tbs_set DBMS_STREAMS_TABLESPACE_ADM.TABLESPACE_SET;
BEGIN
tbs_set(1) := ‘TTS_TEST’;
tbs_set(2) := ‘TTS_TEST_IDX’;
DBMS_STREAMS_TABLESPACE_ADM.CLONE_TABLESPACES(
tablespace_names => tbs_set,
tablespace_directory_object => ‘OBJ_COPY’,
file_group_name => ‘TTS_TEST_FG’,
version_name => ‘V1′);
END;
/
Notes: version_name and file_group_name can be set to anything you like. If you need to change the name of the datafile later, then you can use DBMS_FILE_GROUP.ALTER_FILE.
If you get any errors when running these procedures, run the query:
SELECT REASON FROM SYS.PLUGGABLE_SET_CHECK WHERE TS1_NAME IN (’TTS_TEST’, ‘TTS_TEST_IDX’);
(iii) A copy of the datafile, the export dumpfile and the logfile from this work will all go to the OBJ_COPY database directory. The datafile has been cloned, but is not attached to the database, as we can only have one version of a tablespace attached and online at any time.
(iv) We can query DBA_FILE_GROUP_TABLESPACES and DBA_FILE_GROUP_FILES for information on this new version of the tablespace.
ATTACH TABLESPACE
To be able to use the cloned tablespace in another database, we now need to attach the tablespace.
(i) You can use these procedures when working with tablespaces that have previously been processed by DBMS_STREAMS_TABLESPACE_ADM. DETACH_TABLESPACE and CLONE_TABLESPACE procedures, or with files created by the RMAN or Datapump export transport tablespace commands.
(ii) Copy the datafile to the datafile location of the remote database. Copy the export dumpfile to the DATA_PUMP_DIR directory of the remote database.
(iii) In the remote database, create a database directory pointing to the location of the new datafiles as SYS as sysdba:
CREATE OR REPLACE DIRECTORY OBJ_LOAD AS ‘/u02/oradata/ORCL2′;
This will be used as the setting for the TABLESPACE_DIRECTORY_OBJECT parameter later on.
(iv) Run the commands below against the remote database from the DATA_PUMP_DIR location, which contains the export dumpfile. This will attach the tablespace to the database. (Amend the dump_file.file_name parameter to match the name of your export dumpfile before running the commands).
set serveroutput on
DECLARE
tbs_files dbms_streams_tablespace_adm.file_set;
cvt_files dbms_streams_tablespace_adm.file_set;
dump_file dbms_streams_tablespace_adm.file;
dp_job_name VARCHAR2(30) := NULL;
ts_names dbms_streams_tablespace_adm.tablespace_set;
BEGIN
dump_file.file_name := ‘expdat22.dmp’;
dump_file.directory_object := ‘DATA_PUMP_DIR’;
tbs_files( 1).file_name := ‘TTS_TEST01.dbf’;
tbs_files( 1).directory_object := ‘OBJ_LOAD’;
tbs_files( 2).file_name := ‘TTS_TEST_IDX01.dbf’;
tbs_files( 2).directory_object := ‘OBJ_LOAD’;
dbms_streams_tablespace_adm.attach_tablespaces(
datapump_job_name => dp_job_name,
dump_file => dump_file,
tablespace_files => tbs_files,
converted_files => cvt_files,
tablespace_names => ts_names);
IF ts_names IS NOT NULL AND ts_names.first IS NOT NULL THEN
FOR i IN ts_names.first .. ts_names.last LOOP
dbms_output.put_line(’imported tablespace ‘|| ts_names(i));
END LOOP;
END IF;
END;
/
(v) If you query DBA_TABLESPACES, you should now see that the tablespaces have been added to your remote database. (Note: These commands won’t create a new file group in the remote database - they’ll just attach the tablespaces).
(vi) Finally we just need to put the tablespaces into read write mode.
ALTER TABLESPACE TTS_TEST READ WRITE;
ALTER TABLESPACE TTS_TEST_IDX READ WRITE;
Note: There is also a Transport Tablespace Wizard in Oracle Enterprise Manager for carrying out these tasks.
References
(i) Oracle documentation at: http://www.oracle.com/technology/documentation/database.html