[英]PostgreSQL Primary Key sequence lost after migration using AWS DMS
I recently migrated a self hosted Postgres database to AWS RDS using the AWS Migration Service.我最近使用 AWS 迁移服务将一个自托管的 Postgres 数据库迁移到 AWS RDS。
Postgres Version: 10.6 Postgres 版本:10.6
I have noticed that all of my primary keys are no longer set to "sequence", and when I attempt to manually add a sequence it starts at 1 instead of continuing the count that is already set.我注意到我所有的主键不再设置为“序列”,当我尝试手动添加一个序列时,它从 1 开始,而不是继续已经设置的计数。
I use Rails with the database, so my sql skills are pretty low.我在数据库中使用 Rails,所以我的 sql 技能很低。 I can generally find my way around inserts and updates, but this is not a realm I have much experience in.
我通常可以找到插入和更新的方法,但这不是我有很多经验的 realm。
My question has 2 parts:我的问题有两部分:
After @a_horse_with_no_name pointed me in the right direction and chatting with AWS I am able to answer my own question, at least if you are using AWS Database Migration Service (DMS). 在@a_horse_with_no_name指向正确的方向并与AWS聊天之后,我能够回答我自己的问题,至少在您使用AWS数据库迁移服务(DMS)时。
The problem is, DMS only focuses on the data itself and not really the schema (which to me seems like a major oversight, especially if your using the same database technology but that is another issue). 问题是,DMS只关注数据本身,而不是模式(这对我来说似乎是一个主要的疏忽,特别是如果你使用相同的数据库技术但这是另一个问题)。 So the schema itself is not migrated.
因此,架构本身不会迁移。 The documentation does not really make this clear.
文档并没有真正说明这一点。
To fix this issue: 要解决此问题:
What I have not yet tested is how to handle the fact that I am migrating a live database. 我尚未测试的是如何处理我正在迁移实时数据库的事实。 So the sequences may be out of date on the target database when the migration is done.
因此,迁移完成后,目标数据库上的序列可能已过时。 I believe I can just later go into SCT and only migrate the sequences but I have not tested this yet.
我相信我可以稍后进入SCT并只迁移序列,但我还没有测试过。
Unless you missed something and the migration could be processed once more (with different parameters? I have never used AWS Migration Service but I presume it should preserve the serial
-iness of your columns...) you'll need to re-create the sequences too. 除非你错过了什么和迁移可以处理一次(使用不同的参数?我从来没有使用AWS迁移服务,但我相信它应该保持
serial
的列-iness ......),你就需要重新创建序列也是。
I encountered a similar situation a year or so ago, and wrote this answer on SO . 我大约一年前遇到过类似的情况,并在SO上写下了这个答案 。
Here's the gist of it: 这是它的要点:
CREATE SEQUENCE foo_a_seq OWNED BY foo.a;
SELECT setval('foo_a_seq', coalesce(max(a), 0)) FROM foo;
ALTER TABLE foo ALTER COLUMN a SET DEFAULT nextval('foo_a_seq');
This would create a sequence foo_a_seq
, with its nextval
being one higher than the max
value in foo.a
(or 1
if there is no foo
record). 这将创建一个序列
foo_a_seq
,其nextval
比foo.a
的max
高1
如果没有foo
记录, foo.a
1
)。
I also went ahead and set up a Function
to quickly apply to all the tables/columns in need: 我还继续设置一个
Function
来快速应用于所有需要的表/列:
CREATE OR REPLACE FUNCTION make_into_serial(table_name TEXT, column_name TEXT) RETURNS INTEGER AS $$
DECLARE
start_with INTEGER;
sequence_name TEXT;
BEGIN
sequence_name := table_name || '_' || column_name || '_seq';
EXECUTE 'SELECT coalesce(max(' || column_name || '), 0) + 1 FROM ' || table_name
INTO start_with;
EXECUTE 'CREATE SEQUENCE ' || sequence_name ||
' START WITH ' || start_with ||
' OWNED BY ' || table_name || '.' || column_name;
EXECUTE 'ALTER TABLE ' || table_name || ' ALTER COLUMN ' || column_name ||
' SET DEFAULT nextVal(''' || sequence_name || ''')';
RETURN start_with;
END;
$$ LANGUAGE plpgsql VOLATILE;
Use it like so: 像这样使用它:
SELECT make_into_serial('foo', 'a');
Sequence, Index, and Constraint are not migrated and it is mentioned in the official docs on AWS. Sequence、Index 和 Constraint 没有迁移,AWS 官方文档中提到了。
You can use this source .你可以使用这个来源。 This will help you to migrate Sequence, Index, and Constraint at once.
这将帮助您一次迁移序列、索引和约束。
For any folks using macOS and aren't able to use the SchemaConversionTool suggested in one of the answers above, follow the steps below:对于使用 macOS 且无法使用上述答案之一中建议的 SchemaConversionTool 的任何人,请按照以下步骤操作:
Empty the schema in the target database in order to start fresh清空目标数据库中的模式以重新开始
DROP SCHEMA public;删除架构公开;
Generate a SQL script which contains all the DDL statements from the source database's schema.生成一个 SQL 脚本,其中包含来自源数据库模式的所有 DDL 语句。 If you use dbeaver then right click on schema name and click on Generate SQL
如果您使用 dbeaver 然后右键单击模式名称并单击生成 SQL
Run the commands in the generated SQL script from step 2 in the target database so that the schemas are now in sync with each other在目标数据库中运行步骤 2 生成的 SQL 脚本中的命令,以便模式现在彼此同步
Go to AWS DMS and start the task with target table preparation mode set to truncate Go 到 AWS DMS 并启动目标表准备模式设置为截断的任务
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.