欢迎您关注我的公众号【尚雷的驿站】
****************************************************************************
公众号:尚雷的驿站
CSDN :https://blog.csdn.net/shlei5580
墨天轮:https://www.modb.pro/u/2436
PGFans:https://www.pgfans.cn/user/home?userId=4159
****************************************************************************
一、背景描述
最近,我需要对一套 Oracle 11g 数据库进行迁移。这次迁移不是将源库的所有表数据同步至目标库,而是只需要迁移部分表的数据。
此外,业务要求迁移过程中不能停机,并确保迁移前后的数据一致性,以便在失败时能够回退。因此,我采用了 Oracle GoldenGate (OGG) 来同步数据,并进行了双向同步的配置(关于双向同步的详细内容将在后续进行补充)。
不仅如此,更为苛刻的要求是源端和目标端的序列必须满足以下需求:
- 调整新库的序列和步长,并记录当前操作时间:
- 确保新库的序列号尾数为偶数
- 将所有序列的步长调整为2
- 导出调整后的序列信息,以确认调整是否成功
- 确认新库的正常执行后,调整旧库的序列和步长,并记录当前操作时间:
- 确保旧库的序列号尾数为奇数
- 将所有序列的步长调整为2
- 导出调整后的序列信息,以确认调整是否成功
尽管这些要求有些过分,但客户坚持这样要求。虽然这个数据库平时的业务量不大,但是迁移过程中不允许停机,必须实时同步数据,同时源端和目标端必须保持连接,还要保证应用程序可以花费相当长的时间来调整数据源。
通过以上序列的修改,我保证了当应用程序同时连接源端和目标端,并对数据进行修改时,不会因为序列问题导致数据冲突,从而影响了 OGG 的同步过程。因此,我费尽九牛二虎之力,最终编写了一个 PL/SQL 脚本,批量修改了源端和目标端的序列值,以使两端都能满足业务需求。
二、PL/SQL脚本
要分别在源端和目标端执行如下pl/sql脚本:
2.1 源端pl/sql
源端执行如下pl/sql脚本
sqlplus / as sysdba
-- 然后执行如下脚本
SET SERVEROUTPUT ON
DECLARE
CURSOR c_sequences IS
SELECT sequence_owner,sequence_name,increment_by,last_number
FROM dba_sequences
WHERE sequence_owner in ('XXX','XXX') AND (increment_by <> 2 or MOD(last_number, 2) != 1);
BEGIN
FOR r_sequence IN c_sequences LOOP
DECLARE
current_val NUMBER;
BEGIN
EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || r_sequence.sequence_owner||'.'||r_sequence.sequence_name || ' INCREMENT BY 1 NOCACHE';
EXECUTE IMMEDIATE 'SELECT ' || r_sequence.sequence_owner||'.'||r_sequence.sequence_name || '.NEXTVAL FROM DUAL' into current_val;
EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || r_sequence.sequence_owner||'.'||r_sequence.sequence_name || ' INCREMENT BY 2 CACHE 20';
DBMS_OUTPUT.PUT_LINE(r_sequence.sequence_name);
END;
END LOOP;
DBMS_OUTPUT.PUT_LINE('序列修改完成');
END;
/
2.2 目标端pl/sql
目标端执行如下pl/sql脚本
sqlplus / as sysdba
-- 然后执行如下脚本
SET SERVEROUTPUT ON
DECLARE
CURSOR c_sequences IS
SELECT sequence_owner,sequence_name,increment_by,last_number
FROM dba_sequences
WHERE sequence_owner in ('XXX','XXX') AND (increment_by <> 2 or MOD(last_number, 2) != 0);
BEGIN
FOR r_sequence IN c_sequences LOOP
DECLARE
current_val NUMBER;
BEGIN
EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || r_sequence.sequence_owner||'.'||r_sequence.sequence_name || ' INCREMENT BY 1 NOCACHE';
EXECUTE IMMEDIATE 'SELECT ' || r_sequence.sequence_owner||'.'||r_sequence.sequence_name || '.NEXTVAL FROM DUAL' into current_val;
EXECUTE IMMEDIATE 'ALTER SEQUENCE ' || r_sequence.sequence_owner||'.'||r_sequence.sequence_name || ' INCREMENT BY 2 CACHE 20';
DBMS_OUTPUT.PUT_LINE(r_sequence.sequence_name);
END;
END LOOP;
DBMS_OUTPUT.PUT_LINE('序列修改完成');
END;
/
2.3 比对序列
可通过用在源端和目标端创建一个dblink,通过dblink来比对源端和目标端的序列值。
脚本如下:
conn xxx/xxx
WITH source_seq AS (
SELECT sequence_name, last_number, increment_by
FROM all_sequences
WHERE sequence_owner in ('XXX','XXX')
),
target_seq AS (
SELECT sequence_name, last_number, increment_by
FROM all_sequences@XXX.XXX
WHERE sequence_owner in ('XXX','XXX')
)
SELECT 'SOURCE' AS seq_type, sequence_name, last_number, increment_by
FROM source_seq
UNION ALL
SELECT 'TARGET' AS seq_type, sequence_name, last_number, increment_by
FROM target_seq;