Skip to content

fix: preserve index visibility in clone - #27143

Merged
XuPeng-SH merged 9 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27078-f861fbf7
Aug 14, 2026
Merged

XuPeng-SH merged 9 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27078-f861fbf7

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Aug 14, 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 #27078

What this PR does / why we need it:

Table/database CLONE and CREATE TABLE ... LIKE ... rebuild source schemas through
SHOW CREATE-style DDL. The serializer omitted INVISIBLE and catalog insertion
always wrote is_visible = 1, so rebuilding a secondary index could alter
optimizer-visible schema state.

Historic proto3 IndexDef.Visible=false is ambiguous: it can be a pre-upgrade
default-visible index whose field was omitted or an explicitly invisible index.
mo_indexes.is_visible is authoritative for a planner-resolved, persisted local
source table.

Changes

  • Add IndexOption.Visibility, a durable tri-state marker at protobuf wire
    field 4. New regular/spatial, unique, fulltext, plugin, and ALTER-add indexes
    write it while synchronizing the legacy boolean. IndexDef reserved field 14
    is untouched.
  • Reconcile every normal persisted local source index from mo_indexes at the
    source table ID, snapshot, and tenant before LIKE/CLONE or ALTER COPY rebuilds
    DDL, even when a marker is present. This prevents an old CN preserving a
    stale unknown marker after ALTER INDEX changes catalog visibility.
  • Keep ConstructCreateTableSQL a pure formatter: it has callers that do not
    own a source catalog snapshot. Local SHOW CREATE TABLE now reconciles after
    resolving its source table; subscription SHOW/LIKE sources retain the
    publisher-provided definition instead of querying the subscriber catalog.
  • Treat fixed system-schema indexes, including mo_task, as visible bootstrap
    metadata. They intentionally have no mo_indexes rows. Missing or
    contradictory metadata for normal user tables still fails before emitting a
    rebuilt schema.
  • Preserve marker-based visibility through SHOW CREATE-style DDL, deep copies,
    ALTER visibility updates, and mo_indexes insertion. Visibility-only options
    still emit SQL NULL for the unrelated options column.

Issue-to-test proof

  • Distributed BVT clone_index_visibility creates visible and invisible
    secondary indexes, then asserts SHOW INDEX and SHOW CREATE for the source,
    table CLONE, CREATE TABLE LIKE, and database CLONE. It checks cloned rows
    through predicates and verifies an INVISIBLE COMMENT index remains hidden.
  • Planner regressions construct a pre-marker source with raw false and prove
    catalog-visible and catalog-invisible output for LIKE and table CLONE. A
    marker-bearing visible source with catalog-invisible metadata proves the
    catalog result overrides a stale marker.
  • Shared reconciliation tests cover legacy-invisible local SHOW CREATE,
    snapshot/tenant lookup, incomplete and contradictory metadata, no partial
    mutation on incomplete metadata, formatter-only callers with a table ID, and
    fixed-visible bootstrap/system schemas including mo_task.
  • Catalog/compiler tests cover marker semantics, legacy default-visible catalog
    insertion, explicit invisible insertion with an exact visibility tuple, and
    visibility-only option SQL.

Validation

  • Focused CGo planner visibility regressions, including
    TestBuildShowCreateTableReconcilesLegacyIndexVisibility,
    TestReconcileIndexVisibilityUsesFixedVisibleSystemDatabase, and the
    legacy/stale/missing-metadata LIKE/CLONE controls.
  • Full CGo ./pkg/sql/plan package.
  • git diff --check.
  • The failed PR BVT artifacts were manually inspected: the new
    clone_index_visibility case passed (25/25), while unrelated existing
    system and subscription cases exposed the missing-catalog ownership boundary
    corrected by this update.

Residual risks

No catalog migration is required. Planner-backed reconstruction resolves
authoritative visibility only where it owns the source table and catalog
snapshot. Context-free CDC, publication, dump, and get_ddl consumers retain
the historical visible fallback for an unmarked proto3 false definition; they
cannot safely resolve a pre-marker explicitly invisible index without a
separate source-catalog ownership change outside #27078's CLONE/LIKE scope.

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Aug 14, 2026
@mergify mergify Bot added kind/bug Something isn't working kind/test-ci labels Aug 14, 2026
@gouhongshen
gouhongshen force-pushed the agent/issue-27078-f861fbf7 branch from eea02e4 to 439315b Compare August 14, 2026 07:39
@gouhongshen
gouhongshen force-pushed the agent/issue-27078-f861fbf7 branch from 439315b to 2d74a85 Compare August 14, 2026 08:49
@gouhongshen
gouhongshen marked this pull request as ready for review August 14, 2026 08:49
@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 →

@aptend aptend 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 re-review of exact head 9cbdaed63d4d26560cc284d84a4b94a0cbb93c0b, including the complete current diff and all prior visibility comments. The original raw-proto3-bool issue is partially addressed, but the new state model is not closed across rolling upgrades or all DDL reconstruction consumers.

[P1] Do not treat IndexOption.Visibility presence as proof that it is authoritative. A new CN can persist VISIBILITY_VISIBLE; an old CN can then execute ALTER INDEX ... INVISIBLE, updating IndexDef.Visible and mo_indexes.is_visible while preserving the unknown nested option field during protobuf round-trip. reconcileIndexVisibility returns immediately at lines 169-170 when all markers are present and skips marked definitions at lines 223-225, so the stale VISIBLE marker wins over the catalog and CLONE/ALTER COPY reconstructs the index incorrectly. The marker needs a version/ownership protocol or authoritative catalog reconciliation that also detects and repairs stale marked values.

[P1] Reconcile legacy visibility at every production reconstruction boundary. ConstructCreateTableSQL currently makes createTableIndexVisible return true for every unset definition. SHOW CREATE calls it directly with table/snapshot context, while publication, CDC, get_ddl, and table-dump paths also call the context-free serializer. A pre-upgrade explicitly invisible index has Visible=false, no marker, and mo_indexes.is_visible=0; these paths therefore emit a visible index and may recreate or publish the wrong schema. LIKE/CLONE-only reconciliation does not close those consumers.

The exact head also fails its owning tests under the required CGo setup. TestGenInsertMOIndexesSqlPersistsInvisibleIndex emits is_visible=1 while the test requires 0. A full ./pkg/sql/plan run additionally fails TestRenameChangeColumnStillUsesCopyAlter, TestCaseOnlyChangeColumnUsesCopyAlterAndUpdatesForeignKeyCatalog, TestAlterTableCopyDropsEveryAdjacentIndexForDroppedColumn, and TestDropReferencedPrimaryKeyIsRejected because the new reconciliation returns missing visibility metadata. pkg/catalog passes. git diff --check is clean.

Requesting changes because rolling-upgrade and legacy-schema correctness remain unsafe and the affected owning test suites are red.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH @aptend I re-checked the rolling-upgrade review against 9cbdaed63d and addressed the in-scope defects in 5a48b7009b.

Do not treat the new marker as unconditionally authoritative across a rolling upgrade.

Fixed. Reconciliation now queries mo_indexes.is_visible for every normal
persisted source index, including a marker-bearing one, then writes the resolved
state only after validating the complete result. The LIKE/CLONE regression
starts with a VISIBLE marker and catalog value 0 and proves the rebuilt index is
INVISIBLE. The same reconciler protects ALTER COPY.

Reconcile legacy visibility before every production DDL reconstruction consumer.

SHOW CREATE now supplies its compiler context and source table ID to the shared
reconciler; its legacy-invisible regression is included. I traced the remaining
context-free callers: CDC receives only a source TableDef and a downstream
executor; publication receives a Relation but not its source engine/transaction;
table dump receives a TableDef plus a text resolver. Their current contracts
cannot perform a source snapshot catalog read. Adding that query ownership
across CDC, publication, dump, and get_ddl is a separate cross-subsystem
migration, outside #27078's CLONE/LIKE fix. This PR deliberately preserves an
explicit marker for those callers and never treats the ambiguous proto3 bool as
a freshness signal. The updated PR description records that residual boundary.

Make the fixture explicitly invisible through the durable marker.

Fixed. The compiler fixture now uses SetIndexVisibility(false) and asserts the
full visibility/options tuple. The exact focused compiler command passes, as do
full catalog, plan, and compile package tests.

@aptend aptend 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 the incremental fix and complete diff at exact head 5a48b7009b34479d832d4fe57a9a1afdaf8f95b1. The new commit fixes stale-marker reconciliation for planner paths, makes SHOW CREATE reconcile when a compiler context is available, repairs the affected fixtures, and the exact focused compile tests plus full pkg/catalog and pkg/sql/plan packages now pass under the required CGo environment. One P1 closure gap remains.

