Skip to content

fix(frontend): handle SET TRANSACTION isolation level - #26665

Merged
mergify[bot] merged 14 commits into
matrixorigin:mainfrom
iamlinjunhong:m-26648
Aug 10, 2026
Merged

mergify[bot] merged 14 commits into
matrixorigin:mainfrom
iamlinjunhong:m-26648

Conversation

@iamlinjunhong

Copy link
Copy Markdown
Contributor

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #26648

What this PR does / why we need it:

fix(frontend): handle SET TRANSACTION isolation level

@iamlinjunhong
iamlinjunhong requested a review from a user August 4, 2026 02:48
@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Aug 4, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Request changes for two correctness gaps:

  1. handleSetTransaction only updates the compatibility sysvar map. TxnHandler.createTxnOpUnsafe never reads transaction_isolation, while txnClient.newTxnMeta initializes the real TxnMeta.Isolation from service-wide moruntime.TxnIsolation. Therefore the new tests prove only that @@transaction_isolation echoes the requested string; a JDBC/ODBC caller can now observe success and the requested value while the next transaction still runs with the old isolation. Please connect the session/next-transaction setting to WithTxnIsolation when creating the owned transaction, reject levels MO cannot implement, and add a test that captures the created transaction option/meta rather than only reading the sysvar.

  2. The AST cannot distinguish SET TRANSACTION ... from SET SESSION TRANSACTION ...: both grammar branches produce SetTransaction{Global:false}. This handler consequently makes the unscoped form a persistent session setting. MySQL defines the unscoped form as next-transaction-only, while SESSION changes subsequent session transactions. Please preserve the scope in the AST and test that an unscoped override is consumed once and then falls back to the session default.

Q1/Q2: the current success paths update/report state, but neither reaches the real transaction owner; there is no generation-scoped next-transaction state to consume. Q3: no cleanup/lifecycle regression found in the added handler itself.

Validation: focused frontend tests passed 10x; the failure is a missing semantic assertion, not test instability.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed exact head d03c459. Two merge-blocking correctness issues remain:

  1. SetTransaction is classified as a parameter-modification statement that must finish the active transaction. The normal frontend finish path therefore commits the whole existing transaction on a successful SET and rolls it back on an invalid SET. For example, BEGIN; INSERT; SET SESSION TRANSACTION ISOLATION LEVEL ...; ROLLBACK can commit at SET, although SESSION scope inside an active transaction must affect only future transactions. An unscoped SET TRANSACTION rejected inside an active transaction can likewise roll back unrelated prior work. The handler-only tests bypass this lifecycle. Please keep SET TRANSACTION from implicitly committing/rolling back the active transaction and add end-to-end transaction-state counterexamples. MySQL reference: https://dev.mysql.com/doc/refman/8.4/en/set-transaction.html

  2. GLOBAL alias persistence is split. Validation accepts transaction_isolation and tx_isolation, but the global update stores only the supplied name; new sessions seed the real TxnHandler isolation only from canonical transaction_isolation. SET GLOBAL tx_isolation='READ-COMMITTED' can therefore update the alias/sysvar view while newly created sessions still execute with the stale canonical isolation. Canonicalize alias writes (and reads) and cover new-session behavior through the actual TxnMeta.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed exact head 628959c1ede68c363981124bfa786e8d3a5865c2. The previous transaction-boundary and canonical-alias blockers are fixed, and the focused frontend/parser tests pass. One correctness blocker remains:

SET TRANSACTION access characteristics are parsed but silently ignored. handleSetTransaction skips every !characteristic.IsLevel item and returns success when no isolation item was found, so SET SESSION TRANSACTION READ ONLY returns OK while storing no read-only state and applying no transaction option; the next transaction is still writable. The BVT/unit tests currently codify that false-success behavior as “syntax compatibility”. Applications can therefore believe a read-only boundary was established when it was not.

Please either implement access mode across the same NEXT/SESSION/GLOBAL state model and apply txnclient.WithTxnReadyOnly() (including READ WRITE reset, one-shot NEXT consumption, active-transaction boundaries, and TxnMeta/write-rejection tests), or fail closed with NotSupported until the behavior is complete. Please also validate duplicate/conflicting characteristics rather than silently accepting a last-wins/no-op list.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Request changes: the new isolation plumbing still has correctness and upgrade blockers.\n\n1. Scope is not closed across equivalent syntaxes. SET SESSION transaction_isolation still takes the generic SetVar path and commits an active user transaction. Bare SET @@transaction_isolation is parsed as SESSION rather than NEXT. Also, SET GLOBAL TRANSACTION creates a frontend temporary txn which consumes a pending NEXT isolation, despite GLOBAL being required to leave existing sessions unaffected. I reproduced all three state transitions.\n\n2. Upgrade availability regression: previous versions accepted and persisted tx_isolation=READ-UNCOMMITTED/SERIALIZABLE. This change reads those valid legacy catalog values, then InitSystemVariables rejects them through txnIsolationFromSystemValue, so new sessions for that account cannot initialize after upgrade. New writes may fail closed, but legacy rows need migration-safe normalization/fallback. Canonical-only writes also leave a rolling-upgrade old node observing the stale tx_isolation row.\n\n3. SET transaction_isolation=DEFAULT uses the static enum default (REPEATABLE-READ), not the service/account global default. On an RC-configured service, a session initialized as RC is changed to RR by DEFAULT; the actual TxnMeta follows the wrong value.\n\n4. The active-NEXT rejection is returned as generic InvalidInput (1105/HY000), whereas MySQL/JDBC compatibility requires ER_CANT_CHANGE_TX_CHARACTERISTICS (1568/25001).\n\nPlease model transaction-characteristic scope centrally at semantic transaction boundaries, and add counterexample tests for these sequences before updating the PR.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Deep-reviewed exact head 9e80ee7.

[P1] A SESSION isolation assignment must clear a pending NEXT-transaction override.

TxnHandler.setSessionTxnIsolation updates only sessionTxnIsolation and hasSessionTxnIsolation (pkg/frontend/txn.go:357-362). It leaves hasNextTxnIsolation set, while txnIsolationUnsafe gives NEXT precedence (pkg/frontend/txn.go:379-389). Consequently:

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
START TRANSACTION;

still starts the first transaction as RC and consumes the stale NEXT override. The same defect is reachable through bare SET transaction_isolation and SET @@SESSION.transaction_isolation.

MySQL explicitly specifies that a SESSION transaction_isolation assignment between transactions overrides any preceding next-transaction assignment: https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_transaction_isolation

I added a minimal exact-head state-machine counterexample locally: set NEXT=RC, then SESSION=SI, then call txnIsolationUnsafe(true). It fails because the handler returns RC with consumeNext=true instead of SI with consumeNext=false. Clear the pending NEXT generation when applying SESSION scope and add regressions for SET SESSION TRANSACTION plus both SESSION sysvar spellings.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Deep-reviewed exact head 5298e821d531d68e7728dfc43df3ff5858c975e6. The stale NEXT override and all previously reported single-assignment scope/default/upgrade gaps are fixed. One transaction-boundary blocker remains:

[P1] Mixed SET assignments bypass the transaction-characteristic preservation path and can roll back an existing user transaction.

isTransactionCharacteristicStatement returns true only when every SetVar assignment is an isolation assignment (pkg/frontend/txn.go). Therefore this valid sequence:

BEGIN;
-- any prior DML
SET @@transaction_isolation = 'READ-COMMITTED', @probe = 1;

correctly raises ER_CANT_CHANGE_TX_CHARACTERISTICS (1568), but needToFinishTransactionAtStatementEnd then falls back to NeedToBeCommittedInActiveTransaction(SetVar). The error cleanup takes RollbackUnsafe1, discards the whole pre-existing transaction, and the client can no longer decide to COMMIT or ROLLBACK its prior work. The same classification problem affects successful mixed SESSION-isolation assignments by allowing generic SET lifecycle rules to finish the existing transaction.

I reproduced this on the exact head by adding the mixed statement to TestSetTransactionDoesNotFinishExistingTransaction: after the expected 1568, handler.InActiveTxn() is false and the test fails. Please classify transaction isolation assignments independently of unrelated assignments (or otherwise preserve an active transaction for the whole mixed SET) and add both rejected NEXT and successful SESSION mixed-assignment counterexamples.

Validation of the unaffected paths: focused isolation/boundary tests passed 13x each under race, the full frontend package passed under race, frontend/parser/moerr package tests passed, and the latest-main merge tree is clean.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Deep-reviewed exact head a0bc85b3830a5eaef212b88eed2bbe1ecc3f6639. The latest mixed-SET fix closes the remaining transaction-boundary blocker: any transaction-isolation assignment now preserves an already active user transaction, including rejected NEXT and successful SESSION statements mixed with unrelated assignments, while statement-owned temporary transactions are still finalized.

I also rechecked scope/default semantics, canonical transaction_isolation / legacy tx_isolation compatibility, rolling-upgrade catalog normalization, one-shot NEXT consumption and failure retention, unsupported access-mode fail-closed behavior, commit timestamp propagation, parser/AST construction, and Q1-Q3 lifecycle paths. I found no remaining correctness, compatibility, concurrency, resource-lifecycle, or performance blocker.

Validation on this exact head: focused transaction-boundary test 100x under -race; isolation/meta/global/legacy/commit-TS tests 100x under -race; full pkg/frontend under -race; parser/tree/moerr tests; and build/vet for all affected production packages. All passed.

@mergify

mergify Bot commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-10 17:39 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks skipped · PR is already up-to-date
  • ✅ Merged — 2026-08-10 17:39 UTC · at a0bc85b3830a5eaef212b88eed2bbe1ecc3f6639 · squash

This pull request spent 31 seconds in the queue, including 4 seconds running CI.

Required conditions to merge
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-neutral = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / e2e BVT Test on Linux/x64(LAUNCH, PESSIMISTIC)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone CI / SCA Test on Linux/arm64
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-neutral = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
    • check-skipped = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone Utils CI / Coverage
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86

This branch was previously deployed

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

Labels

size/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants