

# Data mappings and collocations

In a database cluster group for Aurora with routing enabled, a data mapping tells the routing layer which member cluster holds a database or a table. The routing layer doesn't discover where your objects are. An object with no data mapping isn't reachable through the routing endpoint. Each mapping also names a collocation, the set of objects that stay together on one member cluster and move together.

You need data mappings only for the routing endpoint. Member cluster endpoints need no mapping.

## Data placement options
<a name="databaseclustergroups-data-mappings-placement"></a>

You decide placement, and you declare it. The size of the object decides which declaration fits:
+ A **database-level mapping** maps a whole database so that all of its objects are reachable through the routing endpoint. This is the most common starting point, for example a database for each tenant.
+ A **table-level mapping** maps one table of a database so that you can reach that table through the routing endpoint. Use it when you want a table reachable without exposing the other tables of its database.
+ A **scaleout table** spreads the rows of one table across several member clusters. Use it when one table is too large for one member cluster. For more information, see [Scaleout tables and routing functions](databaseclustergroups-scaleout-tables.md).

For the rules of each mapping level and how to change from one level to another, see [Using database-level and table-level mappings](databaseclustergroups-mapping-levels.md). For guidance on which declaration to use, see [Designing data placement](databaseclustergroups-choosing-routing.md).

The following diagram shows how the routing layer uses data mappings to send a statement to one member cluster.

![The routing layer reads the data mappings for a database, a table, and a scaleout table, and sends a query to member-b.](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/images/dbcg-data-mappings.png)


In the diagram, database `sales` maps to member-a, and table `app.audit` maps to member-c. A query for `customer_id` 42 in the scaleout table `app.orders` maps to collocation 2, so the routing layer sends it to member-b.

The following examples add a top-level mapping and a table-level mapping. You run them at the routing endpoint, in query routing mode, outside a transaction.

The following statement places the database `tenant_0001` on one member cluster, in its own collocation.

```sql
INSERT INTO dbclustergroup_sys.data_mappings
  (cluster_db_name, router_db_name, member_cluster_alias, router_collocation_group)
VALUES ('tenant_0001', 'tenant_0001', '{{MEMBER_CLUSTER_ALIAS}}', 'cg_tenant_0001');
```

The following statement places the table `retail_db.customer` on one member cluster under the router name `retail_db.customer_s1`.

```sql
INSERT INTO dbclustergroup_sys.data_mappings
  (cluster_db_name, cluster_table_name, router_db_name, router_table_name, member_cluster_alias, router_collocation_group)
VALUES ('retail_db', 'customer', 'retail_db', 'customer_s1', '{{MEMBER_CLUSTER_ALIAS}}', 'cg_customer');
```

For the full syntax, see [Data mapping statements](databaseclustergroups-sql-mappings.md).

The routing layer doesn't check a mapping against the member cluster. You can declare a layout ahead of the data and load the data later. A query fails until the object exists.

A mapping must name a member cluster alias that `dbclustergroup_sys.member_cluster_mappings` lists. Otherwise, the routing layer rejects the `INSERT` with error 64004 (SQLSTATE `42000`). For the error text, see [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md). The routing layer rejects a mapping to a member cluster that you're still adding in the same way until the list shows it. The alias of a removed member cluster stays in the list, so the routing layer accepts a mapping to it, and its first query fails. Validate the member cluster alias before you use it to create a data mapping. Do not derive the member cluster alias from the cluster identifier or any other information.

```sql
SELECT member_cluster_alias, db_cluster_identifier FROM dbclustergroup_sys.member_cluster_mappings;
```

Copy the cluster names with the same care, because a mapping with a wrong cluster name also registers without error.

Changing the member cluster alias of a mapping changes where the routing layer sends queries, but it doesn't copy the data. To move an object that holds data to another member cluster, move its collocation with data movement. For more information, see [Moving data between member clusters](databaseclustergroups-data-movement.md).

## Router names and cluster names in data mappings
<a name="databaseclustergroups-aliases-in-data-mappings"></a>

Every mapping carries a router name, the name that your application uses through the routing endpoint (`router_db_name`, `router_table_name`). It also carries a cluster name, the name of the object on its member cluster (`cluster_db_name`, `cluster_table_name`). When you supply no router name, the cluster name becomes the router name.

A router name fixes a name conflict that the group can't otherwise handle. Two member clusters can each hold a table called `retail_db.customer`, and the routing layer needs one name that reaches exactly one of them. Every fully qualified router name must therefore be unique across the whole group. The routing layer rejects a duplicate. An application that runs the same schema on many clusters needs one router name for each mapping and no table changes.

In query routing mode, every routed statement resolves objects by router name, not by cluster name. A pinned session and a direct connection use cluster names. For a member table of a scaleout table, the router name is the name that the scaleout table uses in the definition. The application queries the scaleout table by its own name.

Every mapping names explicit values. A database or table name in a mapping can hold any character except `.`, whitespace, control characters, characters above U\+FFFF, and ```, `"`, `'`, `\`, `,`, `(`, `/*`, and `*/`. It must not be empty or reserved, and fits 64 characters on Aurora MySQL. Collocation names allow only letters, digits, `_`, `$`, and `-`. Member cluster aliases start with a letter and allow only letters, digits, and `-`. To place every table of a database on one member cluster, map the database itself.

## Collocations
<a name="databaseclustergroups-collocation-groups"></a>

A collocation is a named group of data mappings that stay on the same member cluster and move between member clusters together. A table or a database joins a collocation through the collocation value of its data mapping. A collocation lives on one member cluster, and a member cluster holds many collocations. The first mapping that names a collocation creates it and fixes its member cluster. The routing layer rejects a later mapping that names it with another member cluster.

The following diagram shows three collocations of related scaleout table rows placed on two member clusters.

![Three collocations of related customers, orders, and order_items rows, placed on two member clusters.](https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/images/dbcg-collocation-groups.png)


In the diagram, each collocation holds one customer's rows from the `customers`, `orders`, and `order_items` scaleout tables. Collocations 1 and 2 are on member-a, and collocation 3 is on member-b.

The collocation is the unit of movement. A data movement job moves whole collocations and can't divide one. The number of collocations that you create decides how finely you can rebalance later.

A join between tables in one collocation always runs on one member cluster. A join or transaction over mapped tables in two collocations that share a member cluster runs, but the routing layer refuses it after a move puts them on different member clusters. On a scaleout table, a statement whose key values fall in two collocations is refused with error 1235, `No member cluster could be resolved for this query`, even while both collocations share a member cluster. Put the tables that you join or write together in one collocation.

A scaleout table is split into parts called member tables, and each part lives in a collocation. A collocation can hold only one part of the same scaleout table. For example, if `orders` is split into `orders_1` and `orders_2`, the two parts must be in different collocations.

A mapping that names no collocation gets the default collocation, which is the member cluster alias. Every such object on that member cluster is then in one collocation. A move of one of them by itself fails. You can move one mapping to another collocation later with an `UPDATE` that sets `router_collocation_group` and `member_cluster_alias` together, but the routing layer doesn't move its data. For how to plan collocations, see [Planning collocations and headroom](databaseclustergroups-collocation-planning.md).

## Applying data mapping changes
<a name="databaseclustergroups-when-a-data-mapping-change-takes-effect"></a>

Data mapping and routing function changes, and the routing switch at the end of a data move, reach the routing layer within a short time. Until then, the routing layer can still route a statement on the older mapping. A statement routed on an older mapping gets Aurora MySQL error 1412. The session stays open, and a retry routes on the new mapping.

Inside a transaction, a statement that needs a mapping changed after the transaction's first routed statement fails with error 63928 (Aurora MySQL), and the routing layer rolls back the transaction. Retry the whole transaction. For the full retry rule, see [Transaction errors and retries](databaseclustergroups-sessions.md#databaseclustergroups-when-a-mapping-change-reaches-an-open-transaction).

A pinned session doesn't route each statement. A mapping change doesn't move a session that's already pinned. A direct connection doesn't use data mappings.

Each mapping write is atomic, but mapping changes aren't transactional. A multi-row `INSERT` applies all its rows or none, but `BEGIN` and `COMMIT` don't group separate mapping statements into one unit. Stage a large edit so that each intermediate state still routes.

The routing layer refuses a write to `dbclustergroup_sys.data_mappings` inside a transaction, and the write changes nothing. Issue each change outside a transaction, and check its result before the next one. On Aurora MySQL, a session with `autocommit=0` counts as an open transaction.

If the group can't accept mapping changes for a time, the routing layer keeps serving queries with the existing mappings. During that time, a mapping change fails, so retry it later.

While a data movement job moves a collocation, the routing layer refuses a mapping write that touches the collocation or any object in it, with error 1205 (`Metadata for ... is being changed by another operation`).

