SET router_cluster_pin
Pins a session at the routing endpoint of a database cluster group for Aurora to one member cluster, or clears the pin. Session pinning is a standard way to work with one member cluster through the routing endpoint, alongside routing: a pinned session passes its statements through, including DDL, administrative statements, and catalog reads.
Description
The routing layer passes the statements of a pinned session through to the pinned member cluster unchanged, and intercepts only SET SESSION router_cluster_pin. Any statement that the member cluster accepts runs there, including DDL, access control statements, administrative statements, catalog reads, and temporary tables. On Aurora MySQL, this includes XA transactions. Setting the variable to a different target moves the session to another member cluster in one statement, with no reconnection. Clearing the pin returns the session to query routing mode.
Use a pin when a task needs one member cluster, such as the following tasks:
Roll out DDL on each member cluster in turn.
Run administrative statements.
Read a member cluster's catalog.
Reach a member table by its cluster name.
For what a pin is and when to use one, see Session pinning. For the variable itself, see router_cluster_pin.
Syntax
The database part of the target is optional, as the parameters describe.
SET SESSION router_cluster_pin = 'member_cluster_alias[@cluster_db]'; SET SESSION router_cluster_pin = { DEFAULT | '' };
Parameters and options
The statement takes the following parameters.
member_cluster_alias-
A
member_cluster_aliasvalue fromdbclustergroup_sys.member_cluster_mappings. It isn't the DB cluster identifier. Name the target directly. The routing layer refuses a predicate or a routing function expression as a target. - @
cluster_db -
A database on that member cluster, by its cluster name, not its router name. On Aurora MySQL, it's optional, and it runs as a
USEof that database right after the pin. - { DEFAULT | '' }
-
Clears the pin and returns the session to query routing mode.
Where it runs and required privileges
Run the statement at the routing endpoint, in a pinned or an unpinned session, outside a transaction. No parameter controls pinning, and the statement needs no privilege for database cluster groups for Aurora: any router user can pin a session. The router user must have a user of the same name on the member cluster, and that member cluster user's grants limit what the session can reach.
Usage notes
Keep the following in mind when you use the statement.
-
Set and clear a pin with
SET SESSIONor a bareSET, as the only statement in the request, with a single-quoted value. Drivers and connection pools can send it in a post-connect hook. The routing layer refusesSET GLOBALandSET LOCALforms. -
The routing layer refuses a pin inside an open transaction, while
LOCK TABLESholds a member cluster, and for an alias that names no member cluster. It refuses the return to query routing mode inside an open transaction and while a change of pin target is in progress. On Aurora MySQL withautocommit = 0, the first read or write opens a transaction, so commit or roll back before you pin or unpin. -
When you pin, the routing layer replays the session state on the member cluster. The replay starts with a
USEof the database that the pin names, or of the session's current database when the pin names none. If that database doesn't exist on the member cluster, or your member cluster user can't use it, the routing layer refuses the pin. The session stays as it was. The rest of the replay, such as session variables and prepared statements, doesn't decide whether the pin succeeds. If the member cluster rejects one of those statements, the pin still takes effect and returns OK. ASHOW WARNINGSright after the pin shows the member cluster's error. The member cluster doesn't run the statements after the rejected one in the replay, including your prepared statements, and including starting values such asSET NAMESandautocommitwhen they come after it. After you pin, set the session variables that your application depends on and prepare your statements again. -
A pinned session uses cluster names. Name the cluster database in the pin, as in
', because the pin runsalias@cluster_db'USEof that name on the member cluster. Router names and scaleout table names don't resolve on the member cluster. A text prepared statement (PREPARE) from before the pin works in the pinned session when the tables it uses are on the pinned member cluster, because the routing layer prepares it again there with the cluster names. If any table it uses is on another member cluster, running it in the pinned session returns an error. Server-side prepared statements, which most drivers use, aren't prepared again. Running one that you prepared before the pin, or before a later pin change, returns error 1235. Prepare it again in the new pin state. -
Writes to a scaleout table's member table through a pin bypass routing. Keep each row on the member cluster that its key maps to.
-
A pinned session forwards a statement on a
dbclustergroup_systable, and everySCALEOUTstatement, to the member cluster, where the statement fails. Clear the pin to change data mappings, routing functions, or scaleout tables. -
On Aurora MySQL, the state of an open XA transaction decides whether the session can unpin. After
XA STARTorXA END, the routing layer refuses the unpin with error 1179. AfterXA PREPAREorXA COMMIT ... ONE PHASE, it allows the unpin. A prepared XA transaction stays prepared on the member cluster after the unpin, andXA RECOVERthere lists it. Query routing mode refuses XA statements with error 1235. To end the prepared transaction, pin to that member cluster again or connect to its endpoint, and runXA COMMITorXA ROLLBACK. -
Temporary tables, session variables, and other session state persist while the session is pinned, and temporary objects don't block the return to query routing mode.
-
A data mapping change doesn't move or end a pinned session. The new mapping applies the next time the session pins.
-
If the member cluster connection fails, the routing layer doesn't reconnect or restore the session state. Reconnect, and then pin again.
A pinned session can't read its own pin target, because it forwards every statement to the member cluster, including a read of @@router_cluster_pin. On Aurora MySQL, read the pin from another session at the routing endpoint that isn't pinned. In dbclustergroup_sys.router_processlist, the pin_status column shows whether each session is pinned, and member_cluster_alias shows the member cluster. Note the session's CONNECTION_ID() before it pins, because in a pinned session the member cluster answers CONNECTION_ID() with its own ID. See Process list table: router_processlist.
Examples
The following example pins the session to cluster_b with a database named by its cluster name, reads a member table and the member cluster's catalog, and clears the pin.
SET SESSION router_cluster_pin = 'cluster_b@app'; SHOW CREATE TABLE app.orders_m5; SELECT table_name FROM information_schema.tables WHERE table_schema = 'app'; SET SESSION router_cluster_pin = DEFAULT;
To roll out DDL on each member cluster through a pin, see Running DDL through a session pin.
Errors
The statement can return the following errors. For all session errors, see Session and transaction errors.
| Aurora MySQL | Meaning |
|---|---|
| 64004 (42000) | Member cluster '<alias>' doesn't exist: the value isn't a known member cluster alias. |
| 1231 (42000) | The router_cluster_pin value must be a quoted string, The router_cluster_pin variable takes a single value, or Member cluster and database name must both be non-empty: the value isn't a single-quoted string, assigns more than one value, has an empty alias or database part. A pinned session that unpins or pins to another member cluster while a character set or collation variable holds a value that the routing layer doesn't support also gets 1231, with The <variable> variable is '<value>', which ... SET it to '<supported value>' on the pinned session and retry the unpin (or re-pin). |
| 1192 (HY000) | The session is not pinned, A pin is already in progress, or The session holds table locks. UNLOCK TABLES first. |
| 1235 (42000) | You sent the pin together with another statement or assignment, or named a routing function expression. Send the pin or unpin as the only statement in the request. On Aurora MySQL, a pin to the member cluster that the session is already pinned to also returns this error, with Changing the database without a pinning destination is not allowed, when it names a different database part, or adds or drops the database part. Repeating the same pin succeeds and changes nothing. To change the database on the pinned member cluster, run USE, or unpin and pin again. |
| 1228 (HY000) | A SET GLOBAL or SET LOCAL form of the pin. A SET @@GLOBAL.router_cluster_pin assignment returns 1235 instead. Use SET SESSION or a bare SET. |
| 1179 (25000) | You are not allowed to pin in a transaction. COMMIT or ROLLBACK first. An unpin returns the same message with unpin in place of pin. The open transaction stays as it was. Run COMMIT or ROLLBACK first. |
| 1429 (HY000) | The member cluster is unreachable, or Internal routing error when the pin couldn't be committed. The routing layer refuses the pin, and the previous pin stays in effect. |
| 3170 (HY000) | The member cluster is not accepting new sessions. Retry later. The routing layer refuses the pin, and the previous pin stays in effect. |
| 1049 (42000) | Unknown database '<db>', or The member cluster rejected the pin: the named database doesn't exist on the target member cluster, or the member cluster refused the pin. When the member cluster user has no access to the database, the member cluster's own error, such as 1044, is returned instead. The routing layer refuses the pin, and the previous pin stays in effect. |
| 1045 (28000) | The member cluster has no user with the router user's name. The routing layer refuses the pin, and the previous pin stays in effect. |
| 1162 (42000) | The session state is too large to replay on the target. |
Related statements
For more information, see the following: