Changing a Column's Data Type While the Application Keeps Writing: Online Table Redefinition on Exadata in Oracle Database@AWS
Some schema mistakes live for years. A column holding dates was created as VARCHAR2, every report converts it with TO_DATE, and the optimizer has no idea which values are close to each other. Changing the type looks simple, but ALTER TABLE ... MODIFY to a different data type only works on an empty column, and the classic workaround of adding a new column, copying the data and dropping the old one locks rows for hours on a large, busy table.
Online table redefinition solves this. The DBMS_REDEFINITION package copies the table into a new structure while the application keeps reading and writing, applies every change made during the copy, and then swaps the two tables with a lock of a few seconds. This post converts a text column to DATE on a large billing table on Exadata Database Service in Oracle Database@AWS.
The Use Case
The billing service runs on Amazon EKS and writes invoices to a two-node RAC database on Exadata Database Service in Oracle Database@AWS. The INVOICES table stores the issue time in ISSUED_AT as VARCHAR2(19), in the format YYYY-MM-DD HH24:MI:SS. Month-end reports filter on it with TO_DATE, so they cannot use the index on the column, and range estimates are poor. The table is written to around the clock, so a maintenance window long enough for a full table rebuild is not available.
Environment used in this runbook
Service: Exadata Database Service on Dedicated Infrastructure in Oracle Database@AWS
Region: AWS US East (N. Virginia), paired with OCI US East (Ashburn)
Database: Oracle Database 19c (19.24), PDB BILLPDB, two-node RAC
Table: BILLING.INVOICES, 62 GB, primary key on INVOICE_ID
Application: billing service in Amazon EKS, writing invoices continuously
Licence: online table redefinition is an Enterprise Edition feature, included in Exadata Database Service
How Online Redefinition Works
An interim table holds the new shape. You create an empty table with the target structure, here with ISSUED_AT as DATE.
The initial copy runs online. START_REDEF_TABLE copies all rows into the interim table, converting the column on the way, while the application keeps working on the original.
A materialized view log records changes. Every insert, update and delete made during the copy is logged and later applied to the interim table.
The swap is short. FINISH_REDEF_TABLE applies the last changes and exchanges the names of the two tables, holding an exclusive lock only for that moment.
Step 1: Measure the Table and Its Dependents
Find out how big the table is and which objects depend on it. The interim table needs the same amount of space again, and every index, constraint and trigger has to be copied to it.
SQL |
-- Connected to BILLPDB as a DBA COLUMN segment_name FORMAT A25 COLUMN object_type FORMAT A12
SELECT segment_name, ROUND(SUM(bytes)/1024/1024/1024, 1) AS gb FROM dba_segments WHERE owner = 'BILLING' AND segment_name = 'INVOICES' GROUP BY segment_name;
SELECT 'INDEX' AS object_type, COUNT(*) AS cnt FROM dba_indexes WHERE table_owner = 'BILLING' AND table_name = 'INVOICES' UNION ALL SELECT 'CONSTRAINT', COUNT(*) FROM dba_constraints WHERE owner = 'BILLING' AND table_name = 'INVOICES' UNION ALL SELECT 'TRIGGER', COUNT(*) FROM dba_triggers WHERE table_owner = 'BILLING' AND table_name = 'INVOICES'; |
Expected output |
SEGMENT_NAME GB
------------------------- ----------
INVOICES 62.4
OBJECT_TYPE CNT
------------ ----------
INDEX 4
CONSTRAINT 7
TRIGGER 1What to expect: A 62 GB table with four indexes, seven constraints and one trigger. The constraints are the primary key, a foreign key to CUSTOMERS and five NOT NULL constraints. Make sure the tablespace has room for a second copy of the table and its indexes before you start.
Step 2: Check That Every Value Converts
The conversion happens during the initial copy, and a single value that is not a valid date stops it with an error such as ORA-01839. VALIDATE_CONVERSION, available since Oracle Database 12.2, finds those rows in advance.
SQL |
SELECT COUNT(*) AS bad_rows FROM billing.invoices WHERE VALIDATE_CONVERSION(issued_at AS DATE, 'YYYY-MM-DD HH24:MI:SS') = 0;
COLUMN issued_at FORMAT A19
SELECT invoice_id, issued_at FROM billing.invoices WHERE VALIDATE_CONVERSION(issued_at AS DATE, 'YYYY-MM-DD HH24:MI:SS') = 0; |
Expected output |
BAD_ROWS
----------
3
INVOICE_ID ISSUED_AT
---------- -------------------
88120417 2026-02-30 09:14:00
91004466 2026-11-31 16:02:11
104233871 26-11-2026 08:45:00What to expect: Three rows out of hundreds of millions: two impossible dates and one in the wrong format, written by an old import job. Agree the correct values with the billing team and fix these rows before you continue. Finding them now costs seconds; finding them twenty minutes into the copy costs the whole copy.
Tip: Run this check again just before Step 5. If the application can still write bad values, add a CHECK constraint or fix the code first, or new bad rows will break the synchronisation later.
Step 3: Confirm the Table Can Be Redefined
CAN_REDEF_TABLE checks the table for features that prevent online redefinition. With CONS_USE_PK, the changes are tracked by primary key, which is the preferred method.
SQL |
BEGIN DBMS_REDEFINITION.CAN_REDEF_TABLE( uname => 'BILLING', tname => 'INVOICES', options_flag => DBMS_REDEFINITION.CONS_USE_PK); END; / |
Expected output |
PL/SQL procedure successfully completed.What to expect: The procedure completes without an error, so the table qualifies. If it cannot be redefined, the procedure raises an error that names the reason, for example a missing primary key, in which case you can use CONS_USE_ROWID instead.
Step 4: Create the Interim Table
Create an empty table with the target structure. Leave out indexes, constraints and triggers: Step 6 copies them from the original table.
SQL |
CREATE TABLE billing.invoices_int ( invoice_id NUMBER(12), customer_id NUMBER(10), issued_at DATE, amount NUMBER(12,2), currency VARCHAR2(3), status VARCHAR2(10) ) TABLESPACE billing_data; |
Expected output |
Table created.What this does: It creates the new shape of the table, identical to the original except for ISSUED_AT, which is now a DATE. The table gets the name of the original at the end of the process, and the original gets the name INVOICES_INT.
Step 5: Start the Redefinition
START_REDEF_TABLE creates the materialized view log on the original table and copies all rows into the interim table. The column mapping converts ISSUED_AT on the way. Parallel execution speeds up the copy on Exadata.
SQL |
ALTER SESSION FORCE PARALLEL DML PARALLEL 8; ALTER SESSION FORCE PARALLEL QUERY PARALLEL 8; SET TIMING ON
BEGIN DBMS_REDEFINITION.START_REDEF_TABLE( uname => 'BILLING', orig_table => 'INVOICES', int_table => 'INVOICES_INT', col_mapping => 'invoice_id invoice_id, customer_id customer_id, ' || 'TO_DATE(issued_at, ''YYYY-MM-DD HH24:MI:SS'') issued_at, ' || 'amount amount, currency currency, status status', options_flag => DBMS_REDEFINITION.CONS_USE_PK); END; / |
Expected output |
Session altered.
Session altered.
PL/SQL procedure successfully completed.
Elapsed: 00:21:37.42What this does: It copies all rows in about 22 minutes, while the billing service keeps writing new invoices to the original table. Those changes are recorded in the materialized view log and are not yet in the interim table. If something goes wrong from here on, DBMS_REDEFINITION.ABORT_REDEF_TABLE removes the log and leaves the original table as it was.
Step 6: Copy the Indexes, Constraints and Triggers
COPY_TABLE_DEPENDENTS creates every dependent object of the original table on the interim table, under temporary names, and can copy the optimizer statistics too. Check the error count it returns before going further.
SQL |
SET SERVEROUTPUT ON
DECLARE l_errors PLS_INTEGER; BEGIN DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS( uname => 'BILLING', orig_table => 'INVOICES', int_table => 'INVOICES_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 COUNT(*) AS redef_errors FROM dba_redefinition_errors WHERE base_table_name = 'INVOICES'; |
Expected output |
Errors: 0
PL/SQL procedure successfully completed.
Elapsed: 00:09:12.08
REDEF_ERRORS
------------
0
Elapsed: 00:00:00.03What to expect: All four indexes, seven constraints and the trigger are created on the interim table without errors, and the statistics are copied. The index on ISSUED_AT is now built on DATE values, which is what makes the month-end reports faster later. If the count is not zero, DBA_REDEFINITION_ERRORS shows the failing DDL and its error text.
Step 7: Synchronise the Interim Table
During the 30 minutes of copying and index building, the application kept writing. SYNC_INTERIM_TABLE applies those changes to the interim table, so the final step has little left to do.
SQL |
BEGIN DBMS_REDEFINITION.SYNC_INTERIM_TABLE( uname => 'BILLING', orig_table => 'INVOICES', int_table => 'INVOICES_INT'); END; / |
Expected output |
PL/SQL procedure successfully completed.
Elapsed: 00:00:48.19What this does: It applies about half an hour of invoice activity in under a minute. You can run it as often as you like, and running it once more just before Step 8 keeps the final lock as short as possible.
Step 8: Finish the Redefinition
FINISH_REDEF_TABLE applies the last changes, takes a short exclusive lock and swaps the two tables. The dml_lock_timeout parameter sets how many seconds it waits for running transactions to release the table.
SQL |
BEGIN DBMS_REDEFINITION.FINISH_REDEF_TABLE( uname => 'BILLING', orig_table => 'INVOICES', int_table => 'INVOICES_INT', dml_lock_timeout => 30); END; / |
Expected output |
PL/SQL procedure successfully completed.
Elapsed: 00:00:06.71What this does: In under seven seconds, INVOICES becomes the converted table, with all its indexes, constraints, triggers, grants and statistics under their original names. The billing service only notices a brief wait on a few inserts. If a long transaction holds the table for more than 30 seconds, the call fails with ORA-42042, nothing is lost, and you simply run it again.
Tip: Schedule this step outside the busiest minutes, and check with the application team that no long batch job is updating the table at the same time.
Step 9: Verify the New Structure
Check the column types and NOT NULL constraints of the redefined table, and look for objects that became invalid because they depend on the table.
SQL |
SET TIMING OFF COLUMN column_name FORMAT A12 COLUMN data_type FORMAT A10 COLUMN nullable FORMAT A8
SELECT column_name, data_type, nullable FROM dba_tab_columns WHERE owner = 'BILLING' AND table_name = 'INVOICES' AND column_name IN ('INVOICE_ID', 'ISSUED_AT', 'AMOUNT') ORDER BY column_id;
SELECT COUNT(*) AS invalid_objects FROM dba_objects WHERE owner = 'BILLING' AND status = 'INVALID'; |
Expected output |
COLUMN_NAME DATA_TYPE NULLABLE
------------ ---------- --------
INVOICE_ID NUMBER N
ISSUED_AT DATE N
AMOUNT NUMBER N
INVALID_OBJECTS
---------------
2What to expect: ISSUED_AT is now a DATE and keeps its NOT NULL constraint. Two PL/SQL objects that reference the table were invalidated by the swap. They usually recompile automatically on their next call, but it is better to recompile them now and to review any code that still applies TO_DATE to the column, because that code now converts a DATE to text and back.
Step 10: Recompile and Clean Up
Recompile the invalid objects in the schema and drop the interim table, which now holds the old structure with the old data.
SQL |
EXEC DBMS_UTILITY.COMPILE_SCHEMA(schema => 'BILLING', compile_all => FALSE)
SELECT COUNT(*) AS invalid_objects FROM dba_objects WHERE owner = 'BILLING' AND status = 'INVALID';
DROP TABLE billing.invoices_int PURGE; |
Expected output |
PL/SQL procedure successfully completed.
INVALID_OBJECTS
---------------
0
Table dropped.What this does: All objects in the schema are valid again, and the 62 GB of the old table are released. Keep the interim table for a day instead if the business wants to compare old and new values first; it is an ordinary table with the old VARCHAR2 column.
Summary
Online table redefinition changes the structure of a large, busy table with only a few seconds of locking. Measure the table and its dependents, and prove with VALIDATE_CONVERSION that every value converts before you start. Confirm the table qualifies with CAN_REDEF_TABLE, create the interim table, and start the copy with a column mapping that converts the data. Copy the dependents and check the error count, synchronise as often as needed, and finish with a dml_lock_timeout that protects the application. Verify the column types, recompile invalid objects and drop the old copy.
Comments