Make plan cache compatible with stale read and history read #22470
Labels
epic/plan-cache
sig/execution
SIG execution
sig/transaction
SIG:Transaction
type/enhancement
The issue or PR belongs to an enhancement.
Background
This is a subtask of #21094.
The prepared plan cache caches the physical plan for each cache key.
The cache key depends on:
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 thePREPARE
statement is parsed, rather than the schema version when theEXECUTE
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.The text was updated successfully, but these errors were encountered: