Home / Databases / Oracle

DatabasesRunbookAdvanced

Oracle online table redefinition with DBMS_REDEFINITION: partition, reorganise or change columns with seconds of locking

A runbook for DBMS_REDEFINITION on large, busy Oracle tables, covering pre-checks, the interim table, dependent objects, sync cadence, the final lock, rollback and the failure modes that strand a redefinition.

Deepak Bhardwaj7 min read

Applies toOracle Database 12.2, 19c and 23ai (Enterprise Edition for online redefinition), RDS for Oracle and OCI Base Database

When to use it

You need to change a large table that the application writes to all day. Typical changes are converting it to partitioned, moving it to a new tablespace with compression, changing a column type, or reordering columns. You can't take an outage, so a plain ALTER TABLE ... MOVE or a CTAS rebuild is ruled out.

Check the simpler options first:

Change Simplest online option (12.2+)
Move or compress a heap table ALTER TABLE t MOVE ONLINE TABLESPACE x COMPRESS ...
Non-partitioned → partitioned ALTER TABLE t MODIFY PARTITION BY RANGE (...) ... ONLINE UPDATE INDEXES (...)
Move one partition ALTER TABLE t MOVE PARTITION p ONLINE ...
Column type change, column reorder, IOT ↔ heap, combined changes, table on a release without the options above DBMS_REDEFINITION

DBMS_REDEFINITION copies the data into an interim table you define, keeps it in sync through a materialized view log, then swaps the two in the data dictionary under a short exclusive lock.

1. Pre-checks

-- Can it be redefined, and by which method?
BEGIN
  DBMS_REDEFINITION.CAN_REDEF_TABLE(
    uname        => 'APP',
    tname        => 'ORDERS',
    options_flag => DBMS_REDEFINITION.CONS_USE_PK);   -- or CONS_USE_ROWID if no PK
END;
/

It raises an error naming the blocker if the table isn't eligible. Common blockers are no primary key and no usable ROWID method, an existing MV log of a type that can't be reused, and some column types.

Capacity checks:

SELECT segment_name, segment_type, ROUND(SUM(bytes)/1024/1024/1024,1) gb
FROM dba_segments WHERE owner = 'APP' AND segment_name = 'ORDERS'
GROUP BY segment_name, segment_type;
  • Space: the target tablespace needs room for a full second copy plus its indexes.
  • Redo/archive: the initial copy generates redo roughly equal to the table size, unless the interim table is NOLOGGING, and NOLOGGING breaks Data Guard and recoverability until the next backup. With a standby, keep logging on and make sure the archive destination and transport can absorb it.
  • Undo and TEMP: index builds in COPY_TABLE_DEPENDENTS need TEMP sized for the biggest index sort.
  • Time: estimate from a test run on a clone. Copy speed is dominated by I/O and parallelism.

Warning

Never run this for the first time on production. Rehearse on a restored copy with the same data volume, and record how long each step took and how much redo it generated.

2. Create the interim table

Define the end state: partitioning, tablespace, compression and columns. Leave indexes, constraints and triggers off. They come later.

CREATE TABLE app.orders_int (
  order_id     NUMBER        NOT NULL,
  customer_id  NUMBER        NOT NULL,
  order_date   DATE          NOT NULL,
  status       VARCHAR2(20),
  amount       NUMBER(14,2)
)
PARTITION BY RANGE (order_date) INTERVAL (NUMTOYMINTERVAL(1,'MONTH'))
( PARTITION p_old VALUES LESS THAN (DATE '2024-01-01') )
TABLESPACE app_data_2
ROW STORE COMPRESS ADVANCED;      -- Advanced Compression option required

3. Start the redefinition

ALTER SESSION FORCE PARALLEL DML PARALLEL 8;
ALTER SESSION FORCE PARALLEL QUERY PARALLEL 8;

BEGIN
  DBMS_REDEFINITION.START_REDEF_TABLE(
    uname        => 'APP',
    orig_table   => 'ORDERS',
    int_table    => 'ORDERS_INT',
    col_mapping  => NULL,                          -- or 'order_id order_id, ...' for transforms
    options_flag => DBMS_REDEFINITION.CONS_USE_PK);
END;
/

This creates an MV log on ORDERS and does the initial copy. From this point every DML on the source also writes to the MV log. That overhead lasts until you finish or abort.

Monitor progress from another session:

SELECT * FROM v$online_redef;                -- 12.2+, progress of the current operation
SELECT sid, opname, sofar, totalwork, time_remaining
FROM v$session_longops WHERE time_remaining > 0;