An `INSERT` is accepted while the member cluster that it names can't be reached, because creating a data mapping doesn't connect to the member cluster. An `UPDATE` or `DELETE` fails while a member cluster that holds the mapped data has no available writer instance. When you move a collocation to another member cluster, this applies only to the member cluster you move it from. The statement fails with error 3168, `The database cluster group metadata is temporarily unavailable`. Retry after the member cluster is available, or remove the member cluster from the database cluster group for Aurora first.

One statement can add, change, or remove at most 1,000 data mappings. For more information about the limit, see [Routing metadata and naming limits](databaseclustergroups-limitations.md#databaseclustergroups-limitations-routing-metadata-limits). A larger `INSERT`, `UPDATE`, or filtered `DELETE` fails with error 1235 and changes nothing. For example, an `INSERT` of 1,500 mappings returns `put of 1500 entries exceeds the per-request limit of 1000; split the write into smaller requests`. A `DELETE` without a `WHERE` clause removes at most 1,000 mappings for each statement, returns a warning when mappings remain, and leaves them for the next statement. A database cluster group for Aurora holds at most 1,000,000 data mappings.

## Managing data mappings
<a name="databaseclustergroups-data-mappings-managing"></a>

You read and change mappings with `SELECT`, `INSERT`, `UPDATE`, and `DELETE` on `dbclustergroup_sys.data_mappings`, at the routing endpoint in query routing mode. A pinned session passes these statements through to the member cluster. To write mappings, a router user needs the `dbclustergroup_metadata_admin` privilege, which also covers the definitions of scaleout tables and routing functions. To read them, a router user needs `dbclustergroup_metadata_read_only` or `dbclustergroup_metadata_admin`. After each write, read the row back and check its member cluster alias and cluster names.

The routing layer accepts only simple statements. It rejects `ORDER BY`, and modifiers on a write such as `IGNORE`, `ON DUPLICATE KEY`, `REPLACE INTO`, and `LIMIT`. A `SELECT` accepts `LIMIT`. For the read, see [SELECT FROM dbclustergroup\_sys.data\_mappings](databaseclustergroups-sql-select-data-mappings.md). A `WHERE` clause compares one column with one value, or one tuple of columns with one tuple of values, for example `WHERE (router_db_name, router_table_name) = ('retail_db', 'customer_s1')`. It rejects `AND`, `OR`, `IN` lists, and operators other than `=`.

**Warning**  
A `DELETE` with no `WHERE` clause removes the data mappings of the whole group, up to 1,000 for each statement, except the mappings that scaleout tables use. Every object whose mapping it removes becomes unreachable through the routing endpoint. Before every change, export the mappings. For more information about exporting, see [Exporting the mappings before and after a change](databaseclustergroups-changing-mappings.md#databaseclustergroups-export-mappings).

## Data mapping names and errors
<a name="databaseclustergroups-data-mappings-engines"></a>

The following table lists the data mapping column names and error codes.


| Item | Aurora MySQL | 
| --- | --- | 
| Data mappings system table | dbclustergroup\_sys.data\_mappings | 
| List of member cluster aliases | dbclustergroup\_sys.member\_cluster\_mappings | 
| Cluster name columns | cluster\_db\_name, cluster\_table\_name | 
| Router name columns | router\_db\_name, router\_table\_name. For a member table of a scaleout table, router\_table\_name is the handle that CREATE SCALEOUT TABLE names. | 
| Member cluster and collocation columns | member\_cluster\_alias, router\_collocation\_group | 
| Privilege to write mappings | dbclustergroup\_metadata\_admin | 
| Privilege to read mappings | dbclustergroup\_metadata\_read\_only or dbclustergroup\_metadata\_admin | 
| Unknown member cluster alias | Error 64004 (SQLSTATE 42000) | 
| Duplicate router name | Error 64021 (SQLSTATE 42000) | 
| A mapping write inside a transaction, or a statement over collocations on two member clusters | Error 1235 | 
| Moving one object of a shared collocation | Not possible. The move statement names a whole collocation; any other move type is a syntax error (1064). | 
| Statement routed on an older mapping | Error 1412 | 
| Filtering by cluster identifier | The routing layer rejects db\_cluster\_identifier in a WHERE clause on dbclustergroup\_sys.data\_mappings. Filter by member\_cluster\_alias. | 

A session can move between router database names without reconnecting. For the statements, see [Data mapping statements](databaseclustergroups-sql-mappings.md). For the columns, see [System tables for database cluster groups for Aurora](databaseclustergroups-system-views.md).