[P1] Preserve pre-upgrade invisible indexes in the context-free DDL consumers. createTableIndexVisible still maps every unset legacy definition to visible. That is necessary for a legacy default-visible index, but it regresses the equally reachable legacy explicitly-invisible state (Visible=false, no new marker, mo_indexes.is_visible=0). Publication (pkg/publication/ddl.go), CDC (pkg/cdc/sinker_v2.go), get_ddl (pkg/frontend/get_ddl.go), and table-dump reconstruction call ConstructCreateTableSQL(nil, ...), so the new reconciliation branch cannot run and these paths emit/recreate the legacy invisible index as visible. Before this PR those paths rendered the raw false state as INVISIBLE; the fallback now flips it. The serializer cannot distinguish the two legacy states without authoritative metadata, so each consumer needs a resolved visibility overlay/context (or a shared boundary that supplies one) before serialization. Please add a regression through at least one context-free public consumer using a pre-marker invisible definition and authoritative mo_indexes=0.

Requesting changes because upgrade compatibility is still incorrect outside the planner/SHOW/CLONE paths.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

@aptend This re-review targets 9cbdaed63d and is stale on current head 5a48b7009b.

  • The reconciler now reads mo_indexes for every normal persisted index, including a marked index; TestReconcileIndexVisibilityOverridesStaleMarker and the LIKE/CLONE stale-marker case prove catalog 0 wins over a VISIBLE marker.
  • SHOW CREATE now reconciles when it has a compiler context and table ID. The remaining nil-context CDC/publication/dump/get_ddl contracts cannot query source snapshot metadata; as explained above and in the PR description, that broader cross-subsystem ownership change is outside [Bug]: table and database CLONE turn invisible indexes visible #27078.
  • The compiler fixture is explicitly invisible, and the four plan fixtures now supply persisted mo_indexes metadata. The full CGo catalog/plan/compile suite passed after 5a48b7009b.

@gouhongshen

Copy link
Copy Markdown
Contributor Author

@aptend I checked the claimed context-free regression against the PR base aa3f1551ca110687d63b932bad7f550eacb7c27c.

ConstructCreateTableSQL previously rendered legacy Visible=false as INVISIBLE.

This is not true on the base. Base pkg/sql/plan/build_show_util.go passes indexVisibilityKnown=false from the public ConstructCreateTableSQL wrapper, and its ordinary and fulltext serializers append INVISIBLE only when indexVisibilityKnown && !indexdef.Visible. Publication, CDC, get_ddl, and dump already call that public API, so a context-free, marker-less proto3 false was already rendered visible before this PR. Current createTableIndexVisible preserves that exact legacy behavior for an unset marker; TestConstructCreateTableSQLDefaultsAmbiguousIndexVisibilityToVisible covers it.

The broader inability to distinguish a pre-marker explicitly invisible index in context-free consumers is real, but it is pre-existing and cannot be solved correctly in the serializer: those APIs lack the source transaction/snapshot/catalog query ownership needed to read mo_indexes. Wiring that ownership through CDC, publication, frontend, and dump is a separate cross-subsystem migration outside #27078, whose scope is the planner-backed table/database CLONE and CREATE TABLE LIKE reconstruction paths. The PR description records this residual boundary.

For this PR, normal persisted source tables—including stale marker-bearing ones—are now resolved from authoritative mo_indexes before LIKE/CLONE and ALTER COPY rendering; SHOW CREATE does the same when compiler context and table ID are available. The post-5a48b7009b full CGo suite for ./pkg/catalog ./pkg/sql/plan ./pkg/sql/compile passed.

@gouhongshen
gouhongshen requested a review from aptend August 14, 2026 13:46

@aptend aptend 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 dfb93576e6327eb6f7d2ddf52c9fbabbb4fdce73 after the rebase and latest response. APPROVED.

The author is correct that my previous context-free-consumer blocker was based on an incorrect base comparison: the pre-PR public ConstructCreateTableSQL passed indexVisibilityKnown=false, so marker-less proto3 Visible=false already used the historical default-visible rendering in nil-context consumers. This PR preserves that behavior and does not introduce the claimed regression. Resolving that pre-existing ambiguity in CDC/publication/dump/get_ddl requires authoritative catalog context outside #27078's CLONE/LIKE scope.

Within scope, this head reconciles normal persisted indexes—including stale marker-bearing definitions—from authoritative mo_indexes at the source snapshot/tenant, propagates resolved visibility through CLONE/LIKE, ALTER COPY, and context-backed SHOW CREATE, handles fixed-visible bootstrap indexes, and persists explicit visibility for new/altered definitions. The rebase range-diff preserves the reviewed commits; its only substantive conflict adaptation uses the upstream SQL quoting helper for index options.

Validation on this exact head: full ./pkg/catalog, ./pkg/sql/plan, and ./pkg/sql/compile pass with the required CGo/thirdparties setup; git diff --check is clean.

@mergify

mergify Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-14 15:56 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • 🟠 Checks running · in-place
  • 🚫 Left the queue — 2026-08-14 16:56 UTC · at 2be4f0297121b9486cf0cebdaf2677e9398dbcb2

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

Waiting for
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of: [🛡 GitHub branch protection]
    • 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)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of: [🛡 GitHub branch protection]
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
All conditions
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / UT Test on Ubuntu/x86
    • check-skipped = Matrixone CI / UT Test on Ubuntu/x86
    • check-success = Matrixone CI / UT Test on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone CI / SCA Test on Linux/arm64
    • check-skipped = Matrixone CI / SCA Test on Linux/arm64
    • check-success = Matrixone CI / SCA Test on Linux/arm64
  • any of [🛡 GitHub branch protection]:
    • 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)
    • check-success = Matrixone Compose CI / multi cn e2e bvt test docker compose(PROXY)
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Utils CI / Coverage
    • check-skipped = Matrixone Utils CI / Coverage
    • check-success = Matrixone Utils CI / Coverage
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-skipped = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
    • check-success = Matrixone UT Coverage / UT Coverage on Ubuntu/x86
  • any of [🛡 GitHub branch protection]:
    • check-neutral = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-skipped = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
    • check-success = Matrixone Standlone CI / multi CN e2e BVT Test on Linux/x64(COMPOSE, PESSIMISTIC)
  • #review-threads-unresolved = 0 [🛡 GitHub branch protection]
  • github-review-approved [🛡 GitHub branch protection]

Reason

Pull request #27143 has been dequeued

Pull request from fork cannot be queued. This pull request comes 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.

Failing checks:

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.

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

  • Requeue this pull request

@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 re-review completed on exact head f42e4790a7f7aa5a817bf6974b711c5e1302f484.

The latest scoping commit closes the remaining ownership and mutation boundaries:

  • local LIKE/CLONE, ALTER COPY, and context-backed SHOW CREATE reconcile every persisted index from authoritative mo_indexes in the same transaction/snapshot and tenant, so both pre-marker definitions and stale rolling-upgrade markers are handled;
  • resolved table definitions are deep-copied before reconciliation/rewrite, avoiding mutation of compiler/engine cache state;
  • subscription definitions are not queried against the subscriber's unrelated catalog and retain publisher-provided metadata;
  • fixed bootstrap/system-schema indexes use the explicit fixed-visible path where mo_indexes rows are intentionally absent;
  • normal/unique/spatial/fulltext/plugin CREATE paths and ALTER visibility paths all persist the tri-state marker, while catalog insertion keeps the legacy default-visible fallback;
  • reconciliation validates completeness/duplicate consistency and applies updates only after the full result has been validated.

I also checked temporary-table/index metadata, multi-row plugin indexes, snapshot table IDs, and the public/context-free formatter boundary. The context-free legacy ambiguity is unchanged from the pre-PR behavior and remains outside #27078; this PR does not regress it.

Validation on this exact head:

  • git diff --check
  • full ./pkg/catalog ./pkg/sql/plan ./pkg/sql/compile with the required CGo/link environment
  • current GitHub UT, SCA, coverage, and BVT checks are green

No blocking findings remain.

@LeftHandCold LeftHandCold left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Deep re-review completed on exact head f42e4790a7f7aa5a817bf6974b711c5e1302f484. APPROVED.

The latest scope fix closes the remaining practical regressions: local LIKE/CLONE and SHOW CREATE operate on private copies, subscription definitions no longer query the subscriber catalog, and fixed-visible system-schema indexes avoid invalid mo_indexes lookups. Normal persisted user indexes—including stale marker-bearing definitions during rolling upgrades—are still reconciled from authoritative mo_indexes.is_visible using the source snapshot and tenant, with missing/contradictory metadata failing before DDL emission.

I traced the production reconstruction callers, new index creation/ALTER persistence paths, protobuf compatibility, and catalog insertion. The context-free legacy ambiguity is pre-existing and remains explicitly outside this PR; this change does not regress its historical default-visible behavior. Current-head UT/SCA/coverage and selected BVT checks are green; diff/format/package-list checks are clean. No actionable blocker found.

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 kind/test-ci size/L Denotes a PR that changes [500,999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: table and database CLONE turn invisible indexes visible

5 participants