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

statistics: avoid deadlock when create/drop extended stats and analyze at the same time #30566

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions statistics/handle/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,15 @@ func (h *Handle) InsertExtendedStats(statsName string, colIDs []int64, tp int, t
return errors.Errorf("extended statistics '%s' with same type on same columns already exists", statsName)
}
}
txn, err := h.mu.ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
version := txn.StartTS()
// Bump version in `mysql.stats_meta` to trigger stats cache refresh.
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID); err != nil {
return err
}
// Remove the existing 'deleted' records.
if _, err = exec.ExecuteInternal(ctx, "DELETE FROM mysql.stats_extended WHERE name = %? and table_id = %?", statsName, tableID); err != nil {
return err
Expand All @@ -1479,17 +1488,10 @@ func (h *Handle) InsertExtendedStats(statsName string, colIDs []int64, tp int, t
// the record from the table, tidb-b should delete the cached item synchronously. While for tidb-c, it has to wait for
// next `Update()` to remove the cached item then.
h.removeExtendedStatsItem(tableID, statsName)
txn, err := h.mu.ctx.Txn(true)
if err != nil {
return errors.Trace(err)
}
version := txn.StartTS()
const sql = "INSERT INTO mysql.stats_extended(name, type, table_id, column_ids, version, status) VALUES (%?, %?, %?, %?, %?, %?)"
if _, err = exec.ExecuteInternal(ctx, sql, statsName, tp, tableID, strColIDs, version, StatsStatusInited); err != nil {
return err
}
// Bump version in `mysql.stats_meta` to trigger stats cache refresh.
_, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID)
return
}

Expand Down Expand Up @@ -1529,10 +1531,10 @@ func (h *Handle) MarkExtendedStatsDeleted(statsName string, tableID int64, ifExi
return errors.Trace(err)
}
version := txn.StartTS()
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_extended SET version = %?, status = %? WHERE name = %? and table_id = %?", version, StatsStatusDeleted, statsName, tableID); err != nil {
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID); err != nil {
return err
}
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_meta SET version = %? WHERE table_id = %?", version, tableID); err != nil {
if _, err = exec.ExecuteInternal(ctx, "UPDATE mysql.stats_extended SET version = %?, status = %? WHERE name = %? and table_id = %?", version, StatsStatusDeleted, statsName, tableID); err != nil {
return err
}
return nil
Expand Down