Online reorganisation of tables using DBMS_REDEFINITION

Last updated on September 3rd, 2016 at 01:34 am

We often need to perform reorganization of the tables . We have to do offline by taking system down and it consumes lot of time.we can perform an online reorganization of tables using DBMS_REDEFINITION. This feature is available from 9i.

How Oracle internally achieve this online reorganization
To achieve this online reorganization, incrementally maintainable local materialized views are used. Snapshot logs need to be defined on the master  tables to support incrementally maintainable materialized views. These logs keep track of the changes to the master tables and are used by the materialized views during refresh synchronization. To keep table indexes and privileges you must use the copy_table_dependents procedure

There are some Restrictions

Tables with the following characteristics cannot be redefined online:
– [9.0.1]Tables with no primary keys
– Tables that have materialized view logs defined on them
– [9i] Tables that are materialized view container tables and AQ tables
– [10g] Tables that are replicated in an n-way master configuration can
be redefined, but horizontal subsetting (subset of rows in the table),
vertical subsetting (subset of columns in the table), and column
transformations are not allowed.
– The overflow table of an IOT table
– Tables with fine-grained access control (row-level security)
– Tables with BFILE columns
– Tables with LONG columns can be redefined online, but those columns must be
converted to CLOBS. Also, LONG RAW columns must be converted to BLOBS.
Tables with LOB columns are acceptable.
– Tables in the SYS and SYSTEM schema
– Temporary tables
Other restrictions:
– A subset of rows in the table
– Only simple deterministic expressions, sequences, and SYSDATE can be used
when mapping the columns in the interim table to those of the original table.
For example, subqueries are not allowed.
– If new columns are being added with no column mappings, then they must not
be declared NOT NULL until the redefinition is complete.
– There cannot be any referential constraints between the table being redefined
and the interim table.
– Table redefinition cannot be done NOLOGGING.
– [10g] For materialized view logs and queue tables, online redefinition is
restricted to changes in physical properties.
– You cannot convert a nested table to a VARRAY.

Summary of DBMS_REDEFINITION Subprograms:

This procedure determines if a given table can be reorganized online. This is the first step of the online reorganization process. If the table is not a  candidate for online redefinition, an error message is raised.


DBMS_REDEFINITION.can_redef_table (
uname IN VARCHAR2,
tname IN VARCHAR2);


This procedure initiates the reorganization process. After verifying that the table can be reorganized online, you create an empty interim table (in the same schema as the table to be reorganized) with the desired attributes of the post-reorganization table.


DBMS_REDEFINITION.start_redef_table (
uname IN VARCHAR2,
orig_table IN VARCHAR2,
int_table IN VARCHAR2,
col_mapping IN VARCHAR2 := NULL);

START_REDEF_TABLE Procedure Parameters:

Parameter Description
———- ————
uname The schema name of the tables.
orig_table The name of the table to be reorganized.
int_table The name of the interim table.
col_mapping The mapping information from the columns in the interim
table to the columns in the original table. (This is similar
to the column list on the SELECT clause of a query.) If NULL,
all the columns in the original table are selected and have
the same name after reorganization.

This procedure completes the reorganization process. Before this step you can create new indexes, triggers, grants, and constraints on the interim table. The
referential constraints involving the interim table must be disabled. After completing this step, the original table is locked briefly during this procedure.

DBMS_REDEFINITION.finish_redef_table (
uname IN VARCHAR2,
orig_table IN VARCHAR2,
int_table IN VARCHAR2);

FINISH_REDEF_TABLE Procedure Parameters:

Parameter Description
——— ————
uname The schema name of the tables.
orig_table The name of the table to be reorganized.
int_table The name of the interim table.

This procedure keeps the interim table synchronized with the original table. This step is useful in minimizing the amount of synchronization needed to be
done by finish_reorg_table before completing the online reorganization. This procedure can be called between long running operations (such as create index)
on the interim table to sync it up with the data in the original table and speed up subsequent operations.


DBMS_REDEFINITION.sync_interim_table (
uname IN VARCHAR2,
orig_table IN VARCHAR2,
int_table IN VARCHAR2);

SYNC_INTERIM_TABLE Procedure Parameters:

Parameters Description
———- ————
uname The schema name of the tables.
orig_table The name of the table to be reorganized.
int_table The name of the interim table.
Let us take a example on Online reorganisation of tables using Dbms_redefination

create table SCOTT.EXAMPLE_TEST (name varchar2(20),
description varchar2(200),
value varchar2(20)) tablespace USERS;

alter table SCOTT.EXAMPLE_TEST add constraint test_pk primary key (name);
create index SCOTT.EXAMPLE_TEST_idx on test (value);

