View a markdown version of this page

Routing layer session variables - Amazon Aurora

Routing layer session variables

The routing layer of a database cluster group for Aurora reads the following session variables itself. They aren't member cluster parameters. You set router_cluster_pin and scaleout_tables_preferred_member in a session at the routing endpoint, with SET SESSION or a bare SET. To read the session's global connection ID, run SELECT CONNECTION_ID() at the routing endpoint. It's the id column of router_processlist and the ID that KILL takes. On a member cluster, the connection attribute _rds_router_connection_id carries it. To follow a session to its member cluster with this ID, see Tracing a session or statement across the group. For how the routing layer handles your other settings, see Supported system variables.

Variable What it does Default Privilege
router_cluster_pin Pins the session to one member cluster, or returns it to query routing. Not pinned None. Any router user can pin.
dbclustergroup_explain_routing_details Controls the routing layer information in EXPLAIN output. On None. Any router user can set it.
scaleout_tables_preferred_member Names the member table that answers catalog reads of a scaleout table. Not set None. Any router user can set it.

These variables aren't part of the group's routing endpoint parameters, which set one value for the whole group. For those parameters, see Configuring the routing endpoint. No privilege for database cluster groups for Aurora governs these variables. For the full list of these privileges, see Routing layer privileges.

router_cluster_pin

Use router_cluster_pin to pin one session to one member cluster. The routing layer then forwards every statement of the session to that member cluster unchanged, and intercepts only SET SESSION router_cluster_pin. For the concept and for when to pin, see Session pinning. For the statement and its errors, see SET router_cluster_pin.

The value names a member cluster by its alias from member_cluster_mappings, not by its DB cluster identifier. The database part, where you give one, is the cluster name that you use on a direct connection, not a router name. A pin behaves as follows:

  • Privileges. Any router user can pin, and a pin needs no privilege for database cluster groups for Aurora. A router user that has no user of the same name on a member cluster can't pin to it. The grants of that member cluster user limit what the session can reach.

  • Full SQL surface. A pinned session uses cluster names, and any statement that the member cluster accepts runs there. This includes DDL, access control statements, administrative statements, and catalog reads. To roll out a schema change this way, see Running DDL through a session pin.

  • Transactions. You can't pin inside an open transaction. Setting the variable to a different target moves the session to another member cluster in one statement, with no reconnection.

  • Returning to query routing. Set the variable to DEFAULT or to an empty string. The routing layer refuses the return inside an open transaction, while a change of pin target is in progress, and for SET GLOBAL or SET LOCAL instead of SET SESSION. Temporary objects on the member cluster don't block the return.

  • Names aren't converted. USE keeps its value when a session pins or unpins, so a name that resolved through the routing layer doesn't resolve on the member cluster. A prepared statement on a table that the routing layer reached by router name returns an error when you run it in the pinned session.

  • Scaleout tables and group statements. A pinned session can't name a scaleout table, and reaches its rows only through the member tables. The session forwards statements on dbclustergroup_sys and the SCALEOUT statements to the member cluster, where they fail. Make mapping and scaleout changes in an unpinned session.

  • Doesn't follow your data. A data mapping change doesn't move or end a pinned session. The new mapping takes effect the next time the session pins.

  • Lost member connection. If the member cluster connection fails, the routing layer doesn't reconnect or restore session state. Reconnect, then pin again.

  • Connection pools. Because the pin is a standard SET statement, drivers and connection pools send it unchanged, including in a post-connect hook. RESET doesn't release a pin, so clear the pin before you return a connection to a pool. For more information, see Connection pools and session pins.

Syntax
SET SESSION router_cluster_pin = { 'alias' | 'alias@database' | DEFAULT | '' };

The value is a single-quoted string, and the SET must be the only statement in the query.

The target is 'alias' or 'alias@database'. The database part is optional, and it runs as a USE of that database on the member cluster right after the session pins.

  • With autocommit = 0, the first statement opens a transaction, so commit or roll back before you pin or unpin.

  • A pinned session keeps one member cluster connection, so temporary tables and session variables persist between statements. Unpinning returns the session to query routing in the state that it held before the pin. In query routing mode, you can't pin while the session holds locks from LOCK TABLES; run UNLOCK TABLES first. In a pinned session, unpinning closes the member cluster session and releases its table locks.

  • A pin replays the session's state on the target member cluster, including its current database. If that database doesn't exist there, the routing layer refuses the pin and the session stays as it was. Pin with 'alias@database', or pin with no database selected.

  • A pinned session runs SHOW PROCESSLIST, XA transactions, temporary tables, and KILL of that member cluster's sessions.

  • XA statements run only in a pinned session. While an XA transaction is active, after XA START, or idle, after XA END, the session can't unpin and gets error 1179. After XA PREPARE or XA COMMIT ... ONE PHASE, it can. A prepared XA transaction stays on the member cluster after the unpin, and XA RECOVER there lists it. To end it, pin to that member cluster again or connect to it directly, and run XA COMMIT or XA ROLLBACK.

  • To check whether a session is pinned, read pin_status in router_processlist.

The following statement pins the session to member cluster member-b and selects the database app there, by its cluster name.

SET SESSION router_cluster_pin = 'member-b@app';

Either of the following statements returns the session to query routing.

SET SESSION router_cluster_pin = DEFAULT; SET SESSION router_cluster_pin = '';

A session can't read its own pin. On Aurora MySQL, SELECT @@router_cluster_pin returns NULL in query routing mode. A pinned session sends the statement to its member cluster, which returns error 1193, and a read of router_processlist in a pinned session also goes to the member cluster. To see whether a session is pinned, read pin_status in router_processlist from another session.

dbclustergroup_explain_routing_details

dbclustergroup_explain_routing_details is a Boolean that controls the routing layer information in EXPLAIN output. When it's on, which is the default, EXPLAIN at the routing endpoint adds the routing layer's details. On Aurora MySQL, a TRADITIONAL plan (the default format) starts with one routing row, with id 0, select_type ROUTER, table router, and the details in Extra; FORMAT=JSON, FORMAT=TREE, and EXPLAIN ANALYZE carry no routing details. When it's off, EXPLAIN returns only the member cluster's plan. For EXPLAIN at the routing endpoint, see EXPLAIN.

On Aurora MySQL, a plan in the TRADITIONAL format also carries a routing row, so a tool that reads the plan by row position reads that row too. Turn the details off for such a tool. The following statement turns the details off for the session.

SET SESSION dbclustergroup_explain_routing_details = OFF;

The default is ON. On Aurora MySQL, change it with SET [SESSION] dbclustergroup_explain_routing_details = { ON | OFF | 1 | 0 | DEFAULT } as the only assignment in the statement, outside a transaction. DEFAULT turns the details back on. SET GLOBAL is refused, and a SET inside a transaction or with autocommit = 0 returns error 1766. Read the value with SELECT @@dbclustergroup_explain_routing_details.

scaleout_tables_preferred_member

Aurora MySQL only. Use scaleout_tables_preferred_member to describe a scaleout table at the routing endpoint. A scaleout table has no table of its own on a member cluster, so its rows live in several member tables. The variable names the member table that answers SHOW CREATE TABLE, SHOW COLUMNS, SHOW INDEX, and DESCRIBE of the scaleout table. If it isn't set, or names a table that isn't a member of the scaleout table, these statements return error 1146. For the concept, see Scaleout tables and routing functions. For the accepted values and errors, see SET scaleout_tables_preferred_member.

Name the scaleout table alone, with no database. Name the member table by the router table name that its data mapping gives it, with its router database name, not by its cluster name. Separate entries for several scaleout tables with commas. The catalog reads answer from one member table at a time, so they can't show differences between member tables. Compare member tables on the member clusters directly.

SET SESSION scaleout_tables_preferred_member = 'scaleout_table:router_db.router_table[,...]';

The following statements answer catalog reads of app.orders from member table app.orders_m3.

SET SESSION scaleout_tables_preferred_member = 'orders:app.orders_m3'; SHOW CREATE TABLE app.orders;

The following statement sets member tables for two scaleout tables.

SET SESSION scaleout_tables_preferred_member = 'orders:app.orders_m3,items:app.items_m3';