Skip to content

fix(plan): enforce index metadata invariants safely - #26982

Merged
XuPeng-SH merged 4 commits into
mainfrom
fix/index-visibility-compatibility
Aug 12, 2026
Merged

XuPeng-SH merged 4 commits into
mainfrom
fix/index-visibility-compatibility

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to #26972. Removing the nil-tolerant optimizer visibility filter exposed unchecked nil IndexDef dereferences, but blindly skipping nil metadata on every path is not correct.

Invariant and failure policy

Persisted engine metadata is dense: protobuf and constraint decoders allocate an IndexDef for every stored entry. A nil entry therefore represents incomplete in-memory metadata, not an optional physical index.

  • read-only discovery and display paths skip nil entries and keep processing later valid indexes; the base table remains the authoritative correctness fallback
  • mutation, locking, clone, and index-lifecycle paths fail closed before an unknown index can be omitted from maintenance, locking, copy, truncate, rename, or drop work
  • no metadata is compacted, guessed, repaired in the background, or silently normalized

What changed

Read-only closure:

  • master, regular join, vector single-row, SHOW CREATE, SHOW COLUMNS, PREPARE dependency, and index-size traversals tolerate nil entries
  • later valid indexes and plugin hidden-table dependencies remain eligible

Mutation closure:

  • one dense-metadata validator is applied at the common DML target boundary
  • ALTER, RENAME, TRUNCATE, DROP TABLE, CREATE/DROP INDEX, CREATE TABLE LIKE/CLONE, and foreign-key creation validate before lifecycle work
  • SELECT FOR UPDATE/SHARE validates before hidden-index lock planning
  • foreign-key unique-parent locking and cascaded child mutations validate related table metadata before building their mutation work

Compatibility

The 4.1-dev and 4.2-dev visibility compatibility surface remains unchanged: protobuf field 8, mo_indexes.is_visible, accepted VISIBLE/INVISIBLE syntax, ALTER behavior, and SHOW behavior are preserved. Field 14 remains reserved as established by #26972. No wire, catalog, migration, or persisted metadata changes are introduced.

Performance and unhappy paths

Read-only traversals add one constant-time nil branch per visited index. Mutation validation is a bounded O(number of indexes) planner-time scan per resolved table boundary, with no allocations on the healthy path. There is no row-level or batch-level work, catalog I/O, logging, retry loop, background task, cache, or new resource ownership.

Invalid metadata returns one deterministic internal error. It is not logged repeatedly and cannot proceed far enough to create index divergence or orphan hidden tables.

Validation

  • deterministic RED reproductions for SHOW COLUMNS, SELECT FOR UPDATE, DML/DDL mutation paths, foreign-key unique-parent locking, and cascade-child mutation
  • regressions for nil-before-valid filter, join, vector, SHOW CREATE/COLUMNS, PREPARE, and plugin dependency paths
  • INSERT, LOAD, REPLACE, UPDATE, DELETE, ALTER, RENAME, TRUNCATE, DROP, CREATE/DROP INDEX, CREATE LIKE, and CLONE fail-closed matrix
  • full pkg/sql/plan tests
  • go list, go build, and go vet for pkg/sql/plan
  • git diff --check
  • rebased onto current main at 3f2ed9b

@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 11, 2026
@XuPeng-SH
XuPeng-SH force-pushed the fix/index-visibility-compatibility branch from d517fb6 to 3d03c00 Compare August 11, 2026 21:34
@XuPeng-SH XuPeng-SH changed the title fix(plan): preserve index visibility metadata fix(plan): skip nil index metadata safely Aug 11, 2026
@matrix-meow matrix-meow added size/S Denotes a PR that changes [10,99] lines and removed size/M Denotes a PR that changes [100,499] lines labels Aug 11, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review completed for exact head 3d03c00.

Blocking / request changes:

[P1] Close the PREPARE dependency traversal over sparse IndexDef metadata

The new guards make the master-index, join-index, vector-provider, and SHOW CREATE paths tolerate nil IndexDef entries, but the same TableDef.Indexes invariant is still violated in two PREPARE traversals:

  • pkg/sql/plan/visit_plan_rule.go:107-108, GetParamRule.MatchNode
  • pkg/sql/plan/visit_plan_rule.go:137-138, recordPreparedPluginDependencies

Both dereference indexDef without first skipping nil. This is reachable through the public planner path: prepend nil before the valid index metadata for single_idx_t, then run PREPARE for select val from single_idx_t where val = ?. Exact head deterministically panics at visit_plan_rule.go:108 via BuildPlan -> buildPrepare -> resetPreparePlan -> VisitPlan.visitNode -> GetParamRule.MatchNode. An independent plugin-dependency control with [nil, valid fulltext index] also panics at line 138, so the later valid dependency is never collected.

Please skip nil in both traversals while continuing to process later valid entries, and add regressions for the public PREPARE path plus plugin dependency collection.

Validation on the exact head: the four focused changed-path tests pass; the full pkg/sql/plan test suite passes; go build and go vet for pkg/sql/plan pass. The blocker is an uncovered closure path, not a failure in the newly added tests.

GitHub does not permit REQUEST_CHANGES on a PR authored by the current account, so this review is submitted as COMMENT; the code verdict is blocking/request changes.

@XuPeng-SH

Copy link
Copy Markdown
Contributor Author

Addressed the blocking PREPARE closure finding in 323b7df: both dependency traversals now skip nil metadata while retaining later valid plugin dependencies. Added a public PREPARE regression, extended both plugin dependency tests with nil-before-valid metadata, and strengthened the join test to prove the later valid index is still used. Focused tests, full pkg/sql/plan, build, vet, and diff checks pass.

Keep sparse IndexDef metadata from panicking filter, join, vector single-row, and SHOW CREATE paths while continuing to process later valid indexes. Preserve the existing 4.1/4.2 visibility wire, catalog, DDL, and display behavior unchanged.
@XuPeng-SH XuPeng-SH changed the title fix(plan): skip nil index metadata safely fix(plan): enforce index metadata invariants safely Aug 11, 2026
@matrix-meow matrix-meow added size/M Denotes a PR that changes [100,499] lines and removed size/S Denotes a PR that changes [10,99] lines labels Aug 11, 2026

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep review completed for exact head 6501749 after rebasing onto main 3f2ed9b.

No blocking findings.

Correctness:

  • persisted IndexDef metadata remains a dense invariant; no nil entry is guessed, compacted, or silently normalized
  • read-only optimizer, SHOW, PREPARE, and diagnostic paths safely ignore an unusable entry while retaining later valid indexes and base-table correctness
  • DML, DDL, clone, lock, index lifecycle, foreign-key unique-parent, and cascade-child paths fail before an unknown index can be omitted from maintenance or ownership work
  • errors remain deterministic internal invariant failures and are not swallowed

Unhappy paths:

  • no new resource owner, cleanup edge, wait dependency, retry loop, or accumulation was introduced
  • invalid metadata cannot reach execution, so it cannot create index divergence, orphan hidden tables, or partial lifecycle work
  • no logging was added, avoiding a repeated-error log storm

Performance:

  • read paths add one constant-time branch per existing index traversal
  • mutation paths add bounded planner-time O(index count) validation with no healthy-path allocations
  • there is no per-row, per-batch, catalog-I/O, cache, goroutine, or background maintenance cost

Compatibility:

  • no protobuf, catalog, migration, or persisted metadata change
  • the 4.1-dev and 4.2-dev visibility compatibility surface remains unchanged

Validation:

  • deterministic RED coverage for SHOW COLUMNS, SELECT FOR UPDATE, cascade-child mutation, and the public DML/DDL matrix
  • nil-before-valid controls prove later valid join, vector, SHOW, and PREPARE dependencies remain usable
  • full pkg/sql/plan test suite, go list, go build, go vet, and diff checks pass

GitHub does not permit approving a PR authored by the current account, so this is submitted as COMMENT; the code verdict is approve / no blocker.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep re-review completed for exact head 6501749 (base 3f2ed9b).

Blocking / request changes:

[P1] Enforce the dense-index invariant on legacy DML fallback routes

The new validator is applied in DMLContext.resolveSingleTable, but some DML shapes choose the legacy planner before that resolver reaches a table. They then enter getDmlTableInfo / setTableExprToDmlTableInfo, which still traverses TableDef.Indexes without first validating the slice.

Two public counterexamples still deterministically SIGSEGV on this exact head after prepending nil to the valid emp index metadata:

  1. update emp left join dept on emp.deptno = dept.deptno set emp.sal = 5000

    • the modern resolver classifies the joined form as the legacy multi_target route before resolving the table
    • buildTableUpdate -> isSyncMaintainedIrregularIndex(nil) panics at pkg/sql/plan/build_update.go:37 (caller line 81)
  2. delete emp, dept from emp, dept where emp.deptno = dept.deptno

    • bindDelete returns the legacy fallback signal for multiple targets before table resolution
    • buildDelete -> buildDeleteIndexPlans -> haveUniqueKey(nil) panics at pkg/sql/plan/build_dml_util.go:2922

This violates the PR's fail-closed mutation invariant and leaves the claimed common DML boundary incomplete. Please validate every legacy DML target before the first index traversal (the shared legacy table-resolution path immediately after Resolve is one candidate, provided target/source semantics remain intentional), and add public joined-UPDATE plus multi-table-DELETE regressions that require a deterministic internal metadata error for nil-first and nil-after-valid layouts.

Other evidence on the exact head:

  • all 11 focused PR tests pass, including the 14-operation single-table mutation matrix, FK unique-parent/cascade cases, SHOW, PREPARE, and nil-before-valid read controls
  • full pkg/sql/plan tests pass; go list, go build, go vet, and git diff --check pass
  • transplanting the new tests (without the new call-site guards) onto parent cc39bb1 reproduces the intended RED panics; the two fallback counterexamples above expose the remaining closure gap

GitHub does not permit REQUEST_CHANGES on a PR authored by the current account, so this is submitted as COMMENT; the code verdict is blocking / request changes.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deep re-review completed for exact head 50df06a (base 3f2ed9b).

No blocking findings.

Correctness:

  • the legacy DML fallback now enforces the dense IndexDef invariant immediately after table resolution and before its first index traversal
  • this common boundary dominates legacy INSERT, LOAD, REPLACE, UPDATE, and DELETE metadata consumers, including joined UPDATE and multi-table DELETE fallback routes
  • malformed metadata fails with the same deterministic internal invariant error; it is not compacted, guessed, or silently omitted from index maintenance
  • public nil-first and nil-after-valid regressions cover both fallback shapes

Counterexample evidence:

  • on parent head 6501749, joined UPDATE SIGSEGVs in isSyncMaintainedIrregularIndex(nil)
  • on the same head, multi-table DELETE SIGSEGVs in haveUniqueKey(nil), with the nil-after-valid case also reaching a later delete-index dereference
  • on this head, all four cases return the expected internal metadata error

Unhappy paths:

  • Q1 ownership: no resource, cleanup owner, or ownership transfer is introduced
  • Q2 termination: no lock, channel, RPC, retry, goroutine, or wait edge is introduced
  • Q3 bounded growth: no retained collection, cache, queue, or background state is introduced
  • failure occurs during planning before mutation work can be built or executed

Performance and compatibility:

  • healthy metadata pays one bounded O(index count), zero-allocation planning-time scan per resolved legacy table
  • there is no row, batch, storage-I/O, cache, logging, or background-maintenance cost
  • protobuf, catalog, persisted metadata, SQL syntax, and 4.1-dev / 4.2-dev visibility compatibility are unchanged