insert into SCOTT.EXAMPLE_TEST values(‘rocket’,’rocket works on momentum principle’,’mv’);
insert into SCOTT.EXAMPLE_TEST values(‘laser’,’it is a focus beam’,’laser’);
insert into SCOTT.EXAMPLE_TEST values(‘comet’,’this is comet’,’tail’);
insert into SCOTT.EXAMPLE_TEST values(‘galaxy’,’Milky way is a name of galaxy ‘,’solar’);
Suppose we want to reorganize the table SCOTT.EXAMPLE_TEST from tablespace TOOLS to USERS

Step 01:
Check Whether table SCOTT.EXAMPLE_TEST can be redefined online.

Set serveroutput on
SQL> EXECUTE DBMS_REDEFINITION.CAN_REDEF_TABLE(‘scott’,’EXAMPLE_TEST’, dbms_redefinition.cons_use_pk);
PL/SQL procedure successfully completed.
Step 02:
Create an interim table SCOTT.EXAMPLE_TEST_1 which holds the same structure as the original table except constraints, indexes, triggers but add the new storage attributes
create table SCOTT.EXAMPLE_TEST_1 (name varchar2(20),
description varchar2(200),
value varchar2(20)) tablespace TOOLS;
Later this table can be dropped.

Step 03:
Initiates the redefinition process by calling dbms_redefinition.start_redef_table procedure.
SQL> exec dbms_redefinition.start_redef_table(‘SCOTT’, ‘EXAMPLE_TEST’, ‘EXAMPLE_TEST_1’);
PL/SQL procedure successfully completed.
If any error encountered while running this process then abort the redifinition process first using below statements:

If we receive issues during this process, then we need to first do the abort option

SQL> exec dbms_redefinition.abort_redef_table(‘SCOTT’, ‘EXAMPLE_TEST’, ‘EXAMPLE_TEST_1’);

Resolve the error and restart the redefinition process.

Step 04:
Copies the dependent objects of the original table onto the interim table. The COPY_TABLE_DEPENDENTS Procedure clones the dependent objects of the table being redefined onto the interim table and registers the dependent objects. But this procedure does not clone the already registered dependent objects.
In fact COPY_TABLE_DEPENDENTS Procedure is used to clone the dependent objects like grants, triggers, constraints and privileges from the table being redefined to the interim table which in fact represents the post-redefinition table.

error_count PLS_INTEGER := 0;
dbms_redefinition.copy_table_dependents(‘SCOTT’, ‘EXAMPLE_TEST’, ‘EXAMPLE_TEST_’,1, TRUE, TRUE, TRUE, FALSE,error_count);
DBMS_OUTPUT.PUT_LINE(‘errors := ‘ || TO_CHAR(error_count));
COPY_TABLE_DEPENDENTS procedure to the DBMS_REDEFINITION package. This new procedure optionally copies all indexes, grants, triggers, constraints and privileges from the source table to the interim table.

All referential integrity constraints cloned by the procedure are created in a disabled state, then automatically enabled after the redefinition is complete. The triggers cloned to the interim table are disabled until the redefinition is completed. Once the redefinition is complete, all cloned objects are renamed to the original names used by they objects they were cloned from.
Step 5: Optionally, synchronize the interim table “EXAMPLE_TEST_1”.


step 06:
Completes the redefinition process by calling FINISH_REDEF_TABLE Procedure.
SQL> exec dbms_redefinition.finish_redef_table(‘SCOTT’, ‘EXAMPLE_TEST’, ‘EXAMPLE_TEST_1’);
Step 06:

Step 07:
Check index status by
SQL> select index_name , status from user_indexes where table_name=’EXAMPLE_TEST’;
 Step 08:
Check the constraints
SELECT owner,constraint_name,constraint_type,serach_condition,status
FROM dba_constraints WHERE table_name=’EXAMPLE_TEST’;
Check for the NOT NULL constraints. Any constraint defined at table level on original table is now enforced by system generated constraints.
Step 09:
Drop the interim table EXAMPLE_TEST_1.

Common Issues with DBMS_redifination


ORA-00904: “<column name>”: invalid identifier

Will occur if a table that is being redefined has an index, constraint or trigger on a column that is not included in the redefinition (ie the interim table) and DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS is executed


DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS attempts to create the index on the non included columm


At present there is no way to force the exclusion of indexes, constraints or triggers ( for copy of dependencies) on columns not included in an interim table

There are several solutions that can be tried

a) Set the IGNORE_ERRORS parameter to TRUE before executing the procedure
b) Do not copy the index, constraint or trigger when doing COPY_TABLE_DEPENDENTS using false for these COPY_TABLE_DEPENDENTS.Once the redefinition is finished ,recreate the indexes that should have been copied
c) Drop the index, constraint or trigger on the columns that are not included in the interim table before running COPY_TABLE_DEPENDENTS
d) Manually create the index, constraint or trigger on the interim table then use REGISTER_DEPENDENT_OBJECT to register the mapping of the master to the interim table

Leave a Reply