Skip to content

master: document tidb_foreign_key_check_in_shared_lock#21359

Open
you06 wants to merge 4 commits intopingcap:masterfrom
you06:docs-shared-lock
Open

master: document tidb_foreign_key_check_in_shared_lock#21359
you06 wants to merge 4 commits intopingcap:masterfrom
you06:docs-shared-lock

Conversation

@you06
Copy link
Contributor

@you06 you06 commented Feb 12, 2026

ref tikv/tikv#19087

First-time contributors' checklist

What is changed, added or deleted? (Required)

Which TiDB version(s) do your changes apply to? (Required)

Tips for choosing the affected version(s):

By default, CHOOSE MASTER ONLY so your changes will be applied to the next TiDB major or minor releases. If your PR involves a product feature behavior change or a compatibility change, CHOOSE THE AFFECTED RELEASE BRANCH(ES) AND MASTER.

For details, see tips for choosing the affected versions (in Chinese).

  • master (the latest development version)
  • v9.0 (TiDB 9.0 versions)
  • v8.5 (TiDB 8.5 versions), from v8.5.6
  • v8.1 (TiDB 8.1 versions)
  • v7.5 (TiDB 7.5 versions)
  • v7.1 (TiDB 7.1 versions)
  • v6.5 (TiDB 6.5 versions)
  • v6.1 (TiDB 6.1 versions)
  • v5.4 (TiDB 5.4 versions)

What is the related PR or file link(s)?

Do your changes match any of the following descriptions?

  • Delete files
  • Change aliases
  • Need modification after applied to another branch
  • Might cause conflicts after applied to another branch

Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
@ti-chi-bot ti-chi-bot bot added missing-translation-status This PR does not have translation status info. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 12, 2026
@hfxsd hfxsd added translation/doing This PR’s assignee is translating this PR. and removed missing-translation-status This PR does not have translation status info. labels Feb 12, 2026
@hfxsd hfxsd self-assigned this Feb 12, 2026
Signed-off-by: you06 <you1474600@gmail.com>
@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 12, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please ask for approval from hfxsd. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@hfxsd hfxsd added translation/done This PR has been translated from English into Chinese and updated to pingcap/docs-cn in a PR. v9.0-beta.3 This PR/issue applies to TiDB v9.0-beta.3. and removed translation/doing This PR’s assignee is translating this PR. labels Feb 13, 2026
@hfxsd hfxsd added the needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. label Feb 26, 2026
@qiancai qiancai assigned qiancai and unassigned hfxsd Feb 26, 2026
Copy link
Collaborator

@qiancai qiancai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest LGTM

在 `INSERT` 或者 `UPDATE` 子表时,外键约束会检查父表中是否存在对应的外键值,并对父表中的该行数据上锁,避免该外键值被其他操作删除,导致破坏外键约束。

因为 TiDB 目前暂不支持 `LOCK IN SHARE MODE`,所以,在并发写入子表场景,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。建议在大批量写入子表数据时,关闭 [`foreign_key_checks`](/system-variables.md#foreign_key_checks)。
默认情况下,外键检查对父表的上锁行为等同于对父表中外键值所在行做 `SELECT FOR UPDATE` 操作(即排他锁),在并发写入子表场景中,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
默认情况下,外键检查对父表的上锁行为等同于对父表中外键值所在行做 `SELECT FOR UPDATE` 操作(即排他锁),在并发写入子表场景中,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突
默认情况下,在悲观事务中,外键检查对父表中行的加锁行为等价于对该行执行一次 `SELECT ... FOR UPDATE` 的锁定读(即加排他锁)。在子表高并发写入的场景下,如果大量事务反复引用相同的父表行,可能出现较严重的锁冲突

因为 TiDB 目前暂不支持 `LOCK IN SHARE MODE`,所以,在并发写入子表场景,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。建议在大批量写入子表数据时,关闭 [`foreign_key_checks`](/system-variables.md#foreign_key_checks)。
默认情况下,外键检查对父表的上锁行为等同于对父表中外键值所在行做 `SELECT FOR UPDATE` 操作(即排他锁),在并发写入子表场景中,如果引用的外键值大部分都一样,可能会有比较严重的锁冲突。

你可以通过开启系统变量 [`tidb_foreign_key_check_in_shared_lock`](/system-variables.md#tidb_foreign_key_check_in_shared_lock-从-v856-和-v900-版本开始引入) 来让外键检查使用共享锁。共享锁允许多个事务同时持有同一行的锁,从而减少锁冲突,提升并发写入子表的性能。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
你可以通过开启系统变量 [`tidb_foreign_key_check_in_shared_lock`](/system-variables.md#tidb_foreign_key_check_in_shared_lock-从-v856-和-v900-版本开始引入) 来让外键检查使用共享锁。共享锁允许多个事务同时持有同一行的锁,从而减少锁冲突,提升并发写入子表的性能
你可以通过开启系统变量 [`tidb_foreign_key_check_in_shared_lock`](/system-variables.md#tidb_foreign_key_check_in_shared_lock-从-v856-和-v900-版本开始引入) 来让外键检查使用共享锁。共享锁允许多个事务在同一父表行同时完成外键检查,从而减少锁冲突,提升子表并发写入性能

- 是否受 Hint [SET_VAR](/optimizer-hints.md#set_varvar_namevar_value) 控制:否
- 类型:布尔型
- 默认值:`OFF`
- 这个变量用于控制在悲观事务中,外键约束检查是否使用共享锁(而非排他锁)。开启后,外键检查对父表行加共享锁,允许多个并发事务同时检查同一父表行而不互相阻塞,从而提升并发写入子表的性能。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 这个变量用于控制在悲观事务中,外键约束检查是否使用共享锁(而非排他锁)。开启后,外键检查对父表行加共享锁,允许多个并发事务同时检查同一父表行而不互相阻塞,从而提升并发写入子表的性能
- 该变量用于控制在悲观事务中,外键约束检查对父表中的行加锁时是否使用共享锁(而非排他锁)。开启后,多个并发事务可以同时对同一父表行执行外键检查而不互相阻塞,从而降低锁冲突并提升子表并发写入性能

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Mar 2, 2026
@ti-chi-bot
Copy link

ti-chi-bot bot commented Mar 2, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-03-02 03:31:24.531101023 +0000 UTC m=+156129.109180227: ☑️ agreed by qiancai.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-1-more-lgtm Indicates a PR needs 1 more LGTM. needs-cherry-pick-release-8.5 Should cherry pick this PR to release-8.5 branch. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. translation/done This PR has been translated from English into Chinese and updated to pingcap/docs-cn in a PR. v9.0-beta.3 This PR/issue applies to TiDB v9.0-beta.3.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants