简体   繁体   English

PostgreSQL 使用 AWS DMS 迁移后主键序列丢失

[英]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:我的问题有两部分:

  1. How do I fix a single table?如何修复单个表? I want to understand and test what I am doing before moving on.在继续之前,我想了解并测试我在做什么。
  2. Is there a way I can apply this fix to every table I have without needing to manually modify each table?有没有一种方法可以将此修复程序应用到我拥有的每个表而无需手动修改每个表?

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: 要解决此问题:

  1. Stop (if it still exists) the existing AWS DMS migration 停止(如果它仍然存在)现有的AWS DMS迁移
  2. Drop the existing migrated database, and create a new empty schema to use 删除现有的迁移数据库,并创建要使用的新空模式
  3. Follow the steps here https://docs.aws.amazon.com/SchemaConversionTool/latest/userguide/CHAP_Installing.html to install and setup the Amazon Schema Conversation Tool (SCT) 按照https://docs.aws.amazon.com/SchemaConversionTool/latest/userguide/CHAP_Installing.html中的步骤安装和设置Amazon Schema Conversation Tool(SCT)
  4. Once you are connected to both databases, follow the steps here https://docs.aws.amazon.com/SchemaConversionTool/latest/userguide/CHAP_Converting.html to "convert" your schema (I did the entire "public" schema for this database to ensure everything is covered 一旦连接到两个数据库,请按照此处的步骤https://docs.aws.amazon.com/SchemaConversionTool/latest/userguide/CHAP_Converting.html “转换”您的架构(我为此完成了整个“公共”架构数据库,以确保涵盖所有内容
  5. Create or modify your AWS DMS Migration, ensuring Target Table Preparation Mode = "TRUNCATE" and disable foreign keys on the target database. 创建或修改AWS DMS迁移,确保目标表准备模式=“TRUNCATE”并禁用目标数据库上的外键。 If modifying, make sure when asked you "RESTART" not resume 如果修改,请确保在被要求“恢复”时不恢复

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 ,其nextvalfoo.amax1如果没有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 的任何人,请按照以下步骤操作:

  1. Empty the schema in the target database in order to start fresh清空目标数据库中的模式以重新开始

    DROP SCHEMA public;删除架构公开;

  2. 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 生成sql

  3. 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 脚本中的命令,以便模式现在彼此同步

  4. 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.

相关问题 使用AWS Data Migration Service(DMS)从Heroku PostgreSQL迁移到AWS RDS不起作用 - Migration from Heroku PostgreSQL to AWS RDS using AWS Data Migration Service (DMS) not working 使用 AWS DMS 从 DB2 到 PostgreSQL 的数据迁移 - Varchar 字段显示尾随空格 - Data Migration from DB2 to PostgreSQL using AWS DMS - Varchar fields are showing trailing spaces AWS - DMS 迁移缺少序列、视图、例程...等 - AWS - DMS migration missing sequence , views , routines ... etc 在 AWS DMS 迁移中禁用外键约束不适用于 Postgres - Disable foreign key constrains in AWS DMS Migration not working for Postgres 在主键上具有PostgreSQL序列的学说更新架构 - Doctrine update schema with PostgreSQL sequence on primary key AWS DMS-计划的数据库迁移 - AWS DMS - Scheduled DB Migration Django 和 PostgreSQL 主键自增序列 - Django and PostgreSQL sequence for primary key autoincrement CDC 和副本使用 AWS DMS 从 oracle PeopleSoft 到 RDS postgreSQL - CDC and replica using AWS DMS from oracle PeopleSoft to RDS postgreSQL Hibernate不使用PostgreSQL序列来生成主键 - Hibernate doesn't use PostgreSQL sequence to generate primary key 如何安全地在PostgreSQL中转发主键序列? - How can I forward a primary key sequence in PostgreSQL safely?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM