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

ddl: make "add foreign key" step in multiple schema versions #38679

Merged
merged 5 commits into from
Oct 28, 2022

Conversation

crazycs520
Copy link
Contributor

Signed-off-by: crazycs520 crazycs520@gmail.com

What problem does this PR solve?

Issue Number: close #38678

At the same point in time, there may be two versions of the schema in the TiDB cluster, so we can't create foreign key in one schema version, since this may break foreign key constraint, such as delete reference table
without foreign key constrain check in child table.

-- In TiDB-1 and Schema Version is 1
insert into t_has_foreign_key values (1, 1);
-- In TiDB-0 and  Schema Version is 0
delete from t_reference where id = 1; --Since doesn't know foreign key information in old version, so doesn't do foreign key constrain check.

So The DDL owner handle add foreign key constrain step should be:

  1. None -> Write Only: add foreign key constrain which state is write-only into table.
  2. Write Only - Write Reorg: check all row in the table whether has related foreign key exists in reference table, we can use following SQL to check:
    select count(*) from t2 where t2.a not in (select id from t1);
    The expected result is 0, otherwise, an error is returned and cancel the ddl job.
  3. Write Reorg -> Public: update the foreign key constraint state to public.

A problem is, How the DML treat the foreign key with on delete/update cascade behaviour which state is non-public?
Here is an example:

create table t1 (id int key,a int, index(a));
create table t2 (id int key,a int, index(a));
insert into t1 values (1,1);
insert into t2 values (1,1);
alter  table t2 add constraint fk_1 foreign key (a) references t1(id) ON DELETE CASCADE;

The schema change of foreign key fk_1 is from None -> Write-Only -> Write-Reorg -> Public
When the foreign key fk_1 in Write-Only state, a DML request has come to be processed:

delete from t1 where id = 1;

Then, TiDB shouldn't do cascade delete for foreign key fk_1 in state Write-Only, since the Add Foreign Key DDL job maybe
failed in Write-Reorg state and rollback the DDL job. But it is hard to rollback the cascade deleted executed before.

So, when execute DML with non-publick foreign key, TiDB will do foreign key constraint check instead of foreign key cascade behaviour.

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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.

None

Signed-off-by: crazycs520 <crazycs520@gmail.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Oct 27, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • lcwangchao
  • tangenta

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Oct 27, 2022
ddl/foreign_key.go Outdated Show resolved Hide resolved
@crazycs520 crazycs520 changed the title ddl: fix issue of add foreign key doesn't have multi-schema change ddl: fix issue of add foreign key doesn't have multiple schema versions change Oct 27, 2022
@tangenta tangenta changed the title ddl: fix issue of add foreign key doesn't have multiple schema versions change ddl: make "add foreign key" step in multiple schema versions Oct 27, 2022
Signed-off-by: crazycs520 <crazycs520@gmail.com>
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Oct 28, 2022
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Oct 28, 2022
@crazycs520
Copy link
Contributor Author

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 3c3d2c0

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Oct 28, 2022
@ti-chi-bot
Copy link
Member

@crazycs520: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

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 ti-community-infra/tichi repository.

@crazycs520
Copy link
Contributor Author

/merge

@crazycs520
Copy link
Contributor Author

/run-all-tests

@crazycs520
Copy link
Contributor Author

/merge

@ti-chi-bot ti-chi-bot merged commit e8e37b3 into pingcap:master Oct 28, 2022
@crazycs520 crazycs520 deleted the ddl--add-fk-state branch October 28, 2022 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add foreign key need multi-schema change
4 participants