Validation on the exact code:

  • focused sparse-metadata matrix passes
  • full pkg/sql/plan passes
  • GOWORK=off go list, go build, and go vet for pkg/sql/plan pass
  • git diff --check passes and the worktree is clean

GitHub does not permit approving a PR authored by the current account, so this is submitted as COMMENT; the code verdict is approve / no blocker. CI has started, but per instruction this review does not wait for it.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Independent deep re-review completed for exact head 50df06a (base and merge-base 3f2ed9b).

No blocking findings.

Previous blocker closure:

  • the legacy DML resolver now validates dense IndexDef metadata immediately after Resolve and before its first index traversal
  • that boundary recursively covers joined tables and dominates legacy INSERT, LOAD, REPLACE, UPDATE, and DELETE consumers
  • joined UPDATE and multi-table DELETE are covered with both nil-first and nil-after-valid layouts
  • the same four public cases deterministically SIGSEGV on parent 6501749 and return the expected internal metadata error on this head

Full functional closure:

  • read-only optimizer, SHOW, PREPARE, and diagnostic paths skip an unusable entry while retaining later valid indexes and base-table correctness
  • mutation, DDL, clone, hidden-index locking, foreign-key unique-parent, and cascade-child paths fail before incomplete metadata can omit maintenance or lifecycle work
  • metadata is neither compacted nor guessed; protobuf/catalog persistence and the visibility compatibility surface are unchanged

Unhappy paths and performance:

  • Q1: no resource creation, ownership transfer, or cleanup edge was added
  • Q2: no lock, channel, RPC, goroutine, retry, or wait dependency was added
  • Q3: no retained collection, cache, queue, or unbounded state was added
  • healthy inputs pay only bounded planner-time O(index count) scans, with no row/batch/storage-I/O or healthy-path allocation cost

Fresh validation on the exact head:

  • the focused selection is non-empty and all 12 focused tests pass, including the 14-operation mutation matrix and the four legacy fallback regressions
  • full pkg/sql/plan passes with -count=1 -timeout 120s
  • GOWORK=off go list, go build, and go vet for pkg/sql/plan pass
  • git diff --check passes; the isolated candidate worktree is clean
  • CI currently has 10 passed and 7 pending checks, with no failures

GitHub does not permit approving a PR authored by the current account, so this is submitted as COMMENT; the code verdict is approve / no blocker.

@XuPeng-SH
XuPeng-SH merged commit 9a7c98b into main Aug 12, 2026
32 checks passed
@XuPeng-SH
XuPeng-SH deleted the fix/index-visibility-compatibility branch August 12, 2026 01:53
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Follow-up to matrixorigin#26972. Removing the nil-tolerant optimizer visibility
filter exposed unchecked nil IndexDef dereferences, but blindly skipping
nil metadata on every path is not correct.

## Invariant and failure policy

Persisted engine metadata is dense: protobuf and constraint decoders
allocate an IndexDef for every stored entry. A nil entry therefore
represents incomplete in-memory metadata, not an optional physical
index.

- read-only discovery and display paths skip nil entries and keep
processing later valid indexes; the base table remains the authoritative
correctness fallback
- mutation, locking, clone, and index-lifecycle paths fail closed before
an unknown index can be omitted from maintenance, locking, copy,
truncate, rename, or drop work
- no metadata is compacted, guessed, repaired in the background, or
silently normalized

## What changed

Read-only closure:
- master, regular join, vector single-row, SHOW CREATE, SHOW COLUMNS,
PREPARE dependency, and index-size traversals tolerate nil entries
- later valid indexes and plugin hidden-table dependencies remain
eligible

Mutation closure:
- one dense-metadata validator is applied at the common DML target
boundary
- ALTER, RENAME, TRUNCATE, DROP TABLE, CREATE/DROP INDEX, CREATE TABLE
LIKE/CLONE, and foreign-key creation validate before lifecycle work
- SELECT FOR UPDATE/SHARE validates before hidden-index lock planning
- foreign-key unique-parent locking and cascaded child mutations
validate related table metadata before building their mutation work

