ddl: Add sql_require_primary_key sysvar#36146
Conversation
|
[REVIEW NOTIFICATION] This pull request has been approved by:
To complete the pull request process, please ask the reviewers in the list to review by filling The full list of commands accepted by this bot can be found here. DetailsReviewer can indicate their review by submitting an approval review. |
|
Code Coverage Details: https://codecov.io/github/pingcap/tidb/commit/8cf6e3611707ab50e65373cd62e6174250ae913b |
|
/run-unit-test |
I think a unique key instead of a PK should be allowed. However I don't think this is common or advisable, so this is a very minor thing. |
|
I don't think this should be allowed: |
|
/cc @dveeden |
|
Not sure if this should apply to temp tables: Another thing to check/test for is test for upgrades, internal tables, etc. as that caused some issues for MySQL when this was introduced. |
|
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_sql_require_primary_key It doesn't just work on create table |
These alter tables probably are not supported in TiDB (
Good point! I suggest we always enable
I think this might be a bug somewhere. The code that does |
|
Getting a linter error: You may also need to run |
Co-authored-by: Morgan Tocker <tocker@gmail.com>
MySQL 8 applies it to temporary tables as well.
|
|
I wasn't using the correct error code but a similar one. I have now added the actual error code. Have also added check to stop dropping Primary Key indexes (non-clustered one's) when this is set. For some cases TiDB is returning |
|
|
||
| ["schema:3750"] | ||
| error = ''' | ||
| Unable to create or change a table without a primary key, when the system variable 'sql_require_primary_key' is set. Add a primary key to the table or unset this variable to avoid this message. Note that tables without a primary key can cause performance problems in row-based replication, so please consult your DBA before changing this setting. |
There was a problem hiding this comment.
This is the same error mysql returns, copied from here
The "...row-based replication..." part is a bit weird in a TiDB context, we could change this to:
But keeping this message identical to MySQL has my personal preference. |
|
/cc @7yyo |
|
@7yyo: Thanks for your review. The bot only counts approvals from reviewers and higher roles in list, but you're still welcome to leave your comments. DetailsIn response to this:
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. |
|
Note that DM doesn't use real PKs either: This is with DM v5.4.2. I assume the unique keys with all columns defined as |
|
Hi @joycse06 , thanks for the contribution! If this variable is supposed to be used with TiCDC-based replication, should we consider unique key as well? I know that unique key is not included in |
We don't have a use-case beyond what the MySQL feature offers. It's not a typical use-case to have a table which doesn't have a primary key, but has a unique key. |
…rimary-key * upstream/master: (104 commits) br: fix compatibility issue with concurrent ddl (pingcap#36474) infoschema: fix PromQL for `tidb_distsql_copr_cache` (pingcap#36450) test: stabilize TestTopSQLCPUProfile (pingcap#36468) parser: add support of 'ADMIN SHOW DDL JOB QUERIES LIMIT m OFFSET n' transferring to AST (pingcap#36285) *: enable flaky test for all test (pingcap#36385) expression: fix return type of agg func `bit_or` when handling varbinary column (pingcap#36415) executor: fix aggregating enum zero value gets different results from mysql (pingcap#36208) server: skip check tiflash version (pingcap#36451) *: Minor update to SECURITY.md to improved clarity (pingcap#36346) table partition: add telemetry for partition table (pingcap#36204) ddl: invalid multiple MAXVALUE partitions (pingcap#36329) (pingcap#36345) planner: Fixed `Merge` hint in nested CTE (pingcap#36432) metric: impove concurrency ddl metrics (pingcap#36405) planner: add more test cases for leading outer join (pingcap#36409) ddl: only set concurrent variable if no error (pingcap#36437) ddl: fix update panic in the middle of multi-schema change (pingcap#36421) session: Mising OptimizeWithPlanAndThenWarmUp in prepare-execute path (pingcap#36347) executor,metrics: add a metric for observing execution phases (pingcap#35906) br: unified docker image align with tidb (pingcap#36016) ddl: skip to close nil sessPool (pingcap#36425) ...
@bb7133 I agree with Morgan on this. As we don't have a compelling use case to go beyond what MySQL offers atm, I think we should have it do what MySQL does. |
bb7133
left a comment
There was a problem hiding this comment.
LGTM, please fix the lint as commented by @morgo: #36146 (comment)
9e9038b to
d3c184e
Compare
2f60169 to
4eb5856
Compare
|
/merge |
|
This pull request has been accepted and is ready to merge. DetailsCommit hash: 4eb5856 |
TiDB MergeCI notify🔴 Bad News! [1] CI still failing after this pr merged.
|
|
Hi @joycse06 , thanks for your contribution! Would you like to add the document for this variable(https://github.com/pingcap/docs/blob/master/system-variables.md)? |
|
@bb7133 Sure, I will give it a go. I will raise a PR in that repo when ready. Thanks. |
|
@bb7133 Can you please have a look at pingcap/docs#9886 ? Thanks |
What problem does this PR solve?
Issue Number: close #28544
Problem Summary:
This is a feature from MySQL 8.0:
https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_sql_require_primary_key
It allows you to enforce tables have a primary key. In MySQL it is important because Row-based replication benefits from it.
With TiDB, TiCDC (mysql sink) will not replicate tables without a primary/unique key by default. If you chose to replicate tables without a primary key, the data will be duplicated. For our use-case this is an issue, since we use TiCDC to async replicate between clusters we intend to failover to.
What is changed and how it works?
sql_require_primary_keysysvar has been added. When enabled it will throw an error if a create table ddl doesn't have a primary key.Check List
Tests
Side effects
Documentation
Release note
Please refer to Release Notes Language Style Guide to write a quality release note.