在两个RDS表(postgres)之间同步特定的模式



目前我们在AWS RDS (Postgres)中有两个dbs。一个用于演示(内部测试),另一个用于测试环境。两个数据库都有一个名为"strapi"的模式。其中包含要显示给用户的常用数据。我们的用例是,无论何时管理员更新demo db中的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是否适合我们的用例。

Thanks in Advance.

我仍然不确定AWS DMS对于我创建管道的用例是否正确,以便在demo db中特定模式的任何更改应该反映在beta db中。但是找到了解决上述DMS任务错误的方法。

"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)"

可以手动删除外键约束,一旦完成全负载迁移任务,我们可以重新创建外键约束

删除外键约束的简单脚本(取自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 $$; 

还要注意,对于全负载任务,只要在目标实例上完成迁移之后创建约束,就不会有任何迁移问题。但是,如果您期望在迁移运行时对目标实例中的表进行任何更新,那么您可能会看到由于删除外键约束而导致的一些不一致问题。

最新更新