

# 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](databaseclustergroups-sql-create-scaleout-routing-function.md) and [ALTER SCALEOUT TABLE](databaseclustergroups-sql-alter-scaleout-table.md).

## Options for changing a routing function
<a name="databaseclustergroups-changing-a-routing-function"></a>

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](#databaseclustergroups-changing-routing-function-switch). | 
| 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](#databaseclustergroups-changing-routing-function-new-table). | 
| 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](databaseclustergroups-rebalancing.md). | 

For the routing functions that are available in preview, see [Routing functions](databaseclustergroups-scaleout-tables.md#databaseclustergroups-routing-functions).

## Before you change a routing function
<a name="databaseclustergroups-changing-routing-function-before"></a>

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](databaseclustergroups-changing-mappings.md#databaseclustergroups-export-mappings). 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
<a name="databaseclustergroups-changing-routing-function-add-member"></a>

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.

1. 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](databaseclustergroups-changing-mappings.md#databaseclustergroups-changing-mappings-add).

1. 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:

```sql
ALTER SCALEOUT TABLE {{SCALEOUT_TABLE}} ADD MEMBER {{ROUTER_DB}}.{{NEW_MEMBER_TABLE}};
```

------

## Switching to a new routing function
<a name="databaseclustergroups-changing-routing-function-switch"></a>

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.

1. 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](#databaseclustergroups-changing-routing-function-add-member).

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

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

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

1. 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](databaseclustergroups-errors-scaleout.md#databaseclustergroups-data-mapping-and-scaleout-ddl-errors) (42000). For the error text, see [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md). 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](databaseclustergroups-sql-create-scaleout-routing-function.md).

------
#### [ Aurora MySQL ]

```sql
-- 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
<a name="databaseclustergroups-changing-routing-function-new-table"></a>

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](databaseclustergroups-create-scaleout-table.md).

1. Backfill it and keep it synchronized, as described in [Running a keyed backfill](databaseclustergroups-backfill.md#databaseclustergroups-running-a-keyed-backfill).

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

1. 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](databaseclustergroups-schema-changes.md#databaseclustergroups-schema-changes-scaleout-ddl).

## Checking a routing function change
<a name="databaseclustergroups-changing-routing-function-check"></a>

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.

## Related pages
<a name="databaseclustergroups-changing-routing-function-related"></a>

For related tasks and concepts, see the following pages:
+ [Scaleout tables and routing functions](databaseclustergroups-scaleout-tables.md)
+ [Choosing a routing key and routing function](databaseclustergroups-routing-key.md)
+ [Copying data and running backfills](databaseclustergroups-backfill.md)