*: fix a bug that update statement uses point get and update plan with different tblInfo (#54183)
This is an automated cherry-pick of #54183
What problem does this PR solve?
Issue Number: close https://github.com/pingcap/tidb/issues/53634
Problem Summary:
Case
Init SQLs:
CREATE TABLE stock ( a int NOT NULL, b char(30) NOT NULL, c int, d char(64), PRIMARY KEY(a,b)) ;
insert into stock values(1, 'a', 11, 'x'), (2, 'b', 22, 'y');
alter table stock add column cct_1 int default 10;
alter table stock modify cct_1 json;
alter table stock add column adc_1 smallint;
- Step1. using conn1 prepare statements:
begin;
SELECT a, c, d from stock where (a, b) IN ((?, ?),(?, ?)) FOR UPDATE;
UPDATE stock SET c = ? WHERE a= ? AND b = 'a';
UPDATE stock SET c = ?, d = 'z' WHERE a= ? AND b = 'b';
commit;
run a statement:
begin;
- Step2. using conn2 do DDL:
alter table stock drop column cct_1;
- Step3. using conn1 When the DDL is in Write-Only state exec statements:
SELECT a, c, d from stock where (a, b) IN ((?, ?),(?, ?)) FOR UPDATE;
UPDATE stock SET c = ? WHERE a= ? AND b = 'a';
UPDATE stock SET c = ?, d = 'z' WHERE a= ? AND b = 'b';
commit;
-
Step4. using conn1 Check the result
select * from stock; -
Statement execution order table
| conn1 | conn2 |
|---|---|
| step1. prepare stmts | |
| step1. begin; | |
| step2. alter table stock drop column cct_1; (state public -> write-only) | |
| step3. exec stmts | write-only |
| step2. alter table stock drop column cct_1; (finish) | |
| step4. select * from stock; |
Conclusion
The update statement in step3 uses point get and update plan, but the tblInfo used by the two plans is inconsistent, resulting in incorrect data in real storage.
-
After executing step3. select statement, the stock is locked in
GetRelatedTableForMDL, sostmt.tbls[i].Meta().Revision != newTbl.Meta().Revisionis false. It meansschemaNotMatchis false. So we needn't toPreprocess. https://github.com/pingcap/tidb/blob/d5b89f8bccdf13cf664ba8bb65a87b7342676d49/pkg/planner/core/plan_cache.go#L114-L119 -
Step3. update statement using
tblName.TableInfo(get it when preparing statements,cct_1is public) innewPointGetPlan. https://github.com/pingcap/tidb/blob/d5b89f8bccdf13cf664ba8bb65a87b7342676d49/pkg/planner/core/point_get_plan.go#L1317 -
Step3. update statement using
tgets fromis.TableByID(tbl.ID)(cct_1is write-only) https://github.com/pingcap/tidb/blob/d5b89f8bccdf13cf664ba8bb65a87b7342676d49/pkg/planner/core/point_get_plan.go#L1945-L1950
What changed and how does it work?
Add the Revision field comparison of tbl(get from txn infoschema) and newTbl to confirm whether reprocess is required.
Check List
Tests
- [x] Unit test
- [ ] Integration test
- [ ] Manual test (add detailed scripts or steps below)
- [ ] No need to test
- [ ] I checked and no code files have been changed.
Side effects
- [ ] Performance regression: Consumes more CPU
- [ ] Performance regression: Consumes more Memory
- [ ] Breaking backward compatibility
Documentation
- [ ] Affects user behaviors
- [ ] Contains syntax changes
- [ ] Contains variable changes
- [ ] Contains experimental features
- [ ] Changes MySQL compatibility
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.
Fix an issue where improper use of metadata locks in some scenarios could cause abnormal data to be written when using the plan cache.
/retest
[APPROVALNOTIFIER] This PR is APPROVED
This pull-request has been approved by: Defined2014, qw4990
The full list of commands accepted by this bot can be found here.
The pull request process is described here
- ~~OWNERS~~ [Defined2014,qw4990]
Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment
[LGTM Timeline notifier]
Timeline:
2024-08-29 04:17:54.571332515 +0000 UTC m=+1016669.705782621: :ballot_box_with_check: agreed by Defined2014.2024-08-29 06:12:12.241129933 +0000 UTC m=+1023527.375580051: :ballot_box_with_check: agreed by qw4990.
/test unit-test
@zimulala: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
/test unit-test
@zimulala: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
/test unit-test
@zimulala: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
/retest
/retest
/retest
/retest
/retest
/retest
/retest
/retest
/test unit-test
@zimulala: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
/test unit-test
@zimulala: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
/retest
/test unit-test
@JaySon-Huang: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
/test unit-test
@zimulala: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
/test unit-test
@zimulala: No presubmit jobs available for pingcap/[email protected]
In response to this:
/test unit-test
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.
Codecov Report
Attention: Patch coverage is 53.96825% with 29 lines in your changes missing coverage. Please review.
Please upload report for BASE (
release-6.5@4ffd88c). Learn more about missing BASE report.
Additional details and impacted files
@@ Coverage Diff @@
## release-6.5 #54259 +/- ##
================================================
Coverage ? 73.6304%
================================================
Files ? 1097
Lines ? 352812
Branches ? 0
================================================
Hits ? 259777
Misses ? 76330
Partials ? 16705
/cherry-pick release-6.5-20241009-v6.5.10