View a markdown version of this page

Changing a routing function - Amazon Aurora

Changing a routing function

In a database cluster group for Aurora, a routing function maps the routing key values of a scaleout table to collocations. A routing function has no ALTER statement, so you create a function under a new name and switch each scaleout table to it. The switch changes only metadata and moves no rows, so change the routing function of a scaleout table only while the table is empty. If the new function sends a row to another collocation, you can no longer read that row by its routing key.

Run each statement at the routing endpoint, in query routing mode, outside a transaction, as a router user that has dbclustergroup_metadata_admin. For the grammar, see CREATE SCALEOUT ROUTING FUNCTION and ALTER SCALEOUT TABLE.

Options for changing a routing function

Use the following table to choose how to change the routing function of a scaleout table.

If Do this
The scaleout table is empty on every member cluster. For example, you want a different number of collocations before you load data. Switch the table to a new routing function. Switching points the scaleout table at the new routing function. For more information, see Switching to a new routing function.
The scaleout table holds rows, or you need a different routing key column. Create a new scaleout table, backfill it with the existing rows, and keep it synchronized. For more information, see Changing the key or splitting a busy collocation.
The data is already placed unevenly across member clusters (skew). Move whole collocations to spread the data. Switching the routing function doesn't fix skew. For more information, see Rebalancing member clusters.

For the routing functions that are available in preview, see Routing functions.

Before you change a routing function

Before you change a routing function, do the following:

  • Export every scaleout table and routing function definition with your change record, as described in Exporting the mappings before and after a change. You can't read back a dropped routing function.

  • List every scaleout table in the same collocations. Every scaleout table in one collocation must use the identical routing function, so you change each of them. A table that takes its function with USING ROUTING FUNCTION OF follows the new function of its parent automatically. Before you switch the parent, give that table a member table in each new collocation, or the routing layer rejects the switch. A table that names the old function directly keeps it until you switch that table too.

  • Check that every table that you switch has a key column with the same name and type as the new function's parameter.

  • Rehearse the change on a test group, and plan a maintenance window.

Adding a member table

Every collocation that the new routing function targets needs exactly one member table of each scaleout table that uses the function. Use collocation names that no data mapping in the group uses, because a collocation can't hold two member tables of the same scaleout table. A data mapping creates the collocation. Adding a member table doesn't change routing, and the routing layer accepts a member table whose collocation isn't in the current routing function.

To add a member table
  1. Through a session pin or a direct connection to the member cluster for the new collocation, create the member table with the same definition as the other member tables.

  2. At the routing endpoint, map the new member table with router_collocation_group set to the new collocation name, as described in Adding a mapping.

  3. At the routing endpoint, add the member table to the scaleout table by its router table name (the router_table_name of its mapping), one member table per statement.

Aurora MySQL

Step 3:

ALTER SCALEOUT TABLE SCALEOUT_TABLE ADD MEMBER ROUTER_DB.NEW_MEMBER_TABLE;

Switching to a new routing function

Use this procedure only for a scaleout table that is empty on every member cluster.

To change the routing function of an empty scaleout table
  1. On each member cluster, through a session pin or a direct connection, count the rows of each member table, for example SELECT count(*) FROM CLUSTER_TABLE;. The routing layer can't count the rows of a scaleout table, because the statement carries no routing key. Continue only when every count is 0.

  2. For each collocation of the new function, add a member table to every scaleout table that you change, as described in Adding a member table.

  3. At the routing endpoint, create the new function under a new name. A routing function can name only a collocation that a data mapping already created.

  4. At the routing endpoint, switch the scaleout table to the new function. Then switch every other scaleout table in the same collocations that names the old function directly. Don't query these tables between the switches, because they then route the same key to different collocations.

  5. At the routing endpoint, remove each member table of an old collocation from the scaleout table, and then delete its data mapping. The routing layer rejects the delete of a data mapping that a scaleout table still names as a member.

  6. When no table uses the old function, drop it at the routing endpoint. If the routing layer refuses the drop, a scaleout table still uses the function. Switch that table, and then drop the function again.

The routing layer checks a switch against the scaleout table itself only. If the table has no member table in a collocation that its new function targets, the routing layer rejects the switch, changes nothing, and names the collocations that have no member table. On Aurora MySQL, the error is 64066 (42000). For the error text, see Error codes for database cluster groups for Aurora. The routing layer doesn't compare the new function with the functions of other tables in the same collocations, so you switch each of them yourself. It accepts a state in which one table uses the new function and another table in the same collocations names the old one, so you can switch the tables one at a time.

The following tabs show steps 3 through 6. HASH functions use their own PARTITION BY form. For the grammar, see CREATE SCALEOUT ROUTING FUNCTION.

Aurora MySQL
-- Step 3: create the function CREATE SCALEOUT ROUTING FUNCTION NEW_ROUTING_FUNCTION(KEY_COLUMN BIGINT) AS PARTITION BY MOD(KEY_COLUMN, DIVISOR) IN TARGET COLLOCATION (NEW_COLLOCATION_GROUP_0 VALUES IN (0), NEW_COLLOCATION_GROUP_1 VALUES IN (1), ...); -- Step 4: switch the table ALTER SCALEOUT TABLE SCALEOUT_TABLE ROUTE BY KEY(KEY_COLUMN POSITION(KEY_POSITION)) USING ROUTING FUNCTION NEW_ROUTING_FUNCTION; -- Step 5: remove an old member table, then delete its mapping ALTER SCALEOUT TABLE SCALEOUT_TABLE DROP MEMBER ROUTER_DB.OLD_MEMBER_TABLE; DELETE FROM dbclustergroup_sys.data_mappings WHERE (router_db_name, router_table_name) = ('ROUTER_DB', 'OLD_MEMBER_TABLE'); -- Step 6: drop the old function DROP SCALEOUT ROUTING FUNCTION OLD_ROUTING_FUNCTION;

Changing the key or splitting a busy collocation

To change the routing function or the routing key of a scaleout table that holds rows, build a new scaleout table. A routing function switch moves no rows, so switch it only on an empty scaleout table, and data movement can't move part of a table's rows. Build the new table by completing the following steps:

  1. Create a new scaleout table with the new routing function or key, as described in Creating and loading a scaleout table.

  2. Backfill it and keep it synchronized, as described in Running a keyed backfill.

  3. In the application, switch reads and then writes to the new table, and check the results.

  4. Keep the old table until you verify the new one, and then drop the old table.

The routing layer doesn't detect DDL that changes a scaleout table's key column on one member cluster. For more information, see Changing scaleout tables.

Checking a routing function change

After each change, before you drop the old routing function, check the following:

  • At the routing endpoint, SHOW CREATE SCALEOUT TABLE shows the new function for each switched table, including the tables that inherit it.

  • A new export differs from the first export only in the mapping rows that you added and removed.

For related tasks and concepts, see the following pages: