An online table redefinition is a copy followed by a switch. Oracle lets the application keep writing during most of the copy, then needs an exclusive lock to finish. The work I plan most carefully is the handling of dependent objects and the last few seconds of the operation.
This Oracle Database 19c lab converts a padded CHAR reference to VARCHAR2 and trims the values during the copy. It uses a disposable schema in a PDB, with tablespace quota and the privileges for own-schema redefinition. Check feature availability for your edition and review Oracle’s table restrictions before reusing the pattern. Changing padding semantics can affect comparisons and application code. That behavior is part of the acceptance test.
Set up the source and check eligibility
Run this as the owner of the empty lab schema in session A. Use the same user and PDB in session B.
CREATE TABLE orders_demo (
order_id NUMBER CONSTRAINT orders_demo_pk PRIMARY KEY,
external_ref CHAR(12) NOT NULL,
amount NUMBER(10,2) NOT NULL
);
CREATE INDEX orders_demo_ref_i ON orders_demo (external_ref);
INSERT INTO orders_demo VALUES (1, 'INV-100', 20);
COMMIT;
BEGIN
DBMS_REDEFINITION.CAN_REDEF_TABLE(
uname => USER,
tname => 'ORDERS_DEMO',
options_flag => DBMS_REDEFINITION.CONS_USE_PK
);
END;
/Prefer a primary key or a qualifying unique key with all columns declared NOT NULL. If there is no suitable key, check the restrictions for CONS_USE_ROWID and run the eligibility test with that mode before proceeding. Use the same mode at startup. ROWID mode is unavailable for index-organized tables and leaves an internal column marked unused after completion. Plan any physical cleanup separately.
Create the interim table and start the copy
Create the desired shape in the same schema. The original primary key and secondary index will be handled in the dependent-object step.
CREATE TABLE orders_demo_int (
order_id NUMBER,
external_ref VARCHAR2(24) NOT NULL,
amount NUMBER(10,2) NOT NULL
);
BEGIN
DBMS_REDEFINITION.START_REDEF_TABLE(
uname => USER,
orig_table => 'ORDERS_DEMO',
int_table => 'ORDERS_DEMO_INT',
col_mapping => 'order_id order_id, RTRIM(external_ref) external_ref, amount amount',
options_flag => DBMS_REDEFINITION.CONS_USE_PK
);
END;
/The mapping works like a select list with a destination name for each expression. If the original has a virtual column, review its expression and dependencies in the interim definition. A virtual destination derives its value from its base columns. Keep it out of an explicit mapping that writes values, and check any dependent index or constraint in the rehearsal.
This duplicate copy consumes space, creates work for redo and undo, and can affect replicas or other jobs sharing the system. Estimate space for the table and indexes and observe the copy under representative write traffic.
Copy dependent objects and check the result
SET SERVEROUTPUT ON
DECLARE
copy_errors PLS_INTEGER;
BEGIN
DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS(
uname => USER,
orig_table => 'ORDERS_DEMO',
int_table => 'ORDERS_DEMO_INT',
copy_indexes => DBMS_REDEFINITION.CONS_ORIG_PARAMS,
copy_triggers => TRUE,
copy_constraints => TRUE,
copy_privileges => TRUE,
ignore_errors => TRUE,
num_errors => copy_errors
);
DBMS_OUTPUT.PUT_LINE('Dependent object errors: ' || copy_errors);
END;
/Treat any nonzero error count as a stop before cutover. With the appropriate privileges, inspect DBA_REDEFINITION_ERRORS for the failed object and its DDL. Correct the cause and retry the copy or create and register an intentionally changed replacement with REGISTER_DEPENDENT_OBJECT. In a production rehearsal, inventory triggers, grants, constraints, indexes, and any materialized view logs beforehand. Compare the finished set with that inventory.
Rehearse the final lock in two sessions
In session B, insert a row into the original and commit. Back in session A, sync and verify that the new row appears in the interim table.
-- Session B
INSERT INTO orders_demo VALUES (2, 'INV-101', 30);
COMMIT;
-- Session A
EXEC DBMS_REDEFINITION.SYNC_INTERIM_TABLE(USER, 'ORDERS_DEMO', 'ORDERS_DEMO_INT');
SELECT order_id, external_ref, LENGTH(external_ref) AS ref_length
FROM orders_demo_int
ORDER BY order_id;Both references should now have a length of seven. To rehearse contention, update a row in session B and leave the transaction open. In session A, attempt the finish with a short timeout.
BEGIN
DBMS_REDEFINITION.FINISH_REDEF_TABLE(
uname => USER,
orig_table => 'ORDERS_DEMO',
int_table => 'ORDERS_DEMO_INT',
dml_lock_timeout => 5
);
END;
/The open transaction should prevent the finish from acquiring the required lock within the timeout. End the lab transaction in B, check the redefinition state, then sync and retry the finish. In production I would time the final sync and agree on how to handle long transactions before opening the cutover window. To abandon a redefinition that started but has yet to finish, use ABORT_REDEF_TABLE and confirm cleanup. A post-cutover rollback requires its own planned workflow.
After finishing, check the column type and values through ORDERS_DEMO, verify the primary key and secondary index, and submit an insert using the application’s normal column list. Check dependent objects and gather appropriate statistics before running a representative query against the new table. Retain the interim table until your acceptance criteria and cleanup plan allow you to remove it.
Oracle 19c online table redefinition guide · DBMS_REDEFINITION reference