This topic describes how to migrate incremental data from a self-managed PostgreSQL database to an ApsaraDB RDS for PostgreSQL instance by using Data Transmission Service (DTS). DTS supports schema migration, full data migration, and incremental data migration. When you migrate data from a self-managed PostgreSQL database to Alibaba Cloud, you can select all of the supported migration types to ensure service continuity.

This topic uses User-Created Database with Public IP Address as an example to describe how to configure an incremental data migration task. For information about how to perform only full data migration, see Migrate full data from a self-managed PostgreSQL database to an ApsaraDB RDS for PostgreSQL instance.

Prerequisites

  • The version of the self-managed PostgreSQL database is 9.4.8 or later, 9.5, 9.6, or 10.0.
  • The available storage space of the ApsaraDB RDS for PostgreSQL instance is larger than the total size of the data in the self-managed PostgreSQL database.
  • The service port of the self-managed PostgreSQL database is accessible over the Internet.

Precautions

  • DTS uses read and write resources of the source and destination databases during full data migration. This may increase the loads of the database servers. If the database performance is unfavorable, the specification is low, or the data volume is large, database services may become unavailable. For example, DTS occupies a large amount of read and write resources in the following cases: a large number of slow SQL queries are performed on the source database, the tables have no primary keys, or a deadlock occurs in the destination database. Before you migrate data, evaluate the impact of data migration on the performance of the source and destination databases. We recommend that you migrate data during off-peak hours. For example, you can migrate data when the CPU utilization of the source and destination databases is less than 30%.
  • If you select a schema as the object to be migrated and create a table in the schema or run the RENAME command to rename the table, you must run the ALTER TABLE schema.table REPLICA IDENTITY FULL; command before you write data to the table.
    Note Replace the schema and table in the preceding sample command with the actual schema name and table name.
  • The tables to be migrated in the source database must have PRIMARY KEY or UNIQUE constraints and all fields must be unique. Otherwise, the destination database may contain duplicate data records.
  • If a data migration task fails, DTS automatically resumes the task. Before you switch your workloads to the destination instance, stop or release the data migration task. Otherwise, the data in the source database will overwrite the data in the destination instance after the task is resumed.

Limits

  • A single data migration task can migrate data from only one database. To migrate data from multiple databases, you must create a data migration task for each database.
  • The name of the source database cannot contain hyphens (-), for example, dts-testdata.
  • If a primary/secondary switchover is performed on the source database during incremental data migration, DTS does not support resumable transmission.
  • Data may be inconsistent between the primary and secondary nodes of the source database due to synchronization delay. Therefore, you must use the primary node as the data source when you migrate data.
    Note We recommend that you migrate data during off-peak hours. You can modify the transfer rate of full data migration based on the read/write performance of the source database. For more information, see Modify the transfer rate of full data migration.
  • DTS does not check the validity of metadata such as sequences. You must manually check the validity of metadata.
  • After your workloads are switched to the destination database, newly written sequences do not increment from the maximum value of the sequences in the source database. Therefore, you must query the maximum value of the sequences in the source database before you switch your workloads to the destination database. Then, you must specify the queried maximum value as the starting value of the sequences in the destination database. You can run the following statements to query the maximum value of the sequences in the source database:
    do language plpgsql $$
    declare
      nsp name;
      rel name;
      val int8;
    begin
      for nsp,rel in select nspname,relname from pg_class t2 , pg_namespace t3 where t2.relnamespace=t3.oid and t2.relkind='S'
      loop
        execute format($_$select last_value from %I.%I$_$, nsp, rel) into val;
        raise notice '%',
        format($_$select setval('%I.%I'::regclass, %s);$_$, nsp, rel, val+1);
      end loop;
    end;
    $$;
  • To ensure that the data migration task runs as expected, you can perform a primary/secondary switchover only on an ApsaraDB RDS for PostgreSQL instance V11. In this case, you must set the rds_failover_slot_mode parameter to sync. For more information, see Use the failover slot feature for logical subscriptions.
    Warning If you perform a primary/secondary switchover on a self-managed PostgreSQL database or an ApsaraDB RDS for PostgreSQL instance of other versions, the data migration task stops.
  • During incremental data migration, DTS migrates only DML operations. DML operations include INSERT, DELETE, and UPDATE.
    Note Only data migration tasks that are created after October 1, 2020 can migrate data definition language (DDL) operations. To do this, you must create a trigger and function in the source database to obtain the DDL information before you configure the task. For more information, see Use triggers and functions to implement incremental DDL migration for PostgreSQL databases.

Billing

Migration type Task configuration fee Internet traffic fee
Schema migration and full data migration Free of charge. Charged only when data is migrated from Alibaba Cloud over the Internet. For more information, see Pricing.
Incremental data migration Charged. For more information, see Pricing.

Permissions required for database accounts

Database Schema migration Full data migration Incremental data migration
Self-managed PostgreSQL database The USAGE permission on pg_catalog The SELECT permission on the objects to be migrated The permissions of the superuser role
ApsaraDB RDS for PostgreSQL instance The CREATE and USAGE permissions on the objects to be migrated The permissions of the schema owner The permissions of the schema owner

For information about how to create and authorize a database account, see the following topics:

Data migration process

The following table describes how DTS migrates the schemas and data of the source PostgreSQL database. The process prevents data migration failures that are caused by dependencies between objects.

Note For more information about schema migration, full data migration, and incremental data migration, see Terms.
Data migration process Description
1.Schema migration DTS migrates the schemas of tables, views, sequences, functions, user-defined types, rules, domains, operations, and aggregates to the destination database.
Note DTS does not migrate plug-ins. In addition, DTS does not migrate functions that are written in the C programming language.
2.Full data migration DTS migrates historical data of the required objects to the destination database.
3.Schema migration DTS migrates the schemas of triggers and foreign keys to the destination database.
4.Incremental data migration DTS migrates incremental data of the required objects to the destination database. Incremental data migration allows you to ensure service continuity when you migrate data from a self-managed PostgreSQL database.
Note
  • During incremental data migration, DTS migrates only DML operations. DML operations include INSERT, DELETE, and UPDATE.
  • Incremental data migration does not support the BIT data type.

Preparation 1: Install the logical flow replication plug-in and modify the system settings

Before you configure an incremental data migration task, install the logical flow replication plug-in that is provided by DTS in the self-managed PostgreSQL database.
Note PostgreSQL version 9.6 is used in this example.
  1. On the server of the self-managed PostgreSQL database, run the wget command to download the logical flow replication plug-in based on the database version. Download the plug-in from one of the following links:
  2. Extract the plug-in from the compressed file.
    tar xvf ali_decoding_9.6.tar
  3. Copy the ali_decoding.so file to the lib directory where PostgreSQL is installed.
    cp ali_decoding.so /usr/lib/postgresql/9.6/lib/
  4. Copy the ali_decoding.control file to the extension directory where PostgreSQL is installed.
    cp ali_decoding.control /usr/share/postgresql/9.6/extension/
  5. Log on to the self-managed PostgreSQL database as a superuser.
  6. Set the value of the max_replication_slots parameter to an integer greater than 1. In this example, set the value to 5.
    Note For more information, see PostgreSQL official documentation.
    ALTER SYSTEM set max_replication_slots = '5';
  7. Set the value of the wal_level parameter to logical.
    ALTER SYSTEM SET wal_level = logical;
  8. Set the value of the max_wal_senders parameter to an integer greater than 1. In this example, set the value to 5.
    Note The max_wal_senders parameter specifies the maximum number of concurrent tasks. We recommend that you set this parameter to the same value as the max_replication_slots parameter.
    ALTER SYSTEM SET max_wal_senders = '5';
  9. Return to the shell of the server for the self-managed PostgreSQL database and run the following command to restart the PostgreSQL service:
    service postgresql restart
  10. Log on to the self-managed PostgreSQL database again and run the following command to check whether the replication slot can be created:
    SELECT * FROM pg_create_logical_replication_slot('replication_slot_test', 'ali_decoding');
    The following message indicates that the logical flow replication plug-in is installed. Check whether the plug-in is installed
  11. Add the CIDR blocks of DTS servers to the PostgreSQL configuration file (pg_hba.conf). For more information about how to edit this configuration file, see The pg_hba.conf File. Skip this step if you have set the IP address in the pg_hba.conf file to 0.0.0.0/0.
    Note You must add the CIDR blocks of DTS servers that reside in the same region as the destination database. For more information, see Add the CIDR blocks of DTS servers to the security settings of on-premises databases.

Preparation 2: Create a database and schema in the destination RDS instance

Create a database and schema in the destination RDS instance based on the database and schema information of the objects to be migrated. The schema name of the source and destination databases must be the same. For more information, see Create a database on an ApsaraDB RDS for PostgreSQL instance and Appendix: User and schema management.

Procedure

  1. Log on to the DTS console.
  2. In the left-side navigation pane, click Data Migration.
  3. At the top of the Migration Tasks page, select the region where the destination RDS instance resides. Select a region
  4. In the upper-right corner of the page, click Create Migration Task.
  5. Configure the source and destination databases. Configure the source and destination databases
    Section Parameter Description
    N/A Task Name DTS automatically generates a task name. We recommend that you specify an informative name for easy identification. You do not need to use a unique task name.
    Source Database Instance Type Select an instance type based on the deployment of the source database. In this example, select User-Created Database with Public IP Address.
    Note If you select other instance types, you must deploy the network environment for the source database. For more information, see Preparation overview.
    Instance Region If the instance type is set to User-Created Database with Public IP Address, you do not need to specify the instance region.
    Note If a whitelist is configured for the self-managed PostgreSQL database, you must add the CIDR blocks of DTS servers to the whitelist of the database. You can click Get IP Address Segment of DTS next to Instance Region to obtain the CIDR blocks of DTS servers.
    Database Type Select PostgreSQL.
    Hostname or IP Address Enter the endpoint that is used to connect to the self-managed PostgreSQL database. In this example, enter the public IP address.
    Port Number Enter the service port number of the self-managed PostgreSQL database. The default port number is 5432.
    Database Name Enter the name of the self-managed PostgreSQL database.
    Database Account Enter the account that is used to log on to the self-managed PostgreSQL database. For more information about the permissions that are required for the account, see Permissions required for database accounts.
    Database Password Enter the password of the database account.
    Note After you specify the source database parameters, click Test Connectivity next to Database Password to verify whether the specified parameters are valid. If the specified parameters are valid, the Passed message appears. If the Failed message appears, click Check next to Failed. Modify the source database parameters based on the check results.
    Destination Database Instance Type Select RDS Instance.
    Instance Region Select the region where the destination RDS instance resides.
    RDS Instance ID Select the ID of the destination RDS instance.
    Database Name Enter the name of the destination database in the RDS instance. The name can be different from the name of the source PostgreSQL database.
    Note Before you configure the data migration task, create a database and schema in the destination RDS instance. For more information, see Preparation 2: Create a database and schema in the destination RDS instance.
    Database Account Enter the database account of the destination RDS instance. For more information about the permissions that are required for the account, see Permissions required for database accounts.
    Database Password Enter the password of the database account.
    Note After you specify the destination database parameters, click Test Connectivity next to Database Password to verify whether the parameters are valid. If the specified parameters are valid, the Passed message appears. If the Failed message appears, click Check next to Failed. Modify the destination database parameters based on the check results.
  6. In the lower-right corner of the page, click Set Whitelist and Next.
    Note DTS adds the CIDR blocks of DTS servers to the whitelist of the destination RDS instance. This ensures that DTS servers can connect to the destination RDS instance.
  7. Select the migration types and the objects to be migrated. Select the migration types and the objects to be migrated
    Setting Description
    Select the migration types
    • To perform only full data migration, select Schema Migration and Full Data Migration.
    • To ensure service continuity during data migration, select Schema Migration, Full Data Migration, and Incremental Data Migration. In this example, select all of the three migration types.
    Note If Incremental Data Migration is not selected, do not write data to the self-managed PostgreSQL database during full data migration. This ensures data consistency between the source and destination databases.
    Select the objects to be migrated

    Select one or more objects from the Available section and click the Right arrow icon to move the objects to the Selected section.

    Note
    • You can select columns, tables, or schemas as the objects to be migrated.
    • By default, after an object is migrated to the destination RDS instance, the name of the object remains the same as that in the self-managed PostgreSQL database. You can use the object name mapping feature to change the names of the objects that are migrated to the destination RDS instance. For more information, see Object name mapping.
    • If you use the object name mapping feature on an object, other objects that are dependent on the object may fail to be migrated.
    Specify whether to rename object names You can use the object name mapping feature to change the names of the objects that are migrated to the destination instance. For more information, see Object name mapping.
    Specify the retry time for failed connections to the source or destination database By default, if DTS fails to connect to the source or destination database, DTS retries within the next 12 hours. You can specify the retry time based on your needs. If DTS reconnects to the source and destination databases within the specified time, DTS resumes the data migration task. Otherwise, the data migration task fails.
    Note When DTS retries a connection, you are charged for the DTS instance. We recommend that you specify the retry time based on your business needs. You can also release the DTS instance at your earliest opportunity after the source and destination instances are released.
  8. Click Precheck.
    Note
    • A precheck is performed before the migration task starts. The migration task only starts after the precheck succeeds.
    • If the precheck fails, click the Note icon next to each failed check item to view the related details. Fix the issues as instructed and run the precheck again.
  9. After the task passes the precheck, click Next.
  10. In the Confirm Settings dialog box, specify the Channel Specification and select Data Transmission Service (Pay-As-You-Go) Service Terms.
  11. Click Buy and Start to start the data migration task.
    • Full data migration

      We recommend that you do not manually stop the task during full data migration. Otherwise, the data migrated to the destination instance will be incomplete. You can wait until the data migration task automatically stops.

    • Incremental data migration

      The task does not automatically stop during incremental data migration. You must manually stop the task.

      Note We recommend that you select an appropriate time to manually stop the data migration task. For example, you can stop the task during off-peak hours or before you switch your workloads to the destination instance.
      1. Wait until Incremental Data Migration and The migration task is not delayed appear in the progress bar of the migration task. Then, stop writing data to the source database for a few minutes. The delay time of incremental data migration may be displayed in the progress bar.
      2. After the status of incremental data migration changes to The migration task is not delayed again, manually stop the migration task. The migration task is not delayed
  12. Switch your workloads to the RDS instance.

What to do next

The database accounts that are used for data migration have the read and write permissions. After data migration is complete, you must delete the accounts of both the self-managed PostgreSQL database and the ApsaraDB RDS for PostgreSQL instance to ensure security.