Skip to content

Commit 5c91200

Browse files
authored
docs: support logging session connect attrs to slow query log (#22547)
1 parent 6d6ae31 commit 5c91200

5 files changed

Lines changed: 60 additions & 2 deletions

File tree

information-schema/information-schema-slow-query.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,16 @@ The output is as follows:
126126
| Plan_digest | varchar(128) | YES | | NULL | |
127127
| Binary_plan | longtext | YES | | NULL | |
128128
| Prev_stmt | longtext | YES | | NULL | |
129+
| Session_connect_attrs | json | YES | | NULL | |
129130
| Query | longtext | YES | | NULL | |
130131
+--------------------------------------------+-----------------+------+------+---------+-------+
131-
89 rows in set (0.00 sec)
132+
90 rows in set (0.00 sec)
132133
```
133134

134135
The maximum statement length of the `Query` column is limited by the [`tidb_stmt_summary_max_sql_length`](/system-variables.md#tidb_stmt_summary_max_sql_length-new-in-v40) system variable.
135136

137+
The `Session_connect_attrs` column stores session connection attributes in JSON format parsed from the slow log. TiDB controls the maximum payload size written to this field using [`performance_schema_session_connect_attrs_size`](/system-variables.md#performance_schema_session_connect_attrs_size-new-in-v900).
138+
136139
## CLUSTER_SLOW_QUERY table
137140

138141
The `CLUSTER_SLOW_QUERY` table provides the slow query information of all nodes in the cluster, which is the parsing result of the TiDB slow log files. You can use the `CLUSTER_SLOW_QUERY` table the way you do with `SLOW_QUERY`. The table schema of the `CLUSTER_SLOW_QUERY` table differs from that of the `SLOW_QUERY` table in that an `INSTANCE` column is added to `CLUSTER_SLOW_QUERY`. The `INSTANCE` column represents the TiDB node address of the row information on the slow query.
@@ -246,9 +249,10 @@ The output is as follows:
246249
| Plan_digest | varchar(128) | YES | | NULL | |
247250
| Binary_plan | longtext | YES | | NULL | |
248251
| Prev_stmt | longtext | YES | | NULL | |
252+
| Session_connect_attrs | json | YES | | NULL | |
249253
| Query | longtext | YES | | NULL | |
250254
+--------------------------------------------+-----------------+------+------+---------+-------+
251-
90 rows in set (0.00 sec)
255+
91 rows in set (0.00 sec)
252256
```
253257

254258
When the cluster system table is queried, TiDB does not obtain data from all nodes, but pushes down the related calculation to other nodes. The execution plan is as follows:

performance-schema/performance-schema-session-connect-attrs.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,18 @@ Fields in the `SESSION_CONNECT_ATTRS` table are described as follows:
6666
* `ATTR_NAME`: Attribute name.
6767
* `ATTR_VALUE`: Attribute value.
6868
* `ORDINAL_POSITION`: Ordinal position of the name/value pair.
69+
70+
## Size limit and truncation
71+
72+
TiDB uses the [`performance_schema_session_connect_attrs_size`](/system-variables.md#performance_schema_session_connect_attrs_size-new-in-v900) global system variable to control the maximum total size of connection attributes per session.
73+
74+
- Default value: `4096` bytes
75+
- Range: `[-1, 65536]`
76+
- `-1` means no configured limit, and TiDB treats it as up to `65536` bytes.
77+
- `0` means that TiDB does not retain client-provided session connection attributes, which effectively disables recording session attributes.
78+
79+
When the total size exceeds this limit, TiDB truncates excess attributes and adds `_truncated` to indicate the number of truncated bytes.
80+
81+
The accepted connection attributes are also written to the `Session_connect_attrs` field in the slow log and can be queried from [`INFORMATION_SCHEMA.SLOW_QUERY`](/information-schema/information-schema-slow-query.md) and `INFORMATION_SCHEMA.CLUSTER_SLOW_QUERY`. To control the payload size written to the slow log, adjust `performance_schema_session_connect_attrs_size`.
82+
83+
TiDB also enforces a hard limit of 1 MiB on connection attribute payload in handshake packets. If this hard limit is exceeded, the connection is rejected.

status-variables.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ Additionally, the [FLUSH STATUS](/sql-statements/sql-statement-flush-status.md)
3131
- Type: Integer
3232
- The compression level that is used for the MySQL Protocol.
3333

34+
### Performance_schema_session_connect_attrs_longest_seen
35+
36+
- Scope: GLOBAL
37+
- Type: Integer
38+
- The largest total size (in bytes) of session connection attributes observed by TiDB.
39+
40+
### Performance_schema_session_connect_attrs_lost
41+
42+
- Scope: GLOBAL
43+
- Type: Integer
44+
- The number of sessions whose connection attributes were truncated due to `performance_schema_session_connect_attrs_size`.
45+
3446
### Ssl_cipher
3547

3648
- Scope: SESSION | GLOBAL

system-variable-reference.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,13 @@ Referenced in:
600600
- [TiDB 8.5.0 Release Notes](/releases/release-8.5.0.md)
601601
- [TiDB 7.6.0 Release Notes](/releases/release-7.6.0.md)
602602

603+
### performance_schema_session_connect_attrs_size
604+
605+
Referenced in:
606+
607+
- [`SESSION_CONNECT_ATTRS`](/performance-schema/performance-schema-session-connect-attrs.md)
608+
- [System Variables](/system-variables.md#performance_schema_session_connect_attrs_size-new-in-v900)
609+
603610
### plugin_dir
604611

605612
Referenced in:

system-variables.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,26 @@ mysql> SHOW GLOBAL VARIABLES LIKE 'max_prepared_stmt_count';
803803
* In a cluster with a large number of Regions, the PD leader experiences high CPU pressure due to the increased overhead of handling heartbeats and scheduling tasks.
804804
* In a TiDB cluster with many TiDB instances, the PD leader experiences high CPU pressure due to a high concurrency of requests for Region information.
805805

806+
### performance_schema_session_connect_attrs_size <span class="version-mark">New in v9.0.0</span>
807+
808+
- Scope: GLOBAL
809+
- Persists to cluster: Yes
810+
- Applies to hint [SET_VAR](/optimizer-hints.md#set_varvar_namevar_value): No
811+
- Type: Integer
812+
- Default value: `4096`
813+
- Range: `[-1, 65536]`
814+
- Unit: Bytes
815+
- Controls the maximum total size of connection attributes for each session.
816+
- If the total size of connection attributes exceeds this value, TiDB truncates excess attributes and adds `_truncated` to indicate the number of truncated bytes.
817+
- Connection attributes accepted within this limit are written to the `Session_connect_attrs` field in the slow log and can be queried from [`INFORMATION_SCHEMA.SLOW_QUERY`](/information-schema/information-schema-slow-query.md) and `INFORMATION_SCHEMA.CLUSTER_SLOW_QUERY`.
818+
- You can control the size of `Session_connect_attrs` recorded in the slow log by adjusting this variable.
819+
- If the value is set to `-1`, this means the limit is not configured and TiDB treats it as up to `65536` bytes.
820+
- If the value is set to `0`, TiDB does not retain client-provided session connection attributes, which effectively disables recording session attributes.
821+
822+
> **Note:**
823+
>
824+
> TiDB enforces a hard limit of 1 MiB for handshake connection attributes. If this hard limit is exceeded, the connection is rejected.
825+
806826
### plugin_dir
807827

808828
> **Note:**

0 commit comments

Comments
 (0)