Skip to content

*: implement shared lock for foreign key#65752

Merged
ti-chi-bot[bot] merged 18 commits intopingcap:masterfrom
you06:master-shared-lock
Feb 11, 2026
Merged

*: implement shared lock for foreign key#65752
ti-chi-bot[bot] merged 18 commits intopingcap:masterfrom
you06:master-shared-lock

Conversation

@you06
Copy link
Contributor

@you06 you06 commented Jan 23, 2026

What problem does this PR solve?

Issue Number: close #66154

ref tikv/tikv#19087

Problem Summary:

What changed and how does it work?

Add shared lock support in TiDB.

  • Introduce a new variable tidb_foreign_key_check_in_shared_lock as the feature switch, default off.
  • Mark exclusive or shared flags during execution
  • Some stats and execution details for shared lock.

Check List

Tests

  • 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.

Support shared locks in foreign key use cases.

Copilot AI review requested due to automatic review settings January 23, 2026 07:17
@ti-chi-bot ti-chi-bot bot added do-not-merge/invalid-title do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 23, 2026
@you06 you06 changed the title Master shared lock *: implement shared lock for TiDB Jan 23, 2026
@tiprow
Copy link

tiprow bot commented Jan 23, 2026

Hi @you06. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Details

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.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces shared-lock support for foreign key checks in TiDB and wires basic observability for shared locks.

Changes:

  • Add a new session/global system variable tidb_foreign_key_check_in_shared_lock and associated session fields to control whether foreign key checks use shared locks.
  • Extend the pessimistic DML locking path and foreign key checker to distinguish between exclusive and shared “unchanged” keys, defer FK locks into handlePessimisticDML, and propagate a new InShareMode flag down to the TiKV lock RPCs and coprocessor lock resolution.
  • Add shared-lock-related execution details and metrics (SharedLockKeysDetail, StatementSharedLockKeysCount) and expose them via Prometheus and a new Grafana panel, plus basic integration into runtime stats.

Reviewed changes

Copilot reviewed 18 out of 19 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/util/execdetails/runtime_stats.go Adds SharedLockKeys to RuntimeStatsWithCommit so commit/runtime stats can carry shared-lock execution details.
pkg/util/execdetails/execdetails.go Extends ExecDetails/SyncExecDetails with SharedLockKeysDetail, LockKeysDuration, and a MergeSharedLockKeysExecDetails helper.
pkg/store/copr/coprocessor.go Updates coprocessor lock error handling to resolve either a single lock or a set of shared locks via LockInfo.GetSharedLockInfos().
pkg/sessionctx/variable/sysvar.go Registers the new tidb_foreign_key_check_in_shared_lock sysvar and plumbs it into SessionVars.
pkg/sessionctx/variable/session.go Changes TxnCtxNoNeedToRestore.unchangedKeys to map[string]bool and adds helpers to record and split unchanged keys into exclusive vs shared lock sets; adds ForeignKeyCheckInSharedLock to SessionVars.
pkg/sessionctx/vardef/tidb_vars.go Defines the name and default (false) for TiDBForeignKeyCheckInSharedLock.
pkg/sessionctx/stmtctx/stmtctx.go Extends StatementContext with counters/durations for pessimistic locking, including SharedLockKeysCount.
pkg/metrics/session.go Introduces the StatementSharedLockKeysCount histogram for counting keys locked in shared mode per statement.
pkg/metrics/metrics.go Registers the new StatementSharedLockKeysCount Prometheus metric.
pkg/metrics/grafana/tidb.json Adds a Grafana heatmap panel that visualizes the distribution of statement_shared_lock_keys_count.
pkg/executor/write.go Updates unchanged-key tracking for row/unique keys to mark them as exclusive (shared=false) in the new AddUnchangedKeyForLock API.
pkg/executor/select.go Extends newLockCtx to accept an inSharedMode flag and propagate it to LockCtx.InShareMode; adjusts callers accordingly.
pkg/executor/point_get.go Adapts point get pessimistic locking to the new newLockCtx(..., inSharedMode bool) signature (currently always false).
pkg/executor/insert_common.go Marks duplicate-key unchanged locks as exclusive via the new AddUnchangedKeyForLock(key, false) signature.
pkg/executor/foreign_key.go Changes FK check locking: for pessimistic txns, collects keys into TxnCtx.unchangedKeys as shared; for optimistic txns, continues to lock immediately using newLockCtx(..., false).
pkg/executor/batch_point_get.go Updates batch point get pessimistic locking to the new newLockCtx signature.
pkg/executor/adapter.go Refactors handlePessimisticDML to reset and then split unchanged keys into exclusive vs shared passes, honoring ForeignKeyCheckInSharedLock; records shared-lock metrics and attaches SharedLockKeys exec details into runtime stats.
go.mod Bumps github.com/tikv/pd/client version, promotes some AliCloud dependencies to direct, and adds a replace to use github.com/you06/client-go/v2 for github.com/tikv/client-go/v2.
go.sum Synchronizes module checksums with the updated go.mod (including the new client-go fork and pd client version).

@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jan 28, 2026
@you06 you06 changed the title *: implement shared lock for TiDB *: implement shared lock for foreign key Jan 28, 2026
@you06
Copy link
Contributor Author

you06 commented Jan 28, 2026

/retest

@tiprow
Copy link

tiprow bot commented Jan 28, 2026

@you06: PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test.

Details

In response to this:

/retest

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.

you06 and others added 10 commits February 9, 2026 12:05
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Feb 9, 2026
Signed-off-by: you06 <you1474600@gmail.com>
Signed-off-by: you06 <you1474600@gmail.com>
@you06
Copy link
Contributor Author

you06 commented Feb 10, 2026

/retest

1 similar comment
@cfzjywxk
Copy link
Contributor

/retest

Signed-off-by: you06 <you1474600@gmail.com>
@you06
Copy link
Contributor Author

you06 commented Feb 10, 2026

/retest

2 similar comments
@you06
Copy link
Contributor Author

you06 commented Feb 10, 2026

/retest

@you06
Copy link
Contributor Author

you06 commented Feb 10, 2026

/retest

Signed-off-by: you06 <you1474600@gmail.com>
@you06
Copy link
Contributor Author

you06 commented Feb 11, 2026

/retest

Signed-off-by: you06 <you1474600@gmail.com>
@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Feb 11, 2026
@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 11, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-02-09 08:55:55.116919513 +0000 UTC m=+175170.811059353: ☑️ agreed by cfzjywxk.
  • 2026-02-11 04:15:27.255786887 +0000 UTC m=+331142.949926737: ☑️ agreed by zyguan.

@hawkingrei
Copy link
Member

/retest

Copy link

@yudongusa yudongusa left a comment

Choose a reason for hiding this comment

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

Please open a document PR

Copy link
Contributor

@zimulala zimulala left a comment

Choose a reason for hiding this comment

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

Metrics LGTM

@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 11, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfzjywxk, yudongusa, YuJuncen, zimulala, zyguan

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

The pull request process is described 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

@ti-chi-bot ti-chi-bot bot added the approved label Feb 11, 2026
@hawkingrei
Copy link
Member

/retest

1 similar comment
@hawkingrei
Copy link
Member

/retest

@ti-chi-bot ti-chi-bot bot merged commit 94b366e into pingcap:master Feb 11, 2026
56 of 64 checks passed
@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm ok-to-test Indicates a PR is ready to be tested. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

*: implement shared lock for foreign key usage

8 participants