Skip to content

[Bug]: DELETE/UPDATE followed by COPY ALTER in one transaction leaves stale secondary-index entries #32247

Closed

Description

@iamlinjunhong

Is there an existing issue for the same bug?

  • I have checked the existing issues.

Searched open and closed reports for ALTER, index, DELETE, stale/orphan entries, clone and tombstones. The closest reports describe different sequences: #32056 changes PK types; #30335 concerns async FULLTEXT2 DELETE after COPY ALTER; #27878 is a merge/Rowid-transfer failure that rolls back; #29540 concerns snapshot advancement across TN relocation. This report needs none of those: synchronous ordinary/unique indexes, no PK change, one session, successful DML followed by one COPY ALTER in the same transaction. Related investigation tracker: #32245.

Branch Name

main and 4.2-dev

Commit ID

Locally built and reproduced on both fixed revisions:

  • main: 1d51125661c60ff5216c1216a897a3d312875336
  • 4.2-dev: 58edabfe8f9f8a6c126e0634e7246c031a6f0ade

Other Environment Information

macOS arm64, Go 1.27.1, local launch with 2 CN / 1 TN / 1 LogService. The reproducer uses one CN and one SQL session; a new connection confirms the state after COMMIT. Ordinary MySQL text protocol, synthetic non-NULL data, no source changes and no writes to hidden index tables.

Actual Behavior

An indexed DELETE or UPDATE correctly maintains the indexes initially. A subsequent unrelated-column COPY ALTER in the same explicit transaction succeeds but resurrects old hidden-index entries. COMMIT succeeds and the inconsistency remains visible from a new connection.

Both tested branches give the same results for each ordinary ix(k) and unique ux(u) index:

Sequence Base rows Hidden rows per index Key-level result at this stage
DELETE, before ALTER 2 2 consistent
DELETE, then one ALTER 2 3 one orphan entry for deleted id=2; missing=0
UPDATE, before ALTER 3 3 consistent
UPDATE, then one ALTER 3 4 old and new keys both refer to id=1; missing=0

For the UPDATE case, one stale entry also contributes one duplicate origin-PK reference; these overlap and represent one extra physical row, not two. This is not a duplicate encoded index key.

Expected Behavior

COPY ALTER must preserve the transaction-visible live index contents. Rows deleted earlier in the transaction must not return in cloned indexes. After UPDATE, only the new index keys may reference the updated row. Successful COMMIT must not leave the base and its synchronous secondary indexes inconsistent.

Steps to Reproduce

Run only in a disposable test database. No concurrency or manual hidden-index corruption is needed.

DELETE reproducer:

CREATE TABLE deleted (
  id INT PRIMARY KEY,
  k INT NOT NULL,
  u INT NOT NULL,
  n INT NOT NULL,
  body TEXT,
  KEY ix(k),
  UNIQUE KEY ux(u)
);
INSERT INTO deleted VALUES
  (1,10,100,0,'a'),(2,20,200,0,'b'),(3,30,300,0,'c');

BEGIN;
DELETE FROM deleted WHERE id=2;
-- Both indexes are still consistent here (2 live entries each).
ALTER TABLE deleted MODIFY body LONGTEXT;
COMMIT;

SELECT id,k,u FROM deleted IGNORE INDEX(ix,ux) ORDER BY id;
-- (1,10,100), (3,30,300)

Independent UPDATE reproducer:

CREATE TABLE updated (
  id INT PRIMARY KEY,
  k INT NOT NULL,
  u INT NOT NULL,
  n INT NOT NULL,
  body TEXT,
  KEY ix(k),
  UNIQUE KEY ux(u)
);
INSERT INTO updated VALUES
  (1,10,100,0,'a'),(2,20,200,0,'b'),(3,30,300,0,'c');

BEGIN;
UPDATE updated SET k=11,u=101 WHERE id=1;
-- Both indexes are still consistent here (3 live entries each).
ALTER TABLE updated MODIFY body LONGTEXT;
COMMIT;

SELECT id,k,u FROM updated IGNORE INDEX(ix,ux) ORDER BY id;
-- (1,11,101), (2,20,200), (3,30,300)

After COMMIT, reconnect to the same database and resolve the replacement hidden tables; do not reuse their pre-ALTER names:

SELECT DISTINCT t.relname,i.name,i.index_table_name
FROM mo_catalog.mo_indexes i
JOIN mo_catalog.mo_tables t ON i.table_id=t.rel_id
WHERE t.reldatabase=DATABASE()
  AND t.relname IN ('deleted','updated')
  AND i.name IN ('ix','ux')
ORDER BY t.relname,i.name;

For each returned hidden name, read __mo_index_pri_col and __mo_index_idx_col directly. For ordinary ix, use HEX(__mo_index_idx_col) and compare with HEX(serial_full(k,id)) from a base scan. For unique ux, the raw key is the integer u. These reads were performed both before and after ALTER, and again on a new connection after COMMIT.

Actual unique-index contents after COMMIT:

-- columns: __mo_index_pri_col, __mo_index_idx_col
-- deleted.ux (should have only id=1 and id=3):
1  100
2  200  <-- orphan; base id=2 is absent
3  300

-- updated.ux (should have only key 101 for id=1):
1  100  <-- stale
1  101
2  200
3  300

Ordinary ix shows the corresponding extra encoded (k,id) key: (20,2) after DELETE and (10,1) after UPDATE. All current base rows still have matching entries.

Additional information

  • Reproduced using one ALTER on both branches, including a clean same-transaction read immediately before ALTER and a fresh connection after COMMIT. A separate combined UPDATE/DELETE/INSERT + two-ALTER sequence also reproduced; later inserted rows were indexed correctly while old entries remained.
  • This differs from the missing-only historical cohort tracked in [Bug]: Investigate remaining causes of secondary index inconsistency #32245. It must not be cited as that cohort's confirmed cause.
  • The sampled public FORCE/IGNORE range queries returned the same rows. This report proves persistent physical/logical index inconsistency; it does not claim those particular SELECTs produced wrong rows or that uniqueness errors have been reproduced.
  • Investigation lead, not yet a fully proven runtime root cause: TableMetaReader.collectDataOfAObjsAndInMem uses Policy_SkipUncommitedInMemory while rewriting appendable/in-memory data to new objects; the tombstone phase collects original Rowids. Check whether copied transaction tombstones still target the rewritten data. The code does collect tombstones, so this is not a claim that tombstones are simply never copied.
  • fix table clone visibility for repeated alter #24987's transaction-visible clone fix is already present in both revisions. Its presence does not prevent this new minimal counterexample.

Suggested regression boundary: separate DELETE-only and indexed-UPDATE cases in an explicit transaction, one unrelated-column COPY ALTER, raw base/index key comparison before ALTER and after successful COMMIT, fresh-connection confirmation, and appendable versus persisted-object controls. Verify both ordinary and unique indexes on main and 4.2-dev.

