Repository navigation
tidb: global memory arbitrator mechanism (#22882) #24606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release-8.5
Are you sure you want to change the base?
Changes from all commits
2d85d2a
f9e2ec8
6789088
0a22583
cd11b7e
e00d660
4b72c61
0e8c29b
8c28055
de05998
a6b3949
e277f64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,23 @@ | |
|
|
||
| # TiDB Memory Control | ||
|
|
||
| Currently, TiDB can track the memory quota of a single SQL query and take actions to prevent OOM (out of memory) or troubleshoot OOM when the memory usage exceeds a specific threshold value. The system variable [`tidb_mem_oom_action`](/system-variables.md#tidb_mem_oom_action-new-in-v610) specifies the action to take when a query reaches the memory limit: | ||
| Currently, TiDB can track the memory quota of a single SQL query and take actions to prevent OOM (out of memory) or troubleshoot OOM when the memory usage exceeds a specific threshold value. | ||
|
|
||
| <CustomContent platform="tidb"> | ||
|
|
||
| The system variable [`tidb_mem_oom_action`](/system-variables.md#tidb_mem_oom_action-new-in-v610) specifies the action to take when a query reaches the memory limit: | ||
|
|
||
| - A value of `LOG` means that queries will continue to execute when the [`tidb_mem_quota_query`](/system-variables.md#tidb_mem_quota_query) limit is reached, but TiDB will print an entry to the log. | ||
| - A value of `CANCEL` means TiDB stops executing the SQL query immediately after the [`tidb_mem_quota_query`](/system-variables.md#tidb_mem_quota_query) limit is reached, and returns an error to the client. The error information clearly shows the memory usage of each physical execution operator that consumes memory in the SQL execution process. | ||
|
|
||
| </CustomContent> | ||
|
|
||
| <CustomContent platform="tidb-cloud"> | ||
|
|
||
| When a single SQL statement exceeds the memory quota specified by [`tidb_mem_quota_query`](/system-variables.md#tidb_mem_quota_query), TiDB Cloud stops executing the SQL query immediately and returns an error to the client. The error information clearly shows the memory usage of each physical execution operator that consumes memory in the SQL execution process. | ||
|
|
||
| </CustomContent> | ||
|
|
||
| ## Configure the memory quota of a query | ||
|
|
||
| The system variable [`tidb_mem_quota_query`](/system-variables.md#tidb_mem_quota_query) sets the limit for a query in bytes. Some usage examples: | ||
|
|
@@ -70,6 +82,8 @@ | |
|
|
||
| To view the memory-related operations and execution basis of the current instance or cluster, you can query the system table [`INFORMATION_SCHEMA.(CLUSTER_)MEMORY_USAGE_OPS_HISTORY`](/information-schema/information-schema-memory-usage-ops-history.md). For each instance, this table retains the latest 50 records. | ||
|
|
||
| <CustomContent platform="tidb"> | ||
|
|
||
| ## Trigger the alarm of excessive memory usage | ||
|
|
||
| When the memory usage of a tidb-server instance exceeds its memory threshold (70% of its total memory by default) and any of the following conditions is met, TiDB records the related status files and prints an alarm log. | ||
|
|
@@ -150,7 +164,7 @@ | |
| - When the disk spill is triggered, TiDB outputs a log containing the keywords `memory exceeds quota, spill to disk now` or `memory exceeds quota, set aggregate mode to spill-mode`. | ||
| - Disk spill for the Sort, MergeJoin, and HashJoin operators is introduced in v4.0.0. Disk spill for the non-parallel algorithm of the HashAgg operator is introduced in v5.2.0. Disk spill for the parallel algorithm of the HashAgg operator is introduced in v8.0.0 as an experimental feature and becomes generally available (GA) in v8.2.0. Disk spill for the TopN operator is introduced in v8.3.0. | ||
| - You can control whether to enable the parallel HashAgg algorithm that supports disk spill using the [`tidb_enable_parallel_hashagg_spill`](/system-variables.md#tidb_enable_parallel_hashagg_spill-new-in-v800) system variable. This variable will be deprecated in a future release. | ||
| - When the SQL executions containing Sort, MergeJoin, HashJoin, HashAgg, or TopN cause OOM, TiDB triggers disk spill by default. | ||
| - When the SQL executions containing Sort, MergeJoin, HashJoin, HashAgg, or TopN cause OOM, TiDB triggers disk spill by default. | ||
|
|
||
| > **Note:** | ||
| > | ||
|
|
@@ -207,8 +221,6 @@ | |
| 9 rows in set (1 min 37.428 sec) | ||
| ``` | ||
|
|
||
| ## Others | ||
|
|
||
| ### Mitigate OOM issues by configuring `GOMEMLIMIT` | ||
|
|
||
| GO 1.19 introduces an environment variable [`GOMEMLIMIT`](https://pkg.go.dev/runtime@go1.19#hdr-Environment_Variables) to set the memory limit that triggers GC. | ||
|
|
@@ -226,3 +238,118 @@ | |
| - In TiDB v6.1.3, `GOMEMLIMIT` is set to 40000 MiB. It is found that the simulated workload runs stably for a long time, OOM does not occur in the TiDB server, and the maximum memory usage of the process is stable at around 40.8 GiB: | ||
|
|
||
|  | ||
|
|
||
| </CustomContent> | ||
|
|
||
| <CustomContent platform="tidb-cloud"> | ||
|
|
||
| ## Memory arbitrator mode | ||
|
|
||
| Before CLOUD.202609.1, the [memory control mechanism](#configure-the-memory-usage-threshold-of-a-tidb-server-instance) has the following issues: | ||
|
|
||
| - When the memory usage of a TiDB instance exceeds the limit, TiDB might randomly terminate running SQL statements. | ||
| - Memory resources follow a "use first and report later" mechanism, and memory usage is isolated across different SQL statements. As a result, TiDB cannot centrally schedule or control memory resources at the instance level. | ||
| - Under high memory pressure, the overhead of Go garbage collection (GC) increases significantly, and in severe cases might cause out of memory (OOM) issues. | ||
|
|
||
| Starting from CLOUD.202609.1, TiDB introduces memory arbitrator mode. This mode introduces a global memory arbitrator in each TiDB instance to centrally manage and schedule the memory resources of the instance from top to bottom, mitigating the preceding issues. | ||
|
|
||
| > **Warning:** | ||
| > | ||
| > This feature is experimental and is not recommended for use in the production environment. This feature might be changed or removed without prior notice. If you find a bug, report it by submitting an [issue](https://github.com/pingcap/tidb/issues) on GitHub. | ||
|
|
||
| You can enable memory arbitrator mode using the [`tidb_mem_arbitrator_mode`](/system-variables.md#tidb_mem_arbitrator_mode-new-in-cloud2026091) system variable or the `instance.tidb_mem_arbitrator_mode` parameter in the TiDB configuration file. | ||
|
|
||
| - `disable`: disables memory arbitrator mode | ||
|
solotzg marked this conversation as resolved.
Resolved
|
||
|
|
||
| - `standard` or `priority`: enables memory arbitrator mode. After it is enabled: | ||
| - Memory resources are used with a subscribe-before-allocation mechanism and are uniformly scheduled by the memory arbitrator in each TiDB instance. | ||
| - The overall memory usage of the TiDB instance is expected not to exceed the limit of [`tidb_server_memory_limit`](/system-variables.md#tidb_server_memory_limit-new-in-v640), and the [alarm for high memory usage](/configure-memory-usage.md#trigger-the-alarm-of-excessive-memory-usage) no longer takes effect. | ||
| - The behavior of the following system variables remains effective: | ||
| - [`tidb_mem_oom_action`](/system-variables.md#tidb_mem_oom_action-new-in-v610) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tidb_mem_oom_action is read-only for TiDB X
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The default behavior of |
||
| - [`tidb_mem_quota_query`](/system-variables.md#tidb_mem_quota_query) | ||
| - [`max_execution_time`](/system-variables.md#max_execution_time) | ||
|
|
||
| When memory resources are insufficient or there is an OOM risk, the arbitrator can reclaim memory resources by terminating SQL statements, but it does not terminate `DDL`, `DCL`, or `TCL` SQL statements. Terminated SQL returns error code `8180` to the client. The error format is: `Query execution was stopped by the global memory arbitrator [reason=?, path=?] [conn=?]`. The related fields are explained as follows: | ||
|
|
||
| - `conn`: the connection (session) ID | ||
| - `reason`: the specific reason why the SQL is terminated | ||
| - `path`: the stage at which the SQL is terminated (if the error does not contain the `path` field, the SQL is terminated during the execution stage) | ||
| - `ParseSQL`: parsing | ||
| - `CompilePlan`: compiling the execution plan | ||
|
|
||
| Memory arbitrator mode pools the arbitrable memory resources in each TiDB instance. The memory resources corresponding to `tidb_server_memory_limit` are divided into the following four categories, which can be viewed through the `Mem Quota Stats` monitoring metric: | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Is the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| - `allocated`: allocated memory quota, including but not limited to the memory pool to which the SQL belongs and the memory pools of various public modules | ||
| - `available`: allocatable memory quota | ||
| - `buffer`: memory quota of the buffer | ||
| - `out-of-control`: memory quota not controlled by the arbitrator, including but not limited to: | ||
| - unsafe or disabled memory | ||
| - memory waiting for garbage collection | ||
|
solotzg marked this conversation as resolved.
Resolved
|
||
| - memory used by the Go runtime | ||
| - other untracked memory usage | ||
|
|
||
| `out-of-control` is a risk metric that requires special attention. It is dynamically calculated by the arbitrator, with a minimum value of `tidb_server_memory_limit - tidb_mem_arbitrator_soft_limit`. The arbitrator quantifies this type of risky memory quota as accurately as possible and reserves space for it during memory allocation, thereby reducing the risk of OOM in the TiDB instance. | ||
|
solotzg marked this conversation as resolved.
Resolved
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Is this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is |
||
|
|
||
| ### `standard` mode | ||
|
|
||
| In `standard` mode, during SQL execution, SQL dynamically subscribes to memory resources from the arbitrator and blocks to wait when resources are insufficient. The arbitrator processes subscription requests on a first in, first out (FIFO) basis. | ||
|
|
||
| - When parsing SQL or compiling the execution plan, the required memory quota is estimated by TiDB and is proportional to the number of SQL keywords. | ||
| - If global memory resources are insufficient, the arbitrator fails the subscription request and terminates the SQL. The `reason` field in the returned error is `CANCEL(out-of-quota & standard-mode)`. | ||
|
|
||
| ### `priority` mode | ||
|
|
||
| In `priority` mode, during SQL execution, SQL dynamically subscribes to memory resources from the arbitrator and blocks to wait when resources are insufficient. The arbitrator processes subscription requests according to the [resource group priority](/information-schema/information-schema-resource-groups.md) (`LOW | MEDIUM | HIGH`) to which the SQL belongs. | ||
|
|
||
| - When parsing SQL or compiling the execution plan, the required memory quota is estimated by TiDB and is proportional to the number of SQL keywords. Unlike `standard` mode, in `priority` mode, unless there is an `OOM` risk, a failed subscription does not immediately terminate the SQL. | ||
| - The arbitrator processes subscription requests in descending order of priority. Requests with the same priority are queued in the order they are initiated. | ||
| - When global memory resources are insufficient: | ||
| - The arbitrator terminates lower-priority SQL in order (from low to high priority, and from large to small memory quota usage) to reclaim resources for higher-priority SQL. The `reason` field in the returned error is `CANCEL(out-of-quota & priority-mode)`. | ||
| - If there is no terminable SQL, the subscription request continues to wait until an existing SQL finishes execution and releases resources. | ||
|
|
||
| If you want SQL to avoid the latency overhead caused by waiting for memory resources, you can set the [`tidb_mem_arbitrator_wait_averse`](/system-variables.md#tidb_mem_arbitrator_wait_averse-new-in-cloud2026091) system variable to `1`. | ||
|
|
||
| - The subscription requests of the related SQL are automatically bound to priority `HIGH`. | ||
| - When global memory resources are insufficient, the arbitrator directly terminates the related SQL, and the `reason` field in the returned error is `CANCEL(out-of-quota & wait-averse)`. | ||
|
solotzg marked this conversation as resolved.
Resolved
|
||
|
|
||
| ### Memory risk control | ||
|
|
||
| When the memory usage of a TiDB instance reaches the `95%` threshold of `tidb_server_memory_limit`, the arbitrator starts handling memory risks. If the actual memory usage cannot be reduced to a safe level in the short term or the actual memory release rate is too low, the TiDB instance faces the `OOM` risk. The arbitrator forcibly terminates SQL in order (from low to high priority, and from large to small memory quota usage), and the `reason` field in the returned error is `KILL(out-of-memory)`. | ||
|
|
||
| ### Manually ensuring memory safety | ||
|
|
||
| You can set the upper limit of the arbitrator's memory quota in a TiDB instance using the [`tidb_mem_arbitrator_soft_limit`](/system-variables.md#tidb_mem_arbitrator_soft_limit-new-in-cloud2026091) system variable or the `instance.tidb_mem_arbitrator_soft_limit` parameter in the TiDB configuration file. The smaller the upper limit, the safer the global memory, but the lower the memory resource utilization. You can use this variable to manually and quickly converge memory risks. | ||
|
|
||
| TiDB internally caches the historical maximum memory usage of some SQL statements and pre-subscribes sufficient memory quota before the next execution of the SQL. If it is known that a SQL statement has a large amount of uncontrolled memory usage, you can use the [`tidb_mem_arbitrator_query_reserved`](/system-variables.md#tidb_mem_arbitrator_query_reserved-new-in-cloud2026091) system variable to specify the quota subscribed by the SQL. The larger the value, the safer the global memory, but the lower the memory resource utilization. Pre-subscribing sufficient or excess quota can effectively ensure the isolation of memory resources for SQL. | ||
|
Check warning on line 323 in configure-memory-usage.md
|
||
|
|
||
| ### Monitoring and observability metrics | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Are these metrics available for TiDB X?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I've add relevant grafana to repo tidbcloud/clinic-grafana. |
||
|
|
||
| The `TiDB-Cluster / Memory Arbitrator` panel is added to `Grafana` monitoring, containing the following metrics: | ||
|
|
||
| - `Work Mode`: the memory management mode of the TiDB instance | ||
| - `Arbitration Exec`: statistics on the arbitrator processing various requests | ||
| - `Events`: statistics on various events in arbitrator mode. Pay special attention to `mem-risk` (memory risk) and `oom-risk` (`OOM` risk) | ||
| - `Mem Quota Stats`: usage of various memory quotas. Pay special attention to `out-of-control`, `wait-alloc` (total memory quota waiting for allocation), and `mem-inuse` (actual memory usage) | ||
| - `Mem Quota Arbitration`: processing time of memory resource subscription requests | ||
| - `Mem Pool Stats`: the number of various memory pools | ||
| - `Runtime Mem Pressure`: memory pressure value, that is, the ratio of actual memory usage to allocated memory quota | ||
| - `Waiting Tasks`: the number of various tasks queued and waiting for memory allocation | ||
|
|
||
| [`SLOW_QUERY`](/information-schema/information-schema-slow-query.md) adds the `Mem_arbitration` field, which indicates the total time that SQL waits for memory resources. The `Mem Arbitration` column on the [TiDB Dashboard slow query page](/dashboard/dashboard-slow-query.md) also displays this information. | ||
|
|
||
| [`PROCESSLIST`](/information-schema/information-schema-processlist.md) adds the following fields: | ||
|
|
||
| - `MEM_ARBITRATION`: the total time that SQL has waited for memory resources so far | ||
| - `MEM_WAIT_ARBITRATE_START`: the start time of the current memory resource subscription request. If there is currently no waiting subscription request, the value is NULL | ||
| - `MEM_WAIT_ARBITRATE_BYTES`: the requested bytes of the current memory resource subscription request. If there is currently no waiting subscription request, the value is NULL | ||
|
|
||
| [`Expensive query`](/identify-expensive-queries.md) adds the `mem_arbitration` field to record the time that SQL waits for memory resources and the information of the current subscription request, for example: | ||
|
|
||
| - `cost_time 2.1s, wait_start 1970-01-02 10:17:36.789 UTC, wait_bytes 123456789123 Bytes (115.0 GB)` | ||
|
|
||
| [`STATEMENTS_SUMMARY`](/statement-summary-tables.md): the `statements_summary`, `statements_summary_history`, `cluster_statements_summary`, and `cluster_statements_summary_history` tables add the following fields. The `Mean Mem Arbitration` column on the [TiDB Dashboard SQL statement analysis execution details page](/dashboard/dashboard-statement-list.md) also displays this information: | ||
|
|
||
| - `AVG_MEM_ARBITRATION`: the average time that SQL waits for memory resources | ||
| - `MAX_MEM_ARBITRATION`: the maximum time that SQL waits for memory resources | ||
|
|
||
| </CustomContent> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the new compound modifiers.
LanguageTool flags
out of memoryandfirst in, first outin the added prose. Use the hyphenated forms.Suggested replacement
Also applies to: 277-277
LanguageTool
[grammar] ~234-~234: Use a hyphen to join words.
Context: ...tly, and in severe cases might cause out of memory (OOM) issues. Starting from v9.0...
(QB_NEW_EN_HYPHEN)
Sources: Path instructions, Linters/SAST tools