

# Tracing a session or statement across the group

After you collect the facts in [Before you start: what to collect](databaseclustergroups-troubleshooting.md#databaseclustergroups-before-you-start-troubleshooting), trace an affected session to the member cluster and instance that ran its work. In a database cluster group for Aurora, you troubleshoot a member cluster as you troubleshoot any Aurora DB cluster. The added step links a routed session to the member cluster session behind it. Lock and transaction state is on the writer of the member cluster that holds the rows, and the routing layer doesn't show it. Then use the symptom pages in [Troubleshooting database cluster groups for Aurora](databaseclustergroups-troubleshooting.md). If every routed session is stuck, not only one, see [Troubleshooting an unreachable member cluster or a stall](databaseclustergroups-ts-stalls.md) instead.

Tracing is read-only. Ending a session is a separate step that you choose to run. To find a slow statement, end a statement or a session, or read lock waits, see [Troubleshooting slow queries, long-running statements, and lock waits](databaseclustergroups-ts-slow-queries.md).

## The global connection ID
<a name="databaseclustergroups-trace-session-global-id"></a>

The routing layer gives each client connection a global connection ID. It's a 32-bit identifier that is unique across the group, and it names both the connection and the part of the routing layer that holds it. It's always a positive integer, so a client that stores it in a signed integer field accepts it unchanged. A routed session has at most one member cluster session on each member cluster that it has reached, and later statements to that member cluster reuse it. That member cluster session carries the global connection ID, so you can match the two. You can also start from a `/* tid=... */` trace ID, or from a long transaction or lock wait on a member cluster.

At the routing endpoint, `SELECT CONNECTION_ID()` returns your own session's global connection ID, the same value as the `ID` column of `dbclustergroup_sys.router_processlist`. For another session, read the ID from that column. On the member cluster, a session that the routing layer opened carries the following two connection attributes, which you read in `performance_schema.session_connect_attrs`.


| Attribute | Value | 
| --- | --- | 
| \_rds\_connection\_source | The connection source of the session | 
| \_rds\_router\_connection\_id | The session's global connection ID, which is the ID column of router\_processlist | 

For a session that the routing layer opened, `_rds_connection_source` reads `ROUTER`. The routing layer sets no other value. A direct session has no such attribute.

These attributes require `performance_schema` to be turned on for each member cluster's instances. To check, run `SELECT @@performance_schema` on each instance endpoint (direct). It's a static parameter that you set in the DB parameter group, and it takes effect after a restart. For more information, see [Parameter groups for Amazon Aurora](USER_WorkingWithParamGroups.md). If it's turned off, match sessions by user and by the `/* tid=... */` comment instead, and note that you can't join a routed session to its member cluster session by ID. Classify sessions by `_rds_connection_source`, not by host, because a session that the routing layer opened shows a local host. The service's own sessions run as `rdsadmin`, `event_scheduler`, and `rds_aurora_*` users. To run the trace, use a member cluster user with `SELECT` on `performance_schema` and the `PROCESS` privilege.

## Tracing a session
<a name="databaseclustergroups-trace-session-procedure"></a>

**Before you begin**
+ Make sure that you can reach every instance endpoint of every member cluster. Session and statement views are per instance, and the cluster endpoint reaches only the writer.
+ Use a member cluster user that can read other users' sessions and statements, as described in [The global connection ID](#databaseclustergroups-trace-session-global-id).

**To trace a session across the group**

1. At the routing endpoint, read the routed session in the routing layer's process list by its global connection ID. The process list shows the sessions on every part of the routing layer that answers. A part that doesn't answer adds no rows, and the result carries a warning: run `SHOW WARNINGS` to see it. If a session that you expect is missing, rely on the member cluster side, which lists every routed session, and don't treat the session as ended. To see the sessions of other router users, you need `dbclustergroup_process_admin`.

1. List every instance of every member cluster. At the routing endpoint, read the whole `dbclustergroup_sys.member_cluster_mappings` table, with no filter. Then, with the AWS CLI, list the instances and instance endpoints of each member cluster with `describe-db-instances`, as for any Aurora DB cluster.

   ```bash
   # Runs at: AWS CLI, once for each member cluster.
   aws rds describe-db-instances --region {{REGION}} \
       --filters Name=db-cluster-id,Values={{MEMBER_CLUSTER_ID}} \
       --query 'DBInstances[].[DBInstanceIdentifier,Endpoint.Address]' --output text
   ```

1. On each instance endpoint (direct), in one pass, look for the member cluster session that carries the global connection ID. Record when the pass starts and ends, because sessions are short-lived, and rows that you read far apart can come from sessions that never existed at the same time. Label each row with its member cluster and instance, and leave out the row of your own session. A match names the instance and the member cluster session's ID, command, and current statement. If you start from a trace ID, search the statement history instead. If an instance can't be read, continue with the others and note which instances you didn't read. If nothing matches, the session might have ended, or it might not have reached that member cluster.

1. On the instance where you found the session (direct), run the standard flow for the symptom: its statements, its plan, and its waits. Map cluster names back to router names with `dbclustergroup_sys.data_mappings`.

1. If the session waits or blocks, on the member cluster's writer (direct), build the blocking chain, and match each end to its global connection ID. A blocker with no global connection ID is a direct connection, such as a schema tool. For the statements, see [Lock waits and long transactions on a member cluster](databaseclustergroups-ts-slow-queries.md#databaseclustergroups-ts-slow-queries-locks).

1. Choose what to do from [Choosing what to do](#databaseclustergroups-trace-session-next). To end a session, use the following statements.

**Step 1.** At the routing endpoint, read the routed session by its global connection ID, which you can also take from your application's log. Get your own session's ID with `SELECT CONNECTION_ID()`, or another session's from the `ID` column of `router_processlist`. Then filter with the number as a literal. An expression in a filter on the system tables is refused: `WHERE id = CONNECTION_ID()` returns `ERROR 1235: Only literal values are allowed. No expressions, subqueries, or variables.`

```sql
-- Runs at: routing endpoint. Your own session's global connection ID.
SELECT CONNECTION_ID();
-- Runs at: routing endpoint. The session, with the ID as a literal, for example 4711.
SELECT * FROM dbclustergroup_sys.router_processlist WHERE id = {{ROUTER_CONNECTION_ID}};
```

**Step 3.** On each instance endpoint (direct), find the member cluster session by its connection attribute. The attribute value is a string, so quote the ID.

```sql
SELECT @@hostname AS instance, t.PROCESSLIST_ID AS member_pid, t.PROCESSLIST_COMMAND AS command,
       t.PROCESSLIST_TIME AS seconds, LEFT(t.PROCESSLIST_INFO, 200) AS current_sql
FROM performance_schema.session_connect_attrs a
JOIN performance_schema.threads t ON t.PROCESSLIST_ID = a.PROCESSLIST_ID
WHERE a.ATTR_NAME = '_rds_router_connection_id' AND a.ATTR_VALUE = '{{ROUTER_CONNECTION_ID}}';
```

To list every session on an instance with its connection source and global connection ID, join `performance_schema.threads` to the connection attributes with a left join. A session with no attributes is a direct connection.

```sql
-- Runs at: each instance endpoint (direct).
SELECT @@hostname AS instance, t.PROCESSLIST_ID AS member_pid, t.PROCESSLIST_USER AS user,
       src.ATTR_VALUE AS connection_source, rid.ATTR_VALUE AS router_connection_id,
       LEFT(t.PROCESSLIST_INFO, 120) AS current_sql
FROM performance_schema.threads t
LEFT JOIN performance_schema.session_connect_attrs src
       ON src.PROCESSLIST_ID = t.PROCESSLIST_ID AND src.ATTR_NAME = '_rds_connection_source'
LEFT JOIN performance_schema.session_connect_attrs rid
       ON rid.PROCESSLIST_ID = t.PROCESSLIST_ID AND rid.ATTR_NAME = '_rds_router_connection_id'
WHERE t.TYPE = 'FOREGROUND' AND t.PROCESSLIST_ID IS NOT NULL;
```

For a trace ID, search `performance_schema.events_statements_current` and `performance_schema.events_statements_history` on each instance for `SQL_TEXT LIKE '/* tid={{TRACE_ID}}%'`. The trace comment is only on the member cluster side: `router_processlist` doesn't show it, and statement digests drop it. One trace ID can cover more than one member cluster statement. A `BEGIN`, and the session settings that the routing layer replays, share the trace ID of the statement that they come with. For a long transaction, start from the oldest `trx_started` in `information_schema.innodb_trx` on each writer.

The first 12 hexadecimal digits of a trace ID are the time that the statement was created, in milliseconds since the Unix epoch, in UTC. To read that time, run the following statement on a member cluster (direct).

```sql
-- Runs at: a member cluster (direct).
SELECT TIMESTAMP('1970-01-01 00:00:00') + INTERVAL CONV(SUBSTRING('{{TRACE_ID}}', 1, 12), 16, 10) DIV 1000 SECOND AS created_utc;
```

**Step 5.** On the member cluster's writer (direct), build the blocking chain from `performance_schema.data_lock_waits`. Join each end to `_rds_router_connection_id`, and read each ID in `router_processlist` at the routing endpoint, one literal ID per statement. The chain query is in [Lock waits and long transactions on a member cluster](databaseclustergroups-ts-slow-queries.md#databaseclustergroups-ts-slow-queries-locks).

**Step 6.** To cancel the running statement or end the session with `KILL` at the routing endpoint, see [A long-running statement or an idle session holds locks](databaseclustergroups-ts-slow-queries.md#databaseclustergroups-ts-slow-queries-long-running).

**From a member cluster session back to the routing endpoint.** Read the session's `_rds_router_connection_id` on the member cluster, and then read that ID in `router_processlist`. Measure transaction age only from `trx_started`, because the time columns restart with every statement.

For the columns of the routing layer's process list, see [Process list table: router\_processlist](databaseclustergroups-sysviews-router-processlist.md) and [System tables for database cluster groups for Aurora](databaseclustergroups-system-views.md).

## Choosing what to do
<a name="databaseclustergroups-trace-session-next"></a>

The following table maps what the trace found to an action and the page that covers it.


| Finding | Action | Page | 
| --- | --- | --- | 
| The session runs a slow statement | Tune the statement on that instance, on a direct connection | [A query is slow](databaseclustergroups-ts-slow-queries.md#databaseclustergroups-ts-slow-queries-slow) | 
| A long transaction holds locks that others wait on | Fix the application so that it commits or rolls back, or end the session | [A long-running statement or an idle session holds locks](databaseclustergroups-ts-slow-queries.md#databaseclustergroups-ts-slow-queries-long-running) | 
| The blocker has no global connection ID | It's a direct connection, such as a schema tool. Handle it on the member cluster | [Rolling out schema changes](databaseclustergroups-schema-changes.md) | 
| Every routed session is stuck | Find out whether one member cluster or the routing layer causes the stall | [Every client stalls or slows down](databaseclustergroups-ts-stalls.md#databaseclustergroups-every-client-stalls-or-slows-down) | 

## Related pages
<a name="databaseclustergroups-trace-session-related"></a>

The following pages cover related tasks and reference.
+ [Logs for database cluster groups for Aurora](databaseclustergroups-logging.md)
+ [Process list table: router\_processlist](databaseclustergroups-sysviews-router-processlist.md)
+ [Troubleshooting uneven load across member clusters](databaseclustergroups-ts-uneven-load.md)
+ [Error codes for database cluster groups for Aurora](databaseclustergroups-error-reference.md)
+ [Troubleshooting slow queries, long-running statements, and lock waits](databaseclustergroups-ts-slow-queries.md)