Skip to content

Conversation

@wfxr
Copy link
Member

@wfxr wfxr commented Jan 29, 2026

Issue Number: close #64198

Problem Summary:

When autocommit=0 and tidb_read_staleness is set, each SELECT statement recalculates its own read_ts instead of reusing the same snapshot. This causes inconsistent reads across multiple statements within what users expect to be a single transaction.

What changed and how does it work?

Changed EnterNewTxnWithReplaceProvider to EnterNewTxnWithBeginStmt when !IsAutocommit() in updateStateFromStaleReadProcessor(). This triggers activateStaleTxn() in StalenessTxnContextProvider.This ensures:

  • InTxn() becomes true after the first SELECT
  • Subsequent reads reuse the same snapshot (StartTS)
  • Write statements are rejected (existing behavior)
  • COMMIT/ROLLBACK ends the transaction normally

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.

Fix an issue that stale read with `tidb_read_staleness` does not provide consistent snapshot when `autocommit=0`, causing each SELECT to use a different `read_ts` instead of reusing the same snapshot within the transaction.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 29, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@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. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. sig/planner SIG: Planner labels Jan 29, 2026
@tiprow
Copy link

tiprow bot commented Jan 29, 2026

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jan 29, 2026
@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 29, 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 assign king-dylan for approval. 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

@wfxr wfxr force-pushed the stale-read/64198 branch 3 times, most recently from 2857898 to da39e55 Compare January 29, 2026 08:21
@wfxr
Copy link
Member Author

wfxr commented Jan 29, 2026

/test all

@wfxr
Copy link
Member Author

wfxr commented Jan 29, 2026

/retest

@wfxr wfxr force-pushed the stale-read/64198 branch from da39e55 to ea4473c Compare January 29, 2026 10:13
@wfxr
Copy link
Member Author

wfxr commented Jan 29, 2026

/test all

@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.6133%. Comparing base (060097f) to head (a487a74).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #65908        +/-   ##
================================================
- Coverage   77.7756%   77.6133%   -0.1623%     
================================================
  Files          2001       1923        -78     
  Lines        545524     533113     -12411     
================================================
- Hits         424285     413767     -10518     
+ Misses       119577     119340       -237     
+ Partials       1662          6      -1656     
Flag Coverage Δ
integration 41.5259% <100.0000%> (-6.6389%) ⬇️
unit 76.7337% <100.0000%> (+0.3211%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 56.7974% <ø> (ø)
parser ∅ <ø> (∅)
br 48.8491% <ø> (-12.1366%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

When `autocommit=0` and stale read is enabled, the first SELECT now
activates a proper stale-read transaction instead of just replacing
the provider. This ensures:

1. `InTxn()` becomes true after the first SELECT
2. Subsequent reads reuse the same snapshot (StartTS)
3. Write statements are rejected (existing behavior)
4. COMMIT/ROLLBACK ends the transaction normally
@wfxr wfxr force-pushed the stale-read/64198 branch from ea4473c to a487a74 Compare January 29, 2026 12:46
@wfxr
Copy link
Member Author

wfxr commented Jan 29, 2026

/test all

@wfxr
Copy link
Member Author

wfxr commented Jan 29, 2026

/retest

@ti-chi-bot
Copy link

ti-chi-bot bot commented Jan 29, 2026

@wfxr: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-unit-test-next-gen a487a74 link true /test pull-unit-test-next-gen

Full PR test history. Your PR dashboard.

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. I understand the commands that are listed here.

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

Labels

do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support activiating transaction for stale read used with autocommit = 0

1 participant