4. Copy dependent objects

DECLARE
  l_errors PLS_INTEGER;
BEGIN
  DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS(
    uname            => 'APP',
    orig_table       => 'ORDERS',
    int_table        => 'ORDERS_INT',
    copy_indexes     => DBMS_REDEFINITION.CONS_ORIG_PARAMS,
    copy_triggers    => TRUE,
    copy_constraints => TRUE,
    copy_privileges  => TRUE,
    ignore_errors    => FALSE,
    num_errors       => l_errors,
    copy_statistics  => TRUE);
  DBMS_OUTPUT.PUT_LINE('errors: ' || l_errors);
END;
/

SELECT object_type, object_name, base_table_name, ddl_txt
FROM dba_redefinition_errors WHERE base_table_owner = 'APP';

To make indexes local on the new partitioned table, create them yourself on ORDERS_INT first, then register each one with DBMS_REDEFINITION.REGISTER_DEPENDENT_OBJECT so the copy skips it. Otherwise CONS_ORIG_PARAMS copies the original index definitions, which are global on a non-partitioned source.

5. Sync until the delta is small

BEGIN
  DBMS_REDEFINITION.SYNC_INTERIM_TABLE('APP', 'ORDERS', 'ORDERS_INT');
END;
/

Run it repeatedly, for example every few minutes in a loop or job, until it finishes quickly. The finish step does a final sync while it holds the lock, so a large backlog there means a long lock.

-- Rough backlog: rows in the MV log
SELECT log_table FROM dba_mview_logs WHERE master = 'ORDERS' AND log_owner = 'APP';
SELECT COUNT(*) FROM app.mlog$_orders;

6. Finish in a quiet window

BEGIN
  DBMS_REDEFINITION.SYNC_INTERIM_TABLE('APP', 'ORDERS', 'ORDERS_INT');
  DBMS_REDEFINITION.FINISH_REDEF_TABLE(
    uname             => 'APP',
    orig_table        => 'ORDERS',
    int_table         => 'ORDERS_INT',
    dml_lock_timeout  => 60);       -- seconds to wait for the lock instead of queuing forever
END;
/

FINISH_REDEF_TABLE takes an exclusive lock, applies the last changes and swaps the names. Without dml_lock_timeout it waits behind any open transaction on the table, and every new DML queues behind it. That is a self-made outage.

Caution

Long-running uncommitted transactions on the source block the finish. Check v$locked_object joined to v$session for ORDERS before you start the final step, and agree with the application team what may be killed.

7. Verify and clean up

SELECT partitioned, compression, tablespace_name FROM dba_tables
WHERE owner = 'APP' AND table_name = 'ORDERS';

SELECT object_name, object_type, status FROM dba_objects
WHERE owner = 'APP' AND status = 'INVALID';

SELECT constraint_name, status, validated FROM dba_constraints
WHERE owner = 'APP' AND table_name = 'ORDERS';

After the swap, ORDERS_INT holds the old data and structure. Keep it until the application is verified, then drop it. Recompile invalid objects with UTL_RECOMP.RECOMP_SERIAL('APP'). Compare index and constraint names on ORDERS with the pre-change list (behaviour varies with what you registered manually). Rename any that don't match your standards or what your monitoring expects.

Failure modes

Symptom Cause Fix
START_REDEF_TABLE fails partway Space, TEMP, ORA-01555 during the copy ABORT_REDEF_TABLE, fix the cause, restart
Rerun says the table is already being redefined A previous attempt was neither finished nor aborted DBMS_REDEFINITION.ABORT_REDEF_TABLE('APP','ORDERS','ORDERS_INT'); check DBA_REDEFINITION_STATUS
DML on the source got slower MV log maintenance on every change Expected. Keep the window between start and finish short
MV log grows without bound Nobody runs SYNC_INTERIM_TABLE Schedule syncs; watch MLOG$_ size
Finish waits or times out Open transactions hold locks Retry with dml_lock_timeout, or clear the blockers
Constraint copy errors Referencing FKs from child tables Read DBA_REDEFINITION_ERRORS, create the missing ones manually as NOVALIDATE then validate
Standby apply lag spikes Initial copy redo Throttle parallelism; plan around the batch window

Prevent it next time

  • Design partitioning and compression up front for tables expected to grow past a few hundred GB.
  • Keep a tested redefinition script template in source control, with the parameters, sync loop and finish timeout.
  • Run CAN_REDEF_TABLE against your largest tables now, so the blockers are known before you need the procedure.