Skip to content
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

Make plan cache compatible with stale read and history read #22470

Closed
djshow832 opened this issue Jan 21, 2021 · 2 comments
Closed

Make plan cache compatible with stale read and history read #22470

djshow832 opened this issue Jan 21, 2021 · 2 comments
Assignees
Labels
epic/plan-cache sig/execution SIG execution sig/transaction SIG:Transaction type/enhancement The issue or PR belongs to an enhancement.

Comments

@djshow832
Copy link
Contributor

djshow832 commented Jan 21, 2021

Background

This is a subtask of #21094.

The prepared plan cache caches the physical plan for each cache key.

The cache key depends on:

  • The prepared statement ID
  • The connection ID and current database name
  • The table schema, a.k.a. the schema version
  • The snapshot timestamp, if it's a history read
  • Other variables or configurations, such as SQL mode, timezone

Among these variables, the schema version and snapshot timestamp of stale reads need to be handled carefully.

Solution

Let's discuss these two variables separately.

Snapshot timestamp

Actually, the snapshot timestamp doesn't affect the physical plan, so it can be removed. I have submitted issue #22497.

For now, we just ignore this variable and keep it untouched.

Schema version

schemaVersion in the cache key is the schema version when the PREPARE statement is parsed, rather than the schema version when the EXECUTE statement is executed or the schema version recorded in the prepared AST.

When querying the plan cache, callers construct a cache key with the schema version recorded in the prepared AST. After schema changes, the schema version in the prepared AST may also change, so the plans built with older schema versions won't be applied.

When executing the EXECUTE statement, the executor ensures that it always uses the schema version with which the transaction begins. However, this schema version may be too new for stale reads. The easiest solution is forbidding any related schema changes between the transaction start ts and the time when the transaction really begins. This solution is described in #22427.

To summarize, the schemaVersion can also stay what it is for stale reads.

@djshow832 djshow832 added type/enhancement The issue or PR belongs to an enhancement. sig/execution SIG execution labels Jan 21, 2021
@djshow832 djshow832 added the sig/transaction SIG:Transaction label Jan 23, 2021
@Yisaer
Copy link
Contributor

Yisaer commented Jan 25, 2021

/assign

@Yisaer
Copy link
Contributor

Yisaer commented Aug 15, 2022

Currently we forbid stale read with plan cache.

@Yisaer Yisaer closed this as completed Aug 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
epic/plan-cache sig/execution SIG execution sig/transaction SIG:Transaction type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

No branches or pull requests

3 participants