DBAtlas · データベース運用アトラス

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設定)を変更する。データは中間表にコピーされ、変更はMVログで同期され、最後にごく短いロックの下で名前が入れ替えられる。

使用する場面

大規模な非パーティション表を停止なしでパーティション化する、列の型を変更する、BasicFile LOBをSecureFilesに変換するなど、ALTER TABLEではオンラインで行えない変更に。

注意

Enterprise Editionが必要(ターゲットがパーティション化されている場合はPartitioningオプションも)。処理には表の2倍の領域が必要で、REDOとMVログのオーバーヘッドが加わる。FINISHは短時間の排他ロックを取得し、長時間実行トランザクションがあると待機する(dml_lock_timeout)。COPY_TABLE_DEPENDENTSのエラーは必ずDBA_REDEFINITION_ERRORSで確認する。FKはNOVALIDATEでコピーされる場合がある。まずテスト環境でリハーサルする。

DBAtlas で開く →

関連項目