DBMS_REDEFINITION으로 온라인 테이블 재정의(전체 흐름)
Oracle온라인 작업명령어
-- 1) Is it eligible? (use CONS_USE_ROWID if there is no PK)
EXEC DBMS_REDEFINITION.CAN_REDEF_TABLE('CUSTOMER_APP', 'CUSTOMER', DBMS_REDEFINITION.CONS_USE_PK);
-- 2) Interim table with the target structure: hash partitioned in this example
CREATE TABLE customer_app.customer_int (
id NUMBER, name VARCHAR2(100), city VARCHAR2(30))
PARTITION BY HASH (id) PARTITIONS 8;
-- 3) Start: initial copy + change tracking through an MV log
BEGIN
DBMS_REDEFINITION.START_REDEF_TABLE(
uname => 'CUSTOMER_APP',
orig_table => 'CUSTOMER',
int_table => 'CUSTOMER_INT',
col_mapping => NULL,
options_flag => DBMS_REDEFINITION.CONS_USE_PK);
END;
/
-- 4) Copy indexes, constraints, triggers, grants and statistics:
SET SERVEROUTPUT ON
DECLARE
l_errors PLS_INTEGER;
BEGIN
DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS(
uname => 'CUSTOMER_APP',
orig_table => 'CUSTOMER',
int_table => 'CUSTOMER_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('Error count: ' || l_errors);
END;
/
SELECT object_name, base_table_name, ddl_txt FROM dba_redefinition_errors;
-- 5) Close the gap before finishing (shortens finish; can be run several times):
EXEC DBMS_REDEFINITION.SYNC_INTERIM_TABLE('CUSTOMER_APP', 'CUSTOMER', 'CUSTOMER_INT');
-- 6) Swap under a short lock:
EXEC DBMS_REDEFINITION.FINISH_REDEF_TABLE('CUSTOMER_APP', 'CUSTOMER', 'CUSTOMER_INT', dml_lock_timeout => 60);
-- 7) Drop the interim table, which now holds the old structure:
DROP TABLE customer_app.customer_int PURGE;
-- On problems: EXEC DBMS_REDEFINITION.ABORT_REDEF_TABLE('CUSTOMER_APP', 'CUSTOMER', 'CUSTOMER_INT');
-- Status: SELECT * FROM dba_redefinition_status WHERE base_table_owner = 'CUSTOMER_APP';하는 일
애플리케이션이 계속 실행되는 동안 테이블 구조(파티셔닝, 컬럼 타입/순서, 테이블스페이스, 압축, LOB 설정)를 변경한다. 데이터는 interim 테이블로 복사되고, 변경은 MV log로 동기화되며, 마지막에 매우 짧은 락하에서 이름이 교체된다.
사용 시점
대형 비파티션 테이블을 다운타임 없이 파티셔닝하거나, 컬럼 타입을 변경하거나, BasicFile LOB을 SecureFiles로 전환하는 등 ALTER TABLE이 온라인으로 할 수 없는 변경용.
주의
Enterprise Edition이 필요하다(대상이 파티션 테이블이면 Partitioning 옵션도 필요). 이 과정에는 테이블 크기의 두 배 공간이 필요하며 redo와 MV log 오버헤드가 추가된다. FINISH는 짧은 배타 락을 잡으며 장시간 트랜잭션이 있으면 대기한다(dml_lock_timeout). DBA_REDEFINITION_ERRORS에서 COPY_TABLE_DEPENDENTS 오류를 항상 확인한다. FK가 NOVALIDATE로 복사될 수 있다. 먼저 테스트 환경에서 리허설한다.