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 标志指定要组合在一起的表。
更多详情参考链接