Activity

  1. added
    severity/s0
    on Nov 5, 2026
  2. iamlinjunhong commented on Nov 5, 2026

    @iamlinjunhong
    ContributorAuthor

    Additional local controls on the same two exact revisions in the issue (main 1d51125661c6, 4.2-dev 58edabfe8f9f), with synthetic data and no source changes:

    Control Result on both branches
    Commit the DELETE before the unrelated-column COPY ALTER Base and both indexes have the same two live rows.
    In one transaction, DELETE id=2, then ALTER TABLE t MODIFY k BIGINT NOT NULL Ordinary ix(k) is rebuilt and correct (2 entries); unaffected unique ux(u) is cloned and retains id=2 (3 entries). The base has 2 rows.
    Capture __mo_rowid around DELETE and unrelated-column COPY ALTER id=2 disappears from both hidden indexes immediately after DELETE, then reappears after ALTER/COMMIT under a different physical Rowid in each replacement hidden table.

    The planner/clone logs agree with the within-operation control: only ux is selected for cloning when k changes. This localizes the confirmed defect to preserving transaction-local deletes while cloning unaffected index contents; it does not require a PK type change or concurrent DML.

    The Rowid observation strengthens the proposed rewrite/tombstone explanation, but the actual persisted tombstone targets have not been decoded, so a complete runtime mechanism is still not claimed. The code explicitly collects tombstones; the question is whether their original Rowids can delete relocated data. No source fix has been made.

  3. XuPeng-SH commented on Nov 6, 2026

    @XuPeng-SH
    Contributor

    QA 交接(main,待验证)

    PR #32268 已合入 main,merge SHA 4ffcfba267fee3e6d89e445b962d05b99655fe9c。clone materialization 重写行前应用既有 workspace delete 检查,避免同事务 DELETE/UPDATE 后 COPY ALTER 重新带入旧行/索引项;保留 shared objects/tombstones,immutable-only metadata clone 不变。

    已读完整 issue 评论及 PR 讨论。原 main 1d51125661...、4.2 58edabfe... 上的反例早于修复,未发现当前合并版本适用的已验证反例或未完成的本 issue 开发 follow-up。

    证据为作者报告的 visibility/cleanup normal/race、SQL 156/156 两轮、clone 2657/2657、PK-index control 52/52及跨 CN 新连接键值读取。审查在较早 head 1801411237... 复用 CI/作者证据,未新增 Go/SQL 执行;合并队列记录 CI Required 成功。最终 head 与审查 head 不同,不把旧报告冒称本轮实测。本轮没有本地测试。

    请 @Ariznawlll 在含该 merge 的 main 分别复测 DELETE-only、indexed UPDATE 后同事务一次无关列 COPY ALTER,ALTER 前/COMMIT 后/新连接及另一 CN 做完整 base/index 键值对比,覆盖普通/唯一索引、commit-before-ALTER、重建与克隆并存、appendable/持久化及 shared/rewritten 对照。重点补验真实 appendable-file+tombstone 组合的 target relation 读取,此前只具组件级证据;另验证失败/清理及性能边界。

    转测不代表验收通过。此修复未证明或修复 #32245 历史 missing-only 起因/损坏,不将物理索引不一致夸大为已证实的错 SELECT、唯一约束错误或主表丢失。本次仅交 main,不自动要求 4.2 backport。

  4. slyunnn commented on Nov 9, 2026

    @slyunnn

    Regression and extended verification for #32247 (2026-11-09)

    Verification Environment

    • Frozen MatrixOne origin/main at test start, e2459cc55f4b514893e19732743460bc5f16d552 — docs: design early runtime filtering and dictionary vector execution (#32380).
    • macOS arm64, native topology with 2 CN / 1 TN / 1 Log service / Proxy, co-located through the native launcher. Runtime git_version() was checked against the built source.
    • Actual regression SQL ran over TCP through Proxy 127.0.0.1:6001, CN1 127.0.0.1:16001 and CN2 127.0.0.1:16002. PyMySQL used COM_QUERY. Existing BVT used JDBC. Component tests linked matching native libraries.

    What Was Covered

    • OK — Replayed separate DELETE and indexed UPDATE operations followed by same-transaction COPY ALTER. Re-resolved hidden-index names after every ALTER.
    • OK — Compared complete base and ordinary/unique hidden-index key pairs before ALTER, after ALTER, after COMMIT, and from fresh connections through every endpoint. Lookup controls also used FORCE INDEX.
    • OK — Passed 45 layout/operation combinations spanning memory, TN-flushed and forced CN-spill fixtures, with rollback, commit-before-ALTER, indexed-column rebuild, duplicate ordinary keys and NULL controls.
    • OK — Passed 6 additional old-snapshot scenarios. An independent connection physically flushed the source and its indexes after the snapshot was established, with an existing committed tombstone, before same-transaction DML/ALTER and fresh cross-CN target reads.
    • OK — Passed real appendable-format/shared-object component consumers, cancellation, post-persist failure cleanup and focused race tests.
    • OK — Compared the existing clone-visibility BVT golden, 156/156 statements through each endpoint.
    • Not covered — Production-scale clone throughput and the independent historical-corruption scope of #32245.

    Findings

    Fix 4ffcfba267fee3e6d89e445b962d05b99655fe9c, Fix transaction delete visibility during clone materialization, is merged through PR #32268. The tested target relations retained only transaction-visible rows and index entries across all verified layouts and observer connections. Fix PR

    MOTR / BVT Coverage

    Reused git4data/clone/clone_transaction_visibility.sql and the owning materialization/cleanup consumers. The old-snapshot flush and cross-CN checks supplement existing coverage. Fixtures ran sequentially because fault controls and flush ordering are shared execution state. No duplicate MOTR case was added.

    Final Conclusion

    Fixed on the tested main build.

    Large-data regression is unnecessary for this transaction-visibility defect. Minimal rows directly covered ordinary/unique keys, persisted snapshots, tombstones and target-relation reads.

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

Metadata

Metadata

Assignees

Labels

kind/bugneeds-triageseverity/s0

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions