Skip to content

planner: update doc for Instance plan cache #21017

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

Open
wants to merge 23 commits into
base: master
Choose a base branch
from

Conversation

qw4990
Copy link
Contributor

@qw4990 qw4990 commented May 22, 2025

First-time contributors' checklist

What is changed, added or deleted? (Required)

Which TiDB version(s) do your changes apply to? (Required)

Tips for choosing the affected version(s):

By default, CHOOSE MASTER ONLY so your changes will be applied to the next TiDB major or minor releases. If your PR involves a product feature behavior change or a compatibility change, CHOOSE THE AFFECTED RELEASE BRANCH(ES) AND MASTER.

For details, see tips for choosing the affected versions.

  • master (the latest development version)
  • v9.0 (TiDB 9.0 versions)
  • v8.5 (TiDB 8.5 versions)
  • v8.4 (TiDB 8.4 versions)
  • v8.3 (TiDB 8.3 versions)
  • v8.1 (TiDB 8.1 versions)
  • v7.5 (TiDB 7.5 versions)
  • v7.1 (TiDB 7.1 versions)
  • v6.5 (TiDB 6.5 versions)
  • v6.1 (TiDB 6.1 versions)
  • v5.4 (TiDB 5.4 versions)

What is the related PR or file link(s)?

  • This PR is translated from:
  • Other reference link(s):

Do your changes match any of the following descriptions?

  • Delete files
  • Change aliases
  • Need modification after applied to another branch
  • Might cause conflicts after applied to another branch

@qw4990 qw4990 requested a review from Oreoxmt May 22, 2025 22:05
@ti-chi-bot ti-chi-bot bot added missing-translation-status This PR does not have translation status info. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 22, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @qw4990, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

Gemini-code-assist here, providing a summary of this pull request. This PR focuses on updating the documentation for TiDB's prepared plan cache. The primary goal is to introduce and explain the newly supported instance-level plan cache feature, differentiating it from the existing session-level cache. The changes cover how to enable and configure the instance cache, how to monitor its contents using new system tables, how to manage its memory usage, and clarifies the current capabilities of the ADMIN FLUSH PLAN_CACHE statement regarding instance cache.

Highlights

  • Introduction of Instance Plan Cache: Adds a new section explaining the concept of instance-level plan cache, which is shared across all sessions in an instance, contrasting it with the session-level cache.
  • Enabling Instance Plan Cache: Documents the tidb_enable_instance_plan_cache system variable (introduced in v8.4.0) used to switch between session and instance plan caching.
  • Viewing Instance Plan Cache Contents: Introduces the information_schema.tidb_plan_cache and information_schema.cluster_tidb_plan_cache system tables (available after v8.5.0) for inspecting entries in the instance-level plan cache, including a SQL example.
  • Instance Plan Cache Memory Management: Adds documentation for the tidb_instance_plan_cache_max_size and tidb_instance_plan_cache_reserved_percentage system variables used to control the memory limit and purge behavior of the instance plan cache.
  • Clearing Plan Cache: Adds a note under the 'Clear execution plan cache' section clarifying that the ADMIN FLUSH PLAN_CACHE statement currently only supports clearing the session-level cache.
  • Platform-Specific Notes: Includes <CustomContent> blocks to add notes specific to TiDB and TiDB Cloud regarding potential plan cache misses due to memory limits.

Changelog

  • sql-prepared-plan-cache.md
    • Added a new section 'Session and Instance Level Prepared Plan Cache' starting around line 20.
    • Documented the tidb_enable_instance_plan_cache system variable for enabling instance-level cache.
    • Included a SQL example demonstrating plan sharing across sessions with instance-level cache enabled.
    • Introduced and provided examples for the information_schema.tidb_plan_cache and information_schema.cluster_tidb_plan_cache system tables for viewing instance cache entries (after v8.5.0).
    • Added platform-specific notes about plan cache misses under the 'Plan Cache Memory Usage' section (around line 279).
    • Added a new subheading 'Session Level Plan Cache Memory Management' (around line 291).
    • Added a new subheading 'Instance Level Plan Cache Memory Management' (around line 316).
    • Documented the tidb_instance_plan_cache_max_size and tidb_instance_plan_cache_reserved_percentage system variables.
    • Added a note under 'Clear execution plan cache' (around line 324) about the current limitation of ADMIN FLUSH PLAN_CACHE for instance cache.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request provides a valuable update to the documentation for the Instance Plan Cache feature. The new sections on session vs. instance level caches, viewing cache entries, and memory management are well-structured and informative. The examples provided are clear and helpful for users to understand the functionality.

My review focuses on enhancing clarity, conciseness, and adherence to the TiDB documentation style guide. The suggestions aim to make the text even more user-friendly and precise. Overall, the changes significantly improve the documentation for this feature.

Summary of Findings

  • Clarity and Phrasing: Several sentences were updated for better clarity, flow, and formality, aligning with the technical documentation style.
  • Punctuation and Formatting: Minor punctuation (e.g., extra space before a period) and formatting of calculations were adjusted.
  • Terminology Consistency: Ensured consistent use of terms like "clearing" plan cache.
  • Tense Consistency: Adjusted verb tenses to prefer present tense for describing system behavior, as per the style guide.

Merge Readiness

The pull request is in good shape and the documentation updates are valuable. The suggested changes are minor and aim to further improve clarity and adherence to the style guide. As per the documentation style guide, I am providing these comments for consideration and not requesting changes to block the merge. I am not authorized to approve pull requests; please ensure other reviewers approve these changes before merging.

qw4990 and others added 7 commits May 22, 2025 15:08
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@Oreoxmt Oreoxmt self-assigned this May 23, 2025
@Oreoxmt Oreoxmt added translation/doing This PR's assignee is translating this PR. v9.0-beta.1 This PR/issue applies to TiDB v9.0-beta.1. labels May 23, 2025
@ti-chi-bot ti-chi-bot bot removed the missing-translation-status This PR does not have translation status info. label May 23, 2025
@qw4990 qw4990 requested a review from songrijie May 23, 2025 02:04
@qw4990
Copy link
Contributor Author

qw4990 commented May 23, 2025

Not a big update, please help review @songrijie @Oreoxmt

Copy link

ti-chi-bot bot commented May 23, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from oreoxmt. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link
Member

@time-and-fate time-and-fate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we will have another doc PR when it's enabled by default and admin flush global plan_cache is supported, right?


</CustomContent>
TiDB purges the instance-level plan cache periodically. You can control the amount of memory purged each time by using the [`tidb_instance_plan_cache_reserved_percentage`](/system-variables.md#tidb_instance_plan_cache_reserved_percentage-new-in-v840) system variable. For example, if the default percentage is `0.1` and the maximum cache size is `200MiB`, TiDB purges `200 * 0.1 = 20MiB` of memory each time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this description is different from the description in the system variables.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I previously created an illustration for this. Is it better to include it?
image

Comment on lines +281 to +293
<CustomContent platform="tidb">

Due to memory limits, plan cache entries might be missed. You can check this status by viewing the [`Plan Cache Miss OPS` metric](/grafana-tidb-dashboard.md) in the Grafana dashboard.

</CustomContent>

<CustomContent platform="tidb-cloud">

Due to memory limits, plan cache entries might be missed.

</CustomContent>

### Memory management of session-level plan cache
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this change, the tag is misplaced.
It's already wrapped in a <CustomContent platform="tidb"> tag here.

@qw4990
Copy link
Contributor Author

qw4990 commented May 23, 2025

So we will have another doc PR when it's enabled by default and admin flush global plan_cache is supported, right?

Yes, but it's the next version, not v9.0.

@@ -17,6 +17,78 @@ When the execution plan cache is enabled, in the first execution every `Prepare`

TiDB also supports execution plan caching for some non-`PREPARE` statements, similar to the `Prepare`/`Execute` statements. For more details, refer to [Non-prepared plan cache](/sql-non-prepared-plan-cache.md).

## Session-level and instance-level Prepared Plan Cache

TiDB supports both session-level plan cache and instance-level plan cache. Session-level plan cache maintains a separate cache for each session, while instance-level plan cache maintains a single shared cache across all sessions in an instance.
Copy link
Contributor

@songrijie songrijie May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shall recommend customers to select instances plan cache, as it caches more plans with given memory size. Otherwise, customers would ask which one should be chosen.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any scenario we recommend session plan cache?


TiDB supports both session-level plan cache and instance-level plan cache. Session-level plan cache maintains a separate cache for each session, while instance-level plan cache maintains a single shared cache across all sessions in an instance.

To enable or disable instance-level plan cache, you can use the [`tidb_enable_instance_plan_cache`](/system-variables.md#tidb_enable_instance_plan_cache-new-in-v840) system variable. When you set this variable to `ON`, instance-level plan cache is used. Otherwise, session-level plan cache is used.
Copy link
Contributor

@songrijie songrijie May 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial size of instance plan cache is small. We'd better mention setting proper cache size.

Comment on lines +59 to +60
* `information_schema.tidb_plan_cache`: shows entries of instance plan cache in the current TiDB instance.
* `information_schema.cluster_tidb_plan_cache`: shows entries of instance plan cache across all available TiDB instances.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we include a page to describe the view under https://docs.pingcap.com/zh/tidb/stable/information-schema/ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files. translation/doing This PR's assignee is translating this PR. v9.0-beta.1 This PR/issue applies to TiDB v9.0-beta.1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants