Rolling out schema changes
In a database cluster group for Aurora, each member cluster holds its own table definitions. The group holds the data mappings, routing functions, and scaleout table definitions. A schema change can touch both. The routing layer doesn't run DDL on your tables, and the group doesn't manage a schema change across its member clusters. You choose the order, and no transaction spans the two.
The usual way to reach each member cluster is a session pin. A pinned session passes statements through to its member cluster, so anything the member cluster accepts runs there, including DDL, administrative statements, and catalog reads. You can also use a direct connection.
In query routing mode, the routing layer rejects DDL with one error: Aurora MySQL error 1235 (SQLSTATE 42000). A statement that the routing layer doesn't recognize and one that it recognizes but doesn't route return the same error, so match on the outcome, not on the text. For the error text, see Error codes for database cluster groups for Aurora.
The following table shows where to run each kind of statement.
| Statement | Where it runs |
|---|---|
CREATE, ALTER, DROP, RENAME, and TRUNCATE on a table, and CREATE INDEX |
A session pin or a direct connection, with cluster names |
| Stored procedures, functions, triggers, and events | A session pin or a direct connection, on each member cluster that needs them |
| Scaleout table and routing function statements | The routing endpoint, in a session that isn't pinned. A routing function has no ALTER statement. See Changing a routing function. |
GRANT and REVOKE for a scaleout table |
On each member table, on each member cluster, with cluster names |
LOCK TABLES on a scaleout table |
On its member tables, through a session pin or a direct connection |
Account statements for router users, such as CREATE USER |
The routing endpoint. Create the same-named user on each member cluster too. See Router users and routing layer privileges. |
Through a pin or a direct connection, use the cluster names from the cluster_*_name columns of dbclustergroup_sys.data_mappings. To change mappings and routing functions, see Changing data mappings and Changing a routing function.
Running DDL through a session pin
A session pin binds your routing endpoint session to one member cluster and passes every statement through, so any DDL that the member cluster accepts runs there. You can roll out DDL to each member cluster in turn from one connection. For more information, see Session pinning. Keep the following in mind:
-
A pin applies to your session only, so use a dedicated session, not a pooled connection. You can't set or clear a pin inside a transaction.
-
A pinned session uses cluster names and passes
dbclustergroup_sysreads andSCALEOUTstatements to the member cluster, where they fail. Change these definitions in an unpinned session. -
A write to a member table through a pin bypasses routing. Keep each row on the member cluster that its key maps to.
-
A pin keeps the session's current database and doesn't convert it, so a router database name doesn't resolve on the member cluster. Name the cluster database name in the pin.
To roll out DDL through a session pin
-
Connect to the routing endpoint as a router user whose same-named member cluster user has DDL privileges on each member cluster that holds the object. Read the aliases from
dbclustergroup_sys.member_cluster_mappings. -
At the routing endpoint, outside a transaction, pin the session to the first member cluster, and confirm which one you reached.
-
In the pinned session, run the DDL with cluster names.
-
In the same session, clear the pin.
-
Repeat steps 2 through 4 for each member cluster that holds the object.
If the routing layer can't reach a member cluster, use a direct connection. The statements are as follows.
To pin with a database, use '. For the variable, see router_cluster_pin.MEMBER_CLUSTER_ALIAS@CLUSTER_DB'
Step 2, pin and confirm:
SET SESSION router_cluster_pin = 'MEMBER_CLUSTER_ALIAS'; SELECT @@hostname, @@aurora_server_id;
If the routing layer refuses the pin with error 1231, use a quoted alias from member_cluster_mappings. For the error text, see Error codes for database cluster groups for Aurora.
Step 3, run the DDL:
ALTER TABLECLUSTER_DB.TABLEADD COLUMNCOLUMNCOLUMN_TYPENULL;
Step 4, clear the pin. Temporary objects on the member cluster don't block the return to routing.
SET SESSION router_cluster_pin = DEFAULT;
Running DDL over a direct connection
Use a direct connection, from a host in the group's VPC, for schema tools and for long or large-table changes.
To run DDL over a direct connection
-
At the routing endpoint, read the member cluster's DB cluster identifier from
dbclustergroup_sys.member_cluster_mappings. Then, in the AWS CLI or the console, read its cluster endpoint, as described in Connecting to an Amazon Aurora DB cluster. -
On the member cluster (direct), run the DDL with cluster names. The connection follows the member cluster's own TLS settings. For TLS on the routing endpoint, see Encryption in transit (TLS) and at rest.
-
Repeat for each member cluster that holds the object.
Rolling out a schema change
Apply the change to every member cluster that holds the object, and then confirm that the member clusters and the routing layer agree.
Before you begin, do the following:
-
From the mapping rows, find each member cluster that holds the object and, for a scaleout table, each member table's cluster name.
-
Classify the change. An expand (add an object or column) runs member DDL first and the definitions last. A contract (remove one) does the reverse. A rename is both. A change to a routing key column is a routing change. See Changing a routing function.
-
Plan for the period when only some member clusters have the new schema. See Keeping member clusters consistent.
-
Check that no move of the object's collocation is running, because a move blocks DDL on the objects that it carries until the job ends. Check that no long transaction holds metadata locks. Before you add a unique key, check each member cluster for duplicates. Otherwise, the DDL fails on some member clusters and succeeds on others.
To roll out a schema change
-
Take a drift baseline: table signatures on each member cluster (direct), and the mapping export at the routing endpoint. If they already differ, stop. See Auditing access and member cluster configuration.
-
For a contract, wait until the application stops using the object. Then remove its definitions at the routing endpoint, one statement per object: inheriting scaleout tables, parent scaleout tables, routing functions, then mapping rows.
-
Through a session pin or a direct connection, apply the DDL to one member cluster first, such as the least busy. Read its signatures again, and confirm that only the affected table changed.
-
Apply the DDL to the other member clusters, one at a time for a group in use. Meanwhile, at the routing endpoint, run a one-row routed read of the changed table on each. If one slows down or fails, stop.
-
For an expand, when every member cluster matches, apply the definitions at the routing endpoint: mappings, routing functions, then scaleout tables (parents, then children that use
USING ROUTING FUNCTION OF). The client stops at the first error. Before a rerun, read the current rows and run only what's missing. -
In your pipeline, deploy the application code that uses the change, and refresh schema caches.
The statements for steps 2 and 5 are as follows.
Step 2, remove the definitions of a contract:
DROP SCALEOUT TABLEROUTER_DB.TABLE; DELETE FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('ROUTER_DB', 'ROUTER_TABLE');
Step 5, apply the definitions of an expand from a script:
mysql --defaults-extra-file=OPTION_FILE-hROUTING_ENDPOINT-P 3306 --ssl-mode=REQUIRED \ <MAPPING_SCRIPT
The rollout is complete when the signatures match everywhere and a routed statement that uses the new schema succeeds. If the DDL fails on one member cluster, make them match before you continue. See Troubleshooting data moves and schema changes. To rename a mapped table, see Renaming an object and keeping its mappings.
Changing scaleout tables
At the routing endpoint, CREATE, ALTER, and DROP SCALEOUT TABLE and the routing function statements change only these definitions. You change member tables with standard DDL on each member cluster. Because the routing layer doesn't check that member tables match, apply identical DDL to each, check for drift after each change, and do the following:
-
Keep a copy of each scaleout table and routing function definition, or read it back with
SHOW CREATE SCALEOUT TABLEandSHOW CREATE SCALEOUT ROUTING FUNCTION. -
Before any member table change, read the key column from
SHOW CREATE SCALEOUT TABLE. The routing layer doesn't detect a member cluster change that alters, renames, or drops the key column, and keyed reads on that member cluster then fail. To change the key column, see Changing a routing function. -
To rename a scaleout table, run
ALTER SCALEOUT TABLEat the routing endpoint. The new name must not be in use in the group.old_nameRENAME TOnew_name
Keeping member clusters consistent
Member tables of one scaleout table can have different schemas until you finish the last member cluster. Each member cluster applies its change on its own, and the routing layer doesn't compare them. Make every intermediate state valid for both old and new application code. The following table shows what a routed statement returns in common mixed states.
| During the rollout | A routed statement returns |
|---|---|
| A table created on a member cluster, before its mapping exists | An unknown-table error |
| A column added on some member clusters only | An unknown-column error for statements that name the column, on the member clusters without it |
| A column dropped from one member table of a scaleout table | An unknown-column error for that member cluster's keys only |
| A column's collation changed on one member cluster | Results that differ by member cluster, with no error |
Add a nullable column first, and drop an old column in a later release, after the application stops using it. If no order keeps every step valid, split the change across two releases. Keep definers, privileges, and parameter groups the same on every member cluster (see Parameter groups for Amazon Aurora). A move blocks DDL on the objects that it carries until the job ends. To compare member clusters, see Auditing member cluster configuration.
The unknown-table error is 1146, and the unknown-column error is 1054. For DDL that creates functions or triggers, keep log_bin_trust_function_creators the same on every member cluster.
Using schema-change tools
Use the same schema-change tools that you use on a standalone Aurora cluster. Run them over a direct connection to each member cluster's writer endpoint, or through a session pin, one run per member cluster, not through query routing. Give each tool its own account on each member cluster. Run one pipeline per group at a time, because a tool's lock covers only its own member cluster. Check for drift before and after every release. Keep the tools' shadow tables out of data mappings and routed reads.
Query routing doesn't support trigger-based tools such as pt-online-schema-change or binary-log-based tools such as gh-ost. Run them on each member cluster. For each tool's requirements, see the tool's own documentation.
The routing layer doesn't implement the advisory lock functions, such as GET_LOCK. A tool that takes an advisory lock to coordinate concurrent deploys, such as Flyway, must apply its changes over a direct connection or a session pin. That lock then covers only the member cluster that the tool reaches.
Deploying mapping and routing changes from a pipeline
No API restores these definitions on their own, so keep their desired state in source control, one object per statement. In the pipeline that runs the member DDL, add a stage that does the following:
-
Exports the definitions at the routing endpoint and compares them with source control.
-
Applies the member DDL and passes the drift check.
-
Applies the scripts at the routing endpoint with a command-line client in autocommit mode, and stops at the first error.
-
Exports the definitions again and compares them.
Require human approval of these scripts, because a wrong mapping doesn't fail but sends reads and writes to another member cluster.
Related pages
For related tasks and concepts, see the following pages: