Skip to content

fix: refresh prepared DDL plans after schema changes - #26021

Merged
XuPeng-SH merged 40 commits into
matrixorigin:mainfrom
ck89119:issue-25986-main
Jul 28, 2026
Merged

XuPeng-SH merged 40 commits into
matrixorigin:mainfrom
ck89119:issue-25986-main

Conversation

@ck89119

@ck89119 ck89119 commented Jul 22, 2026

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:

Fixes #25986

What this PR does / why we need it:

Prepared schema-changing statements did not record their target tables as schema dependencies. As a result, repeated EXECUTE calls could reuse DDL plans built from stale table definitions and skip duplicate or missing object validation.

This change collects the target table metadata for prepared ALTER TABLE, CREATE INDEX, and DROP INDEX plans. Existing schema-version checks can then rebuild the prepared plan whenever the table definition changes, while preserving compile-cache reuse for unchanged prepared queries.

The regression coverage verifies repeated add/drop column and create/drop index operations, including a drop-column case with a secondary index.

Validation:

  • go build -mod=readonly ./pkg/sql/plan
  • go vet -mod=readonly ./pkg/sql/plan
  • go test -count=1 -timeout=180s ./pkg/sql/plan
  • go test -count=1 -timeout=180s -run 'TestInitExecuteStmtParam' ./pkg/frontend
  • mo-tester prepared DDL BVT: 29/29 passed

@matrix-meow matrix-meow added size/L Denotes a PR that changes [500,999] lines and removed size/M Denotes a PR that changes [100,499] lines labels Jul 22, 2026
@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 →

@gouhongshen gouhongshen 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.

Codex automated review

One blocking correctness regression found in prepared CLONE execution under database remapping.

P1 - Prepared CLONE loses database remapping (pkg/frontend/mysql_cmd_executor.go:1890)

GetDatabase() is the actual current database, while DefaultDatabase() applies the saved remap for unqualified names. remapDbInStmt has no CloneTable case, so CLONE relies on DefaultDatabase(). This new serialization then qualifies both source and destination with the actual database and marks them explicit, causing every refreshed EXECUTE to bypass remapping and clone from/create in the wrong database or fail. Preserve the effective remapped database semantics when creating the fresh CLONE AST.

@gouhongshen gouhongshen 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.

Codex automated review

Previous P1 on prepared CLONE remapping is fixed: cloneSQL now captures the remapped effective default database, with unit and BVT coverage. No other prior blockers exist; the follow-up merge had no conflict-resolution delta. Focused Go test could not run because go is unavailable in this environment.

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

Blocking correctness issue remains on latest head 9b04ddccbbf6.

A prepared statement rebuilt against transaction-local DDL remains cached after that transaction rolls back:

create table t(a int);
prepare s from 'select * from t';
begin;
alter table t add column b int;
execute s;
rollback;
execute s;

I reproduced this on the latest head with binary COM_STMT_PREPARE/EXECUTE. The final EXECUTE still reports columns [a b], while the rolled-back table has only [a].

recordSessionDDL advances ddlVersion after successful DDL, and EXECUTE stores the workspace-derived PreparePlan, ColDefData, cached compile, and matching generation. The full ROLLBACK path does not advance that generation, so the next transaction reuses state derived from a discarded workspace.

Please capture workspace.GetHaveDDL() before the transaction operator is cleared and, after a successful full rollback, advance the session DDL generation when that transaction contained DDL. This should cover explicit ROLLBACK, commit-failure/error rollback, and text/binary prepared statements retained across rollback. It should remain conditional on transaction-local DDL, rather than invalidating every rollback.

The prepared CLONE remap issue from the previous review is fixed by 6afe565744; this request is for the remaining rollback lifecycle gap.

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

Requesting changes at d309dbb463 for the following production blockers:

  1. Failed rollback can preserve a prepared plan built from discarded transaction-local DDL. rollbackUnsafe advances ddlVersion only when TxnOperator.Rollback succeeds. On error or panic it discards the frontend transaction without advancing the generation. DistTAE rollback can call txn.delTransaction() and still return loadCleanupErr, so the workspace may already be gone. Aborted DDL never reaches CatalogCache, leaving no other invalidation signal. Please advance the generation whenever haveDDL is true and this frontend transaction is discarded, regardless of rollback result. The current failed-rollback test incorrectly expects version 0; please also cover the subsequent prepared execution.

  2. DDL executed through backSession does not invalidate the upstream user session. Both recordSessionDDL and advanceDDLVersionAfterDiscardedTxnDDL only handle a direct *Session. Stored-procedure SQL executes through *backSession, while the outer CALL is not catalog-changing. A procedure that drops and recreates a table can therefore leave the caller prepared plan and binary metadata reusable until asynchronous logtail delivery. Please centralize scoped generation propagation through the upstream session chain and add background or stored-procedure commit and rollback coverage.

  3. Stable prepared queries on subscription databases now execute synchronous catalog SQL on every EXECUTE. preparedSubscriptionSchemaChanged calls Resolve before the cheap catalog-cache check. Production Resolve performs subscription account and publication catalog queries through a background executor. The benchmark uses an in-memory resolver and does not measure this cost. Please use a cheap subscription generation or high-watermark signal and resolve only when change is possible; also deduplicate collected dependencies.

  4. Historical snapshot subscription dependencies can rebuild forever. Collection drops the scan or view snapshot, while validation resolves the current subscription with a nil snapshot. If the historical object is v1 and the current publication is v2, every execution detects a change, rebuilds against historical v1 again, and repeats. Please preserve the dependency snapshot or identity, or define an immutable-snapshot strategy, with a test proving the second unchanged execution does not rebuild.

The overall dependency plus session-generation direction is valid and materially improves #25986, but these invalidation gaps and the subscription hot-path regression need to be closed before merge. CI status is intentionally not part of this review.

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

Re-reviewed exact head 08a674c22181576cd0457434a0444c81d2e6ef50. The previously reported rollback lifecycle, upstream background-session propagation, subscription hot-path, and historical-snapshot identity issues are addressed. Focused race tests, full affected-package tests, build/vet, and the issue #25986 embedded regression all pass.

@mergify

mergify Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-27 12:00 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue — 2026-07-27 13:27 UTC · at 08a674c22181576cd0457434a0444c81d2e6ef50

This pull request spent 1 hour 27 minutes 9 seconds in the queue, with no time running CI.

Reason

Pull request #26021 has been dequeued

Queue conditions are not satisfied:

  • -conflict [📌 queue requirement]

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

@mergify

mergify Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-27 14:06 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue — 2026-07-27 15:17 UTC · at f512b7adffdfc49c6032e872042c204876ea09fb

This pull request spent 1 hour 11 minutes 23 seconds in the queue, with no time running CI.

Reason

Pull request #26021 has been dequeued

Queue conditions are not satisfied:

  • -conflict [📌 queue requirement]

Hint

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

# Conflicts:
#	pkg/sql/plan/query_builder.go
@mergify

mergify Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-27 16:57 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🚫 Left the queue — 2026-07-27 17:25 UTC · at 2bb6592a8c4ece563fe464effad1b53856c7dbbe

This pull request spent 27 minutes 41 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

merge conflict between base and head

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Requeued — the merge queue status continues in this comment ↓.

@mergify

mergify Bot commented Jul 27, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-07-28 05:15 UTC · Rule: main · triggered by @ck89119 with the merge queue checkbox
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-07-28 07:32 UTC · at 6962a9f3402744136b0b5da7c5c9302befb458f0

This pull request spent 2 hours 16 minutes 59 seconds in the queue, with no time running CI.

Reason

The pull request can't be updated

This pull request seems to come from a fork, and Mergify needs the author's permission to update its branch.
The author needs to enable "Allow edits from maintainers" on this pull request, or update the branch manually.

Hint

You should update or rebase your pull request manually. If you do, this pull request will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue, you can requeue the pull request, without updating it, by posting a @mergifyio queue comment.

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

ck89119 and others added 3 commits July 28, 2026 11:05
# Conflicts:
#	pkg/frontend/computation_wrapper.go
#	pkg/frontend/mysql_cmd_executor.go
#	pkg/frontend/types.go

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

kind/bug Something isn't working size/XXL Denotes a PR that changes 2000+ lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Prepared schema-changing statements reuse stale validation state across EXECUTE

4 participants