Migrating existing databases into a data group
A database that you created outside of a data group is in the default data group. To migrate it into a data group, use a
data group migration. You start a migration with the aurora_start_data_group_migration SQL
function on the writer DB instance. There's no console or AWS CLI option for migrations at preview.
A migration copies the database into the data group inside the same DB cluster, while your application keeps reading and writing. When the copy is done, you switch over. Switchover makes the copy in the data group the only copy, and Aurora removes the original. The database keeps its name, so your application doesn't need a new connection string.
You can also migrate a database out of a data group and into the default data group, or migrate it from one data group to another.
Don't use ALTER DATABASE ... SET TABLESPACE to migrate a database into a data group or from
one data group to another. Use aurora_start_data_group_migration instead.
To migrate a database into a data group, complete the following steps on the writer DB instance:
-
Check the limitations. For more information, see Limitations for data group migrations.
-
Create the destination data group and map the roles that own objects in the database. For more information, see Creating a data group and Managing roles for data groups.
-
Start the migration. For more information, see Starting a data group migration.
-
Monitor the migration until it's ready to switch over. For more information, see Monitoring a data group migration.
-
Switch over. For more information, see Switching over a data group migration.
You can cancel a migration at any time before switchover starts. For more information, see Canceling a data group migration.
Topics
Limitations for data group migrations
The following limitations apply to data group migrations:
-
A migration moves a whole database. You can't migrate a single table or a system database.
-
A migration works inside one DB cluster. To take data to another DB cluster, migrate it into a data group and then move the data group.
-
Only one migration can run in a DB cluster at a time. A new request is refused while any migration is listed in the status view, including one that's
FAILED. Wait for it to finish, or cancel it. -
Schema changes (DDL) on the object are refused from the start of the migration until it ends. Reads and data changes (DML) are allowed.
-
Every table, index, and materialized view of the database must be in the database's default tablespace. At preview, a migration doesn't refuse a database that has objects in another tablespace, and it leaves those objects out of the data group. After the data group moves, they can't be read on the target DB cluster. Before you migrate, list such objects in the database and move them to its default tablespace. The following queries and statements apply only to the database that you're connected to, so run them in each database that you migrate into the data group.
SELECT c.relname, c.relkind, t.spcname FROM pg_class c JOIN pg_tablespace t ON t.oid = c.reltablespace WHERE c.relkind IN ('r', 'i', 'm') AND c.relnamespace <> 'pg_toast'::regnamespace AND t.spcname <> 'pg_global';If the query returns rows, move each object to the database's default tablespace. Use the statement that matches the object's
relkindvalue:ALTER TABLEforr,ALTER INDEXfori, orALTER MATERIALIZED VIEWform. A table's TOAST data moves with the table. The default tablespace isn't alwayspg_default. To find its name, run the following query.SELECT t.spcname FROM pg_database d JOIN pg_tablespace t ON t.oid = d.dattablespace WHERE d.datname = current_database();In the following example, replace
t2andt2iwith the names that the first query returned, andpg_defaultwith the name that the previous query returned.ALTER TABLEt2SET TABLESPACEpg_default; ALTER INDEXt2iSET TABLESPACEpg_default; -
At preview, switchover isn't automatic. You must start the switchover.
-
Switchover briefly interrupts reads and writes on the database. It can disconnect sessions and roll back their open transactions.
-
You can't move a data group while a migration into it is still running. You also can't start a migration while the source or destination data group is being moved.
The migrated object takes the destination data group's KMS key.
Starting a data group migration
To start a migration, call aurora_start_data_group_migration with the name of the database and the destination data group. To migrate to the default data group, pass an empty string ('') as the
data group.
Errors in the request, such as a database that doesn't exist, return right away and
create no migration. If a check that runs after the call returns fails, the migration status becomes FAILED and
the MESSAGE column shows the reason.
The call fails right away for errors such as the following:
-
The database doesn't exist.
-
The database is already in the destination data group.
-
The destination data group doesn't exist or isn't available.
-
The database is a system database. These are
template0,template1, andrdsadmin. -
Another migration already exists in the DB cluster.
-
The source or destination data group is being moved.
Monitoring a data group migration
To monitor a migration, query the information_schema.aurora_data_group_migration_status view. Any user can read
it.
The view has the following columns:
-
NAME– The object being migrated. -
TYPE– The object type,DATABASE. -
SRC_DATA_GROUP_NAMEandDEST_DATA_GROUP_NAME– The source and destination data groups. An empty string means the default data group.NULLmeans the data group can't be resolved. -
STATUS– The migration status. -
TOTAL_SIZE– The total size to copy, in bytes. It can grow while tables grow. -
SIZE_MIGRATED– The bytes copied so far. When it reachesTOTAL_SIZE, the copy is done, but switchover can still be pending. -
STARTED_AT– When the migration becameACTIVE. -
MESSAGE– A failure or retry message.
The migration status can be one of the following:
-
QUEUED– The request is recorded and the copy hasn't started yet. -
ACTIVE– Aurora is checking the tables and copying data. -
READY_TO_SWITCH– The copy is done. Writes go to both copies until you switch over. -
SWITCHING_OVER– Switchover is in progress. -
CANCELLING– Aurora is cleaning up after a cancel. -
FAILED– The migration stopped. The reason is inMESSAGE.
When a migration completes successfully, its row is removed from the view. A FAILED migration stays until you
cancel it.
The following query shows how much of each migration has been copied, as a percentage.
SELECT name, status, round(100.0 * size_migrated / NULLIF(total_size, 0), 1) AS percent_copied FROM information_schema.aurora_data_group_migration_status;
The output resembles the following example.
name | status | percent_copied ----------+--------+---------------- invoices | ACTIVE | 0.8 (1 row)
Switching over a data group migration
When the status is READY_TO_SWITCH, call aurora_switchover_data_group_migration. Until then, writes go
to both copies, so the copy in the data group stays current.
Switchover is atomic. Aurora closes the database to sessions, ends open sessions and rolls back their transactions, and then
makes the copy in the destination data group the database in one step. After that step, Aurora removes the original copy and
the database accepts connections again. If switchover stops before that step, the database stays in the source data group, and
the status returns to READY_TO_SWITCH. Plan switchover for a quiet time, and have your application retry
connections that fail during it.
If the status isn't READY_TO_SWITCH, the call fails and names the current status.
The call returns before switchover finishes. Keep checking the view. If the status returns to READY_TO_SWITCH, read
MESSAGE, and then call the function again. If the writer DB instance restarts after the switchover step, Aurora
finishes the switchover when the instance is back.
After switchover, the object is in the destination data group and moves with it. The migration entry disappears from the aurora_data_group_migration_status view, and you can start a new migration.
Canceling a data group migration
You can cancel a migration until switchover starts. After that, the cancel is refused.
The call returns right away and the status becomes CANCELLING. When cleanup is done, the row is removed. The object
stays where it was, and the partial copy is removed.
A FAILED migration stays in the view until you cancel it. Read MESSAGE, fix the cause, cancel the
migration, and then start it again.
Permissions for data group migrations
To start, switch over, or cancel a migration, you must be a member of the rds_superuser role.
Any user can read the status view.