Repository: pingcap/tidb
File: pkg/sessionctx/stmtctx/stmtctx.go (Go)

Code before:
	MemTracker   *memory.Tracker
	MemSensitive bool // whether this statement is memory sensitive
	DiskTracker  *disk.Tracker
	// per statement resource group name
	// hint /* +ResourceGroup(name) */ can change the statement group name
	ResourceGroupName   string
	RunawayChecker      resourcegroup.RunawayChecker
	IsTiKV              atomic2.Bool
	IsTiFlash           atomic2.Bool
	RuntimeStatsColl    *execdetails.RuntimeStatsColl
	IndexUsageCollector *indexusage.StmtIndexUsageCollector
	TableIDs            []int64
	IndexNames          []string
	StmtType            string
	OriginalSQL         string
	digestMemo          struct {
		sync.Once
		normalized string
		digest     *parser.Digest
	}
	// BindSQL used to construct the key for plan cache. It records the binding used by the stmt.
	// If the binding is not used by the stmt, the value is empty
	BindSQL string

	// MatchSQLBindingCache is to cache the bindinfo to avoid getting bindinfo from bind cache again.
	MatchSQLBindingCache map[ast.StmtNode]any

	// ExecRetryCount records the number of retries for executing the statement.
	// It is set after ExecStmt execution and currently only used in the Slow Log phase
	// after LogSlowQuery is called.
	ExecRetryCount uint64
	// ExecSuccess indicates whether the statement execution succeeded.
	// It is set after ExecStmt execution and currently only used in the Slow Log phase
	// after LogSlowQuery is called.
	ExecSuccess bool

	// The several fields below are mainly for some diagnostic features, like stmt summary and slow query.
	// We cache the values here to avoid calculating them multiple times.
	// Note:
	//   Avoid accessing these fields directly, use their Setter/Getter methods instead.
	//   Other fields should be the zero value or be consistent with the plan field.
	// TODO: more clearly distinguish between the value is empty and the value has not been set
	planNormalized string
	planDigest     *parser.Digest
	encodedPlan    string
	planHint       string
	planHintSet    bool
	binaryPlan     string
	// indexForce is set if any table in the query has a force or use index applied
	indexForce bool
	// To avoid cycle import, we use interface{} for the following two fields.

Review comment (performance):
It's dangerous to add a map here, we might forget to clear / delete this map. For example, after executing `deallcoate stmt`, we should delete the corresponding key from this map.
Maybe we should avoid using map here, just use a field like `PlanCacheBindingInfo` to record the current SQL's binding info, which should be enough and safer.

Code after:
	MemTracker   *memory.Tracker
	MemSensitive bool // whether this statement is memory sensitive
	DiskTracker  *disk.Tracker
	// per statement resource group name
	// hint /* +ResourceGroup(name) */ can change the statement group name
	ResourceGroupName   string
	RunawayChecker      resourcegroup.RunawayChecker
	IsTiKV              atomic2.Bool
	IsTiFlash           atomic2.Bool
	RuntimeStatsColl    *execdetails.RuntimeStatsColl
	IndexUsageCollector *indexusage.StmtIndexUsageCollector
	TableIDs            []int64
	IndexNames          []string
	StmtType            string
	OriginalSQL         string
	digestMemo          struct {
		sync.Once
		normalized string
		digest     *parser.Digest
	}
	// BindSQL used to construct the key for plan cache. It records the binding used by the stmt.
	// If the binding is not used by the stmt, the value is empty
	BindSQL string

	// MatchSQLBindingCacheKey is the AST node used to match the sql binding. It is the key of the MatchSQLBindingCache.
	MatchSQLBindingCacheKey ast.StmtNode
	// MatchSQLBindingCache is to cache the bindinfo to avoid getting bindinfo from bind cache again.
	MatchSQLBindingCache any

	// ExecRetryCount records the number of retries for executing the statement.
	// It is set after ExecStmt execution and currently only used in the Slow Log phase
	// after LogSlowQuery is called.
	ExecRetryCount uint64
	// ExecSuccess indicates whether the statement execution succeeded.
	// It is set after ExecStmt execution and currently only used in the Slow Log phase
	// after LogSlowQuery is called.
	ExecSuccess bool

	// The several fields below are mainly for some diagnostic features, like stmt summary and slow query.
	// We cache the values here to avoid calculating them multiple times.
	// Note:
	//   Avoid accessing these fields directly, use their Setter/Getter methods instead.
	//   Other fields should be the zero value or be consistent with the plan field.
	// TODO: more clearly distinguish between the value is empty and the value has not been set
	planNormalized string
	planDigest     *parser.Digest
	encodedPlan    string
	planHint       string
	planHintSet    bool
	binaryPlan     string
	// indexForce is set if any table in the query has a force or use index applied