## Compatibility

The 4.1-dev and 4.2-dev visibility compatibility surface remains
unchanged: protobuf field 8, mo_indexes.is_visible, accepted
VISIBLE/INVISIBLE syntax, ALTER behavior, and SHOW behavior are
preserved. Field 14 remains reserved as established by matrixorigin#26972. No wire,
catalog, migration, or persisted metadata changes are introduced.

## Performance and unhappy paths

Read-only traversals add one constant-time nil branch per visited index.
Mutation validation is a bounded O(number of indexes) planner-time scan
per resolved table boundary, with no allocations on the healthy path.
There is no row-level or batch-level work, catalog I/O, logging, retry
loop, background task, cache, or new resource ownership.

Invalid metadata returns one deterministic internal error. It is not
logged repeatedly and cannot proceed far enough to create index
divergence or orphan hidden tables.

## Validation

- deterministic RED reproductions for SHOW COLUMNS, SELECT FOR UPDATE,
DML/DDL mutation paths, foreign-key unique-parent locking, and
cascade-child mutation
- regressions for nil-before-valid filter, join, vector, SHOW
CREATE/COLUMNS, PREPARE, and plugin dependency paths
- INSERT, LOAD, REPLACE, UPDATE, DELETE, ALTER, RENAME, TRUNCATE, DROP,
CREATE/DROP INDEX, CREATE LIKE, and CLONE fail-closed matrix
- full pkg/sql/plan tests
- go list, go build, and go vet for pkg/sql/plan
- git diff --check
- rebased onto current main at 3f2ed9b
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Follow-up to matrixorigin#26972. Removing the nil-tolerant optimizer visibility
filter exposed unchecked nil IndexDef dereferences, but blindly skipping
nil metadata on every path is not correct.

## Invariant and failure policy

Persisted engine metadata is dense: protobuf and constraint decoders
allocate an IndexDef for every stored entry. A nil entry therefore
represents incomplete in-memory metadata, not an optional physical
index.

- read-only discovery and display paths skip nil entries and keep
processing later valid indexes; the base table remains the authoritative
correctness fallback
- mutation, locking, clone, and index-lifecycle paths fail closed before
an unknown index can be omitted from maintenance, locking, copy,
truncate, rename, or drop work
- no metadata is compacted, guessed, repaired in the background, or
silently normalized

## What changed

Read-only closure:
- master, regular join, vector single-row, SHOW CREATE, SHOW COLUMNS,
PREPARE dependency, and index-size traversals tolerate nil entries
- later valid indexes and plugin hidden-table dependencies remain
eligible

Mutation closure:
- one dense-metadata validator is applied at the common DML target
boundary
- ALTER, RENAME, TRUNCATE, DROP TABLE, CREATE/DROP INDEX, CREATE TABLE
LIKE/CLONE, and foreign-key creation validate before lifecycle work
- SELECT FOR UPDATE/SHARE validates before hidden-index lock planning
- foreign-key unique-parent locking and cascaded child mutations
validate related table metadata before building their mutation work

## Compatibility

The 4.1-dev and 4.2-dev visibility compatibility surface remains
unchanged: protobuf field 8, mo_indexes.is_visible, accepted
VISIBLE/INVISIBLE syntax, ALTER behavior, and SHOW behavior are
preserved. Field 14 remains reserved as established by matrixorigin#26972. No wire,
catalog, migration, or persisted metadata changes are introduced.

## Performance and unhappy paths

Read-only traversals add one constant-time nil branch per visited index.
Mutation validation is a bounded O(number of indexes) planner-time scan
per resolved table boundary, with no allocations on the healthy path.
There is no row-level or batch-level work, catalog I/O, logging, retry
loop, background task, cache, or new resource ownership.

Invalid metadata returns one deterministic internal error. It is not
logged repeatedly and cannot proceed far enough to create index
divergence or orphan hidden tables.

## Validation

- deterministic RED reproductions for SHOW COLUMNS, SELECT FOR UPDATE,
DML/DDL mutation paths, foreign-key unique-parent locking, and
cascade-child mutation
- regressions for nil-before-valid filter, join, vector, SHOW
CREATE/COLUMNS, PREPARE, and plugin dependency paths
- INSERT, LOAD, REPLACE, UPDATE, DELETE, ALTER, RENAME, TRUNCATE, DROP,
CREATE/DROP INDEX, CREATE LIKE, and CLONE fail-closed matrix
- full pkg/sql/plan tests
- go list, go build, and go vet for pkg/sql/plan
- git diff --check
- rebased onto current main at 3f2ed9b
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
Follow-up to matrixorigin#26972. Removing the nil-tolerant optimizer visibility
filter exposed unchecked nil IndexDef dereferences, but blindly skipping
nil metadata on every path is not correct.

## Invariant and failure policy

Persisted engine metadata is dense: protobuf and constraint decoders
allocate an IndexDef for every stored entry. A nil entry therefore
represents incomplete in-memory metadata, not an optional physical
index.

- read-only discovery and display paths skip nil entries and keep
processing later valid indexes; the base table remains the authoritative
correctness fallback
- mutation, locking, clone, and index-lifecycle paths fail closed before
an unknown index can be omitted from maintenance, locking, copy,
truncate, rename, or drop work
- no metadata is compacted, guessed, repaired in the background, or
silently normalized

## What changed

Read-only closure:
- master, regular join, vector single-row, SHOW CREATE, SHOW COLUMNS,
PREPARE dependency, and index-size traversals tolerate nil entries
- later valid indexes and plugin hidden-table dependencies remain
eligible

Mutation closure:
- one dense-metadata validator is applied at the common DML target
boundary
- ALTER, RENAME, TRUNCATE, DROP TABLE, CREATE/DROP INDEX, CREATE TABLE
LIKE/CLONE, and foreign-key creation validate before lifecycle work
- SELECT FOR UPDATE/SHARE validates before hidden-index lock planning
- foreign-key unique-parent locking and cascaded child mutations
validate related table metadata before building their mutation work

## Compatibility

The 4.1-dev and 4.2-dev visibility compatibility surface remains
unchanged: protobuf field 8, mo_indexes.is_visible, accepted
VISIBLE/INVISIBLE syntax, ALTER behavior, and SHOW behavior are
preserved. Field 14 remains reserved as established by matrixorigin#26972. No wire,
catalog, migration, or persisted metadata changes are introduced.

## Performance and unhappy paths

Read-only traversals add one constant-time nil branch per visited index.
Mutation validation is a bounded O(number of indexes) planner-time scan
per resolved table boundary, with no allocations on the healthy path.
There is no row-level or batch-level work, catalog I/O, logging, retry
loop, background task, cache, or new resource ownership.

Invalid metadata returns one deterministic internal error. It is not
logged repeatedly and cannot proceed far enough to create index
divergence or orphan hidden tables.

## Validation

- deterministic RED reproductions for SHOW COLUMNS, SELECT FOR UPDATE,
DML/DDL mutation paths, foreign-key unique-parent locking, and
cascade-child mutation
- regressions for nil-before-valid filter, join, vector, SHOW
CREATE/COLUMNS, PREPARE, and plugin dependency paths
- INSERT, LOAD, REPLACE, UPDATE, DELETE, ALTER, RENAME, TRUNCATE, DROP,
CREATE/DROP INDEX, CREATE LIKE, and CLONE fail-closed matrix
- full pkg/sql/plan tests
- go list, go build, and go vet for pkg/sql/plan
- git diff --check
- rebased onto current main at 3f2ed9b

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/M Denotes a PR that changes [100,499] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants