在两个RDS(postgres)特定模式中表同步

0

【以下的问题经过翻译处理】 你好

目前我们在AWS RDS(Postgres)中有两个数据库。一个用于演示(内部测试),另一个用于beta环境。两个数据库都有一个名为“strapi”的模式,其中包含要显示给用户的通用数据。我们的用例是每当管理员更新演示数据库中的strapi数据时,它也应该在beta环境中反映出来。因此,我正在寻找支持我们用例的AWS服务。

我搜索了类似的问题,并发现有人推荐AWS DMS。因此,我继续创建了端点,并最初以“不执行任何操作”选项运行了任务以进行目标表准备模式。

稍后使用“Truncate”选项为目标表准备模式运行了完整的加载任务。

任务失败并显示以下错误:

“RetCode: SQL_ERROR SqlState: 0A000 NativeError: 1 Message: ERROR: cannot truncate a table referenced in a foreign key constraint; Error while executing the query [1022502] (ar_odbc_stmt.c:4828). Failed to truncate table strapi.programs [1022502] (odbc_endpoint_imp.c:4372)”

尝试在RDS目标数据库中将session_replication_role参数设置为replica,仍然出现相同的错误。任何帮助都不胜感激。

注:我仍然不确定 AWS DMS 是否适合我们的用例。

提前致谢。

1 Antwort
0

【以下的回答经过翻译处理】 找到了解决方案。解决方案是在完整加载前手动删除外键约束,并在迁移任务结束后重新创建它们。

一个简单的脚本用于删除外键约束(摘自 https://dba.stackexchange.com/a/97047

create table if not exists dropped_foreign_keys (
        seq bigserial primary key,
        sql text
);

do $$ declare t record;
begin
    for t in select conrelid::regclass::varchar table_name, conname constraint_name,
            pg_catalog.pg_get_constraintdef(r.oid, true) constraint_definition
            from pg_catalog.pg_constraint r
            where r.contype = 'f'
            -- uncomment the below line for current schema only:
            -- and r.connamespace = (select n.oid from pg_namespace n where n.nspname = current_schema())
        loop

        insert into dropped_foreign_keys (sql) values (
            format('alter table %s add constraint %s %s',t.table_name, t.constraint_name, t.constraint_definition));

        execute format('alter table %s drop constraint %s', t.table_name, t.constraint_name);

    end loop;

    
end $$;

重新创建删除的外键约束

do $$ declare t record;
begin
    -- order by seq for easier troubleshooting when data does not satisfy FKs
    for t in select * from dropped_foreign_keys order by seq loop
        execute t.sql;
        delete from dropped_foreign_keys where seq = t.seq;
    end loop;
end $$;

profile picture
EXPERTE
beantwortet vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen