2

我正在尝试将9.6.15我们在 heroku 公共空间中的 postgres db (version ) 移动到 AWS RDS。db 约为 2.2tb,位于 heroku 高级 7 层。

由于我发现从 heroku 进行实时复制几乎是不可能的,因为它们限制了设置复制的功能(根据我的理解),我希望看看我能以多快的速度转储数据并将其加载到 RDS。

我正在寻找优化的方法,pg_dumps到目前为止我所做的是从 AWS 中的 ec2 实例 (m5.2xlarge) 运行它,以查看pg_dumps运行速度。

在 ec2 上运行它时,我只能达到 10mb/秒,这非常慢,因为从这个意义上说,它需要大约 84 小时才能运行。

我是这样跑pg_dumps的:

sudo pg_dump postgres://user:pass@url:5432/db \
--jobs=24 \
--format=directory \
--file=/monodb/pgdump2 \
--verbose

我还能做些什么来加快导出速度,或者这只是对heroku出口带宽的限制?

4

1 回答 1

0

1. 将架构与数据分开转储

当您继续阅读时,这样做的原因会更加清楚,但简单地说,单独转储模式允许我们同时进行多个并发转储和恢复操作。

2.在开始还原之前禁用外键

Postgres 转储通常是一系列插入语句。如果您要插入的表具有外键,则必须为每次插入评估键的规则。


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’

           — 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’,

               quote_ident(t.table_name), quote_ident(t.constraint_name), t.constraint_definition));

       execute format(‘alter table %s drop constraint %s’, quote_ident(t.table_name), quote_ident(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 $$;

3. 您可以使用当前标志为 pg_dump 设置不同的标志,例如:

–data-only – This is the flag to dump only the data from the tables and not the schema information.

–no-synchronized-snapshots – Prior to Postgres 9.2 this a requirement for running jobs in parallel

–schema=public – Instructs pg_dump to only dump the public schemas, for most cases this is all you need.

4. 大表自己dump,小表分组

这样做可以让您同时运行多个转储和恢复。您可以使用 pg_dump 中的 –table 标志指定要组合在一起的表。

更多详情参考链接

于 2019-11-08T15:15:02.197 回答