Skip to content

fix(disttae): transfer tombstones only for objects deleted in window - #27879

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-transfer-rowid-pruning
Aug 31, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
XuPeng-SH:codex/fix-transfer-rowid-pruning

Conversation

@XuPeng-SH

@XuPeng-SH XuPeng-SH commented Aug 30, 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:

issue #27878

What this PR does / why we need it:

CN tombstone transfer previously used absence from PartitionState's persisted-object index as evidence that an object had been deleted. Live appendable objects are intentionally absent from that index, so their tombstones could be staged for transfer even though the source object was still alive. Since the replacement-object list only contains objects created for objects actually deleted in the transfer window, RowID lookup then failed its cardinality invariant and rolled back large indexed DML.

This PR:

  • classifies a tombstone for transfer only when its source ObjectID belongs to the exact deletedObjects set returned for the current (start, end] window;
  • makes both in-memory and persisted transfer paths use the same (start, end] transition window: objects created exactly at start are already visible snapshot state, while a later delete of that object must still be transferred;
  • removes the unsafe absent-means-deleted helper;
  • preserves existing ZoneMap/Bloom pruning and the cardinality guard, with no fallback or global scan;
  • keeps PK-conflict object discovery on the same (from, end] contract without advancing from twice, so an object transition committed at the valid HLC timestamp from.Next() is not skipped;
  • amortizes the existing RowID lookup setup over at most eight source blocks, with an 8 MiB soft size limit. Table processing and each transaction's transfer path remain serial: no goroutine, worker pool, or additional concurrent S3 I/O is introduced. With N concurrent transactions the node still has N serial transfer flows, not 2N per-transaction workers;
  • adds mixed live/deleted, previous-window, zero-width-window, exact-HLC-successor, and lifecycle-boundary coverage.

The set is allocated only after the coarse tombstone filter finds affected work and changes per-row classification to O(1) membership.

QA test plan

Run on a fresh standalone instance and an empty data directory. Execute the statements continuously so the final insert/UPDATE overlaps the normal background object merges, matching the original 100%-repro procedure:

drop database if exists transfer_rowid_qa;
create database transfer_rowid_qa;
use transfer_rowid_qa;

create table write_100m (
  id bigint primary key,
  col3 int,
  col4 bigint,
  key(col3),
  unique key(col4)
);

insert into write_100m values (1, 1, 1);
insert into write_100m select id + 1,        col3, col4 + 1        from write_100m;
insert into write_100m select id + 2,        col3, col4 + 2        from write_100m;
insert into write_100m select id + 4,        col3, col4 + 4        from write_100m;
insert into write_100m select id + 8,        col3, col4 + 8        from write_100m;
insert into write_100m select id + 16,       col3, col4 + 16       from write_100m;
insert into write_100m select id + 32,       col3, col4 + 32       from write_100m;
insert into write_100m select id + 64,       col3, col4 + 64       from write_100m;
insert into write_100m select id + 128,      col3, col4 + 128      from write_100m;
insert into write_100m select id + 256,      col3, col4 + 256      from write_100m;
insert into write_100m select id + 512,      col3, col4 + 512      from write_100m;
insert into write_100m select id + 1024,     col3, col4 + 1024     from write_100m;
insert into write_100m select id + 2048,     col3, col4 + 2048     from write_100m;
insert into write_100m select id + 4096,     col3, col4 + 4096     from write_100m;
insert into write_100m select id + 8192,     col3, col4 + 8192     from write_100m;
insert into write_100m select id + 16384,    col3, col4 + 16384    from write_100m;
insert into write_100m select id + 32768,    col3, col4 + 32768    from write_100m;
insert into write_100m select id + 65536,    col3, col4 + 65536    from write_100m;
insert into write_100m select id + 131072,   col3, col4 + 131072   from write_100m;
insert into write_100m select id + 262144,   col3, col4 + 262144   from write_100m;
insert into write_100m select id + 524288,   col3, col4 + 524288   from write_100m;
insert into write_100m select id + 1048576,  col3, col4 + 1048576  from write_100m;
insert into write_100m select id + 2097152,  col3, col4 + 2097152  from write_100m;
insert into write_100m select id + 4194304,  col3, col4 + 4194304  from write_100m;
insert into write_100m select id + 8388608,  col3, col4 + 8388608  from write_100m;
insert into write_100m select id + 16777216, col3, col4 + 16777216 from write_100m;
insert into write_100m
  select id + 33554432, col3, col4 + 33554432
  from write_100m where id <= 16445568;

select count(*) from write_100m; -- 50000000
update write_100m set col3 = col3 + 1;

select count(*), min(id), max(id), min(col3), max(col3),
       sum(id), sum(col4),
       sum(if(id <> col4 or col3 <> 2, 1, 0)) as invalid_rows
from write_100m;
-- 50000000, 1, 50000000, 2, 2,
-- 1250000025000000, 1250000025000000, 0

explain select * from write_100m force index(col3) where col3 = 2 limit 10;
select count(*) from write_100m force index(col3) where col3 = 2; -- 50000000
select count(*) from write_100m force index(col3) where col3 = 1; -- 0
select * from write_100m where col4 in (1, 8192, 8388608, 33554432, 50000000)
order by col4;

Acceptance:

  1. The UPDATE commits without TRANSFER-ROWIDS-ERROR-LEN-MISMATCH, PK mismatch, retry exhaustion, or transaction rollback.
  2. All exact SQL oracles above match, and EXPLAIN contains Index Table Scan on write_100m.col3 plus Join Type: INDEX.
  3. Stop the service cleanly, restart it on the same data directory, and rerun the final aggregate, forced-secondary-index counts, and unique-index point lookups; results must remain identical.
  4. For performance, record UPDATE client wall time and each CN-TRANSFER-TOMBSTONE-OBJ time-spent on the same host/configuration. Compare with this PR's pre-optimization baseline: UPDATE 232 s end-to-end, base-table transfer 57 s, hidden-secondary-index transfer 107 s. Performance is judged by same-machine before/after comparison, not an absolute CI timeout.

Validation:

  • go test ./pkg/vm/engine/disttae/logtailreplay ./pkg/vm/engine/disttae -count=1
  • focused logtailreplay window tests and disttae transfer tests passed 10 times under -race;
  • the root regression now exercises HandleObjectEntry (live appendable omitted from the persisted index), real window object collection, and mixed-row staging in one deterministic chain; restoring the old absent-means-deleted behavior makes it fail with 2 staged rows instead of 1;
  • make build;
  • lookup microbenchmark: approximately 6.2–6.7 ns/op with 0 B/op and 0 allocs/op; building a 4096-object set takes approximately 52 us and 164 KB once per actual transfer;
  • exact original 100%-repro 50M black-box procedure on a fresh process and fresh data directory at 160d8ca207: create the same primary/secondary/unique-indexed table, use the same doubling inserts plus the final id <= 16445568 insert to reach exactly 50,000,000 rows, then run the same full-table UPDATE col3 = col3 + 1 while base/index merges replace objects. It committed without RowID mismatch or rollback. Final oracle: count(*)=50000000, min(id)=1, max(id)=50000000, min(col3)=max(col3)=2. Transfer evidence: base table transferred 33,222,784 rows from 20 deleted objects; hidden secondary index transferred 35,704,960 rows from 23 deleted objects.
  • post-commit persistence and index-integrity verification after two clean service restarts: the base table has no row where id != col4 or col3 != 2, and sum(id)=sum(col4)=1250000025000000; the hidden unique-index table has exactly 50,000,000 rows, key/ref range 1..50000000, equal sums, and zero key/ref mismatches; the hidden secondary-index table has exactly 50,000,000 rows with the same ref range and sum, zero dangling refs, and zero base rows missing an index ref. A forced secondary-index plan returned 50,000,000 rows for col3=2 and zero for col3=1.
  • bounded batching optimization at caf968f0f3 (executable production code identical; the only post-build change was a comment), using the same fresh-process/fresh-data 50M procedure: UPDATE wall time improved from 232 s to 203.127 s (-12.5%); base-table transfer improved from 57.166 s to 41.066 s (-28.2%); hidden-secondary-index transfer improved from 107.035 s to 95.839 s (-10.5%). All exact aggregate and forced-index oracles passed with no mismatch or rollback. The full script, including loading and validation queries, took 4m00.64s versus 4m24.22s before batching.

@XuPeng-SH
XuPeng-SH requested a review from gouhongshen as a code owner August 30, 2026 23:44
@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

No code findings. Existing reviews and inline comments are empty; the sole PR comment is an unrelated bot notice. Focused CGo tests could not start because the checkout lacks cgo/libmo.dylib.

XuPeng-SH added a commit that referenced this pull request Aug 31, 2026
…27880)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue #27878

Backport of #27879 to `4.2-dev`.

## What this PR does / why we need it:

CN tombstone transfer must move a RowID only when its source object
belongs to the exact set deleted in the current `(start, end]` window.
Absence from the persisted-object index is not evidence of deletion
because live appendable objects are intentionally absent. Violating this
invariant can make large indexed DML fail commit-time RowID cardinality
validation and roll back.

This backport:

- uses exact deleted-object membership for tombstone transfer;
- aligns in-memory and persisted transfer windows to `(start, end]`;
- preserves the same boundary for PK-conflict object discovery,
including an object transition committed at `from.Next()`;
- keeps ZoneMap/Bloom pruning and the cardinality guard;
- batches at most eight source blocks with an 8 MiB soft limit, without
adding goroutines, worker pools, or concurrent S3 I/O.

The three commits were cherry-picked cleanly from #27879. Their stable
patch IDs match the source commits exactly; no 4.2-specific
production-code adaptation was needed.

## Validation

### Deterministic tests and build

- Exact selected regression tests were listed and executed on the
backport head:
  - `TestPartitionState_TransferObjectWindowBoundaries`
  - `TestDeletedObjectFilterUsesExactTransferWindow`
  - `TestTransferBatchLimit`
  - `TestTransferFlowBatchesAcrossObjectBlocks`
  - `TestTransferFlowStagesOnlyObjectsDeletedInWindow`
- CGo-controlled owning-package tests passed:
  - `./pkg/vm/engine/disttae/logtailreplay`
  - `./pkg/vm/engine/disttae`
- `make build-with-prebuilt-native` passed with `libmo.dylib` built from
the `4.2-dev` worktree's own source.

### Real 50M black-box QA on this backport head

Validated commit `235ad329ce37a4dbc3c3da1847ebe7a7855f0164` with a newly
built `mo-service`, a fresh data directory, and an isolated SQL port.
The database was populated from scratch; no prior data was reused.

QA procedure:

1. Create one table with a BIGINT primary key, an indexed `col3`, and a
unique indexed `col4`.
2. Populate exactly 50,000,000 rows by doubling inserts plus the final
bounded insert.
3. Execute `UPDATE write_100m SET col3 = col3 + 1` so the update
rewrites every base row and its secondary-index entry while background
merges can replace source objects.
4. Check the base-table count/range/checksums and every row predicate.
5. Check public secondary-index predicates and unique-index point
lookups.
6. Query both generated hidden index tables directly for index
cardinality.
7. Stop the service cleanly, restart a new process on the same data
directory, and repeat all correctness/index checks.

Observed results:

- Full fresh-data load + UPDATE + first verification: **214.15s**.
- 50M-row UPDATE: **162.644668s**.
- Base table before and after restart:
  - rows: `50,000,000`
  - `id` range: `1..50,000,000`
  - `col3` range: `2..2`
  - `sum(id) = sum(col4) = 1,250,000,025,000,000`
  - rows violating `id = col4 AND col3 = 2`: `0`
- Secondary-index predicates before and after restart:
  - `col3 = 2`: `50,000,000`
  - `col3 = 1`: `0`
- Direct hidden-index cardinality after restart:
  - secondary index table: `50,000,000`
  - unique index table: `50,000,000`, key range `1..50,000,000`
- Unique-index point lookups for `1`, `8192`, `8388608`, `33554432`, and
`50000000` returned the exact expected rows after restart.
- Restarted verification completed in **6.60s**.
- Transfer phases observed on the updated base/index objects completed
in **33.99s** and **90.76s**.
- No `TRANSFER-ROWIDS-ERROR`, panic, or fatal log was emitted.

No BVT is added: the public failure requires large data plus
background-merge timing, while the violated object-lifecycle and
exact-window contracts are covered deterministically by the focused
tests. The real 50M procedure above is the scale/regression acceptance
evidence for this backport.
@XuPeng-SH
XuPeng-SH merged commit b870113 into matrixorigin:main Aug 31, 2026
68 of 72 checks passed
@XuPeng-SH
XuPeng-SH deleted the codex/fix-transfer-rowid-pruning branch August 31, 2026 03:06
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…atrixorigin#27879)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27878

## What this PR does / why we need it:

CN tombstone transfer previously used absence from `PartitionState`'s
persisted-object index as evidence that an object had been deleted. Live
appendable objects are intentionally absent from that index, so their
tombstones could be staged for transfer even though the source object
was still alive. Since the replacement-object list only contains objects
created for objects actually deleted in the transfer window, RowID
lookup then failed its cardinality invariant and rolled back large
indexed DML.

This PR:

- classifies a tombstone for transfer only when its source ObjectID
belongs to the exact `deletedObjects` set returned for the current
`(start, end]` window;
- makes both in-memory and persisted transfer paths use the same
`(start, end]` transition window: objects created exactly at `start` are
already visible snapshot state, while a later delete of that object must
still be transferred;
- removes the unsafe absent-means-deleted helper;
- preserves existing ZoneMap/Bloom pruning and the cardinality guard,
with no fallback or global scan;
- keeps PK-conflict object discovery on the same `(from, end]` contract
without advancing `from` twice, so an object transition committed at the
valid HLC timestamp `from.Next()` is not skipped;
- amortizes the existing RowID lookup setup over at most eight source
blocks, with an 8 MiB soft size limit. Table processing and each
transaction's transfer path remain serial: no goroutine, worker pool, or
additional concurrent S3 I/O is introduced. With `N` concurrent
transactions the node still has `N` serial transfer flows, not `2N`
per-transaction workers;
- adds mixed live/deleted, previous-window, zero-width-window,
exact-HLC-successor, and lifecycle-boundary coverage.

The set is allocated only after the coarse tombstone filter finds
affected work and changes per-row classification to O(1) membership.

## QA test plan

Run on a fresh standalone instance and an empty data directory. Execute
the statements continuously so the final insert/UPDATE overlaps the
normal background object merges, matching the original 100%-repro
procedure:

```sql
drop database if exists transfer_rowid_qa;
create database transfer_rowid_qa;
use transfer_rowid_qa;

create table write_100m (
  id bigint primary key,
  col3 int,
  col4 bigint,
  key(col3),
  unique key(col4)
);

insert into write_100m values (1, 1, 1);
insert into write_100m select id + 1,        col3, col4 + 1        from write_100m;
insert into write_100m select id + 2,        col3, col4 + 2        from write_100m;
insert into write_100m select id + 4,        col3, col4 + 4        from write_100m;
insert into write_100m select id + 8,        col3, col4 + 8        from write_100m;
insert into write_100m select id + 16,       col3, col4 + 16       from write_100m;
insert into write_100m select id + 32,       col3, col4 + 32       from write_100m;
insert into write_100m select id + 64,       col3, col4 + 64       from write_100m;
insert into write_100m select id + 128,      col3, col4 + 128      from write_100m;
insert into write_100m select id + 256,      col3, col4 + 256      from write_100m;
insert into write_100m select id + 512,      col3, col4 + 512      from write_100m;
insert into write_100m select id + 1024,     col3, col4 + 1024     from write_100m;
insert into write_100m select id + 2048,     col3, col4 + 2048     from write_100m;
insert into write_100m select id + 4096,     col3, col4 + 4096     from write_100m;
insert into write_100m select id + 8192,     col3, col4 + 8192     from write_100m;
insert into write_100m select id + 16384,    col3, col4 + 16384    from write_100m;
insert into write_100m select id + 32768,    col3, col4 + 32768    from write_100m;
insert into write_100m select id + 65536,    col3, col4 + 65536    from write_100m;
insert into write_100m select id + 131072,   col3, col4 + 131072   from write_100m;
insert into write_100m select id + 262144,   col3, col4 + 262144   from write_100m;
insert into write_100m select id + 524288,   col3, col4 + 524288   from write_100m;
insert into write_100m select id + 1048576,  col3, col4 + 1048576  from write_100m;
insert into write_100m select id + 2097152,  col3, col4 + 2097152  from write_100m;
insert into write_100m select id + 4194304,  col3, col4 + 4194304  from write_100m;
insert into write_100m select id + 8388608,  col3, col4 + 8388608  from write_100m;
insert into write_100m select id + 16777216, col3, col4 + 16777216 from write_100m;
insert into write_100m
  select id + 33554432, col3, col4 + 33554432
  from write_100m where id <= 16445568;

select count(*) from write_100m; -- 50000000
update write_100m set col3 = col3 + 1;

select count(*), min(id), max(id), min(col3), max(col3),
       sum(id), sum(col4),
       sum(if(id <> col4 or col3 <> 2, 1, 0)) as invalid_rows
from write_100m;
-- 50000000, 1, 50000000, 2, 2,
-- 1250000025000000, 1250000025000000, 0

explain select * from write_100m force index(col3) where col3 = 2 limit 10;
select count(*) from write_100m force index(col3) where col3 = 2; -- 50000000
select count(*) from write_100m force index(col3) where col3 = 1; -- 0
select * from write_100m where col4 in (1, 8192, 8388608, 33554432, 50000000)
order by col4;
```

Acceptance:

1. The UPDATE commits without `TRANSFER-ROWIDS-ERROR-LEN-MISMATCH`, PK
mismatch, retry exhaustion, or transaction rollback.
2. All exact SQL oracles above match, and `EXPLAIN` contains `Index
Table Scan on write_100m.col3` plus `Join Type: INDEX`.
3. Stop the service cleanly, restart it on the same data directory, and
rerun the final aggregate, forced-secondary-index counts, and
unique-index point lookups; results must remain identical.
4. For performance, record UPDATE client wall time and each
`CN-TRANSFER-TOMBSTONE-OBJ time-spent` on the same host/configuration.
Compare with this PR's pre-optimization baseline: UPDATE 232 s
end-to-end, base-table transfer 57 s, hidden-secondary-index transfer
107 s. Performance is judged by same-machine before/after comparison,
not an absolute CI timeout.

Validation:

- `go test ./pkg/vm/engine/disttae/logtailreplay ./pkg/vm/engine/disttae
-count=1`
- focused `logtailreplay` window tests and `disttae` transfer tests
passed 10 times under `-race`;
- the root regression now exercises `HandleObjectEntry` (live appendable
omitted from the persisted index), real window object collection, and
mixed-row staging in one deterministic chain; restoring the old
absent-means-deleted behavior makes it fail with 2 staged rows instead
of 1;
- `make build`;
- lookup microbenchmark: approximately 6.2–6.7 ns/op with 0 B/op and 0
allocs/op; building a 4096-object set takes approximately 52 us and 164
KB once per actual transfer;
- exact original 100%-repro 50M black-box procedure on a fresh process
and fresh data directory at `160d8ca207`: create the same
primary/secondary/unique-indexed table, use the same doubling inserts
plus the final `id <= 16445568` insert to reach exactly 50,000,000 rows,
then run the same full-table `UPDATE col3 = col3 + 1` while base/index
merges replace objects. It committed without RowID mismatch or rollback.
Final oracle: `count(*)=50000000`, `min(id)=1`, `max(id)=50000000`,
`min(col3)=max(col3)=2`. Transfer evidence: base table transferred
33,222,784 rows from 20 deleted objects; hidden secondary index
transferred 35,704,960 rows from 23 deleted objects.
- post-commit persistence and index-integrity verification after two
clean service restarts: the base table has no row where `id != col4` or
`col3 != 2`, and `sum(id)=sum(col4)=1250000025000000`; the hidden
unique-index table has exactly 50,000,000 rows, key/ref range
`1..50000000`, equal sums, and zero key/ref mismatches; the hidden
secondary-index table has exactly 50,000,000 rows with the same ref
range and sum, zero dangling refs, and zero base rows missing an index
ref. A forced secondary-index plan returned 50,000,000 rows for `col3=2`
and zero for `col3=1`.
- bounded batching optimization at `caf968f0f3` (executable production
code identical; the only post-build change was a comment), using the
same fresh-process/fresh-data 50M procedure: UPDATE wall time improved
from 232 s to 203.127 s (-12.5%); base-table transfer improved from
57.166 s to 41.066 s (-28.2%); hidden-secondary-index transfer improved
from 107.035 s to 95.839 s (-10.5%). All exact aggregate and
forced-index oracles passed with no mismatch or rollback. The full
script, including loading and validation queries, took 4m00.64s versus
4m24.22s before batching.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…atrixorigin#27879)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27878

## What this PR does / why we need it:

CN tombstone transfer previously used absence from `PartitionState`'s
persisted-object index as evidence that an object had been deleted. Live
appendable objects are intentionally absent from that index, so their
tombstones could be staged for transfer even though the source object
was still alive. Since the replacement-object list only contains objects
created for objects actually deleted in the transfer window, RowID
lookup then failed its cardinality invariant and rolled back large
indexed DML.

This PR:

- classifies a tombstone for transfer only when its source ObjectID
belongs to the exact `deletedObjects` set returned for the current
`(start, end]` window;
- makes both in-memory and persisted transfer paths use the same
`(start, end]` transition window: objects created exactly at `start` are
already visible snapshot state, while a later delete of that object must
still be transferred;
- removes the unsafe absent-means-deleted helper;
- preserves existing ZoneMap/Bloom pruning and the cardinality guard,
with no fallback or global scan;
- keeps PK-conflict object discovery on the same `(from, end]` contract
without advancing `from` twice, so an object transition committed at the
valid HLC timestamp `from.Next()` is not skipped;
- amortizes the existing RowID lookup setup over at most eight source
blocks, with an 8 MiB soft size limit. Table processing and each
transaction's transfer path remain serial: no goroutine, worker pool, or
additional concurrent S3 I/O is introduced. With `N` concurrent
transactions the node still has `N` serial transfer flows, not `2N`
per-transaction workers;
- adds mixed live/deleted, previous-window, zero-width-window,
exact-HLC-successor, and lifecycle-boundary coverage.

The set is allocated only after the coarse tombstone filter finds
affected work and changes per-row classification to O(1) membership.

## QA test plan

Run on a fresh standalone instance and an empty data directory. Execute
the statements continuously so the final insert/UPDATE overlaps the
normal background object merges, matching the original 100%-repro
procedure:

```sql
drop database if exists transfer_rowid_qa;
create database transfer_rowid_qa;
use transfer_rowid_qa;

create table write_100m (
  id bigint primary key,
  col3 int,
  col4 bigint,
  key(col3),
  unique key(col4)
);

insert into write_100m values (1, 1, 1);
insert into write_100m select id + 1,        col3, col4 + 1        from write_100m;
insert into write_100m select id + 2,        col3, col4 + 2        from write_100m;
insert into write_100m select id + 4,        col3, col4 + 4        from write_100m;
insert into write_100m select id + 8,        col3, col4 + 8        from write_100m;
insert into write_100m select id + 16,       col3, col4 + 16       from write_100m;
insert into write_100m select id + 32,       col3, col4 + 32       from write_100m;
insert into write_100m select id + 64,       col3, col4 + 64       from write_100m;
insert into write_100m select id + 128,      col3, col4 + 128      from write_100m;
insert into write_100m select id + 256,      col3, col4 + 256      from write_100m;
insert into write_100m select id + 512,      col3, col4 + 512      from write_100m;
insert into write_100m select id + 1024,     col3, col4 + 1024     from write_100m;
insert into write_100m select id + 2048,     col3, col4 + 2048     from write_100m;
insert into write_100m select id + 4096,     col3, col4 + 4096     from write_100m;
insert into write_100m select id + 8192,     col3, col4 + 8192     from write_100m;
insert into write_100m select id + 16384,    col3, col4 + 16384    from write_100m;
insert into write_100m select id + 32768,    col3, col4 + 32768    from write_100m;
insert into write_100m select id + 65536,    col3, col4 + 65536    from write_100m;
insert into write_100m select id + 131072,   col3, col4 + 131072   from write_100m;
insert into write_100m select id + 262144,   col3, col4 + 262144   from write_100m;
insert into write_100m select id + 524288,   col3, col4 + 524288   from write_100m;
insert into write_100m select id + 1048576,  col3, col4 + 1048576  from write_100m;
insert into write_100m select id + 2097152,  col3, col4 + 2097152  from write_100m;
insert into write_100m select id + 4194304,  col3, col4 + 4194304  from write_100m;
insert into write_100m select id + 8388608,  col3, col4 + 8388608  from write_100m;
insert into write_100m select id + 16777216, col3, col4 + 16777216 from write_100m;
insert into write_100m
  select id + 33554432, col3, col4 + 33554432
  from write_100m where id <= 16445568;

select count(*) from write_100m; -- 50000000
update write_100m set col3 = col3 + 1;

select count(*), min(id), max(id), min(col3), max(col3),
       sum(id), sum(col4),
       sum(if(id <> col4 or col3 <> 2, 1, 0)) as invalid_rows
from write_100m;
-- 50000000, 1, 50000000, 2, 2,
-- 1250000025000000, 1250000025000000, 0

explain select * from write_100m force index(col3) where col3 = 2 limit 10;
select count(*) from write_100m force index(col3) where col3 = 2; -- 50000000
select count(*) from write_100m force index(col3) where col3 = 1; -- 0
select * from write_100m where col4 in (1, 8192, 8388608, 33554432, 50000000)
order by col4;
```

Acceptance:

1. The UPDATE commits without `TRANSFER-ROWIDS-ERROR-LEN-MISMATCH`, PK
mismatch, retry exhaustion, or transaction rollback.
2. All exact SQL oracles above match, and `EXPLAIN` contains `Index
Table Scan on write_100m.col3` plus `Join Type: INDEX`.
3. Stop the service cleanly, restart it on the same data directory, and
rerun the final aggregate, forced-secondary-index counts, and
unique-index point lookups; results must remain identical.
4. For performance, record UPDATE client wall time and each
`CN-TRANSFER-TOMBSTONE-OBJ time-spent` on the same host/configuration.
Compare with this PR's pre-optimization baseline: UPDATE 232 s
end-to-end, base-table transfer 57 s, hidden-secondary-index transfer
107 s. Performance is judged by same-machine before/after comparison,
not an absolute CI timeout.

Validation:

- `go test ./pkg/vm/engine/disttae/logtailreplay ./pkg/vm/engine/disttae
-count=1`
- focused `logtailreplay` window tests and `disttae` transfer tests
passed 10 times under `-race`;
- the root regression now exercises `HandleObjectEntry` (live appendable
omitted from the persisted index), real window object collection, and
mixed-row staging in one deterministic chain; restoring the old
absent-means-deleted behavior makes it fail with 2 staged rows instead
of 1;
- `make build`;
- lookup microbenchmark: approximately 6.2–6.7 ns/op with 0 B/op and 0
allocs/op; building a 4096-object set takes approximately 52 us and 164
KB once per actual transfer;
- exact original 100%-repro 50M black-box procedure on a fresh process
and fresh data directory at `160d8ca207`: create the same
primary/secondary/unique-indexed table, use the same doubling inserts
plus the final `id <= 16445568` insert to reach exactly 50,000,000 rows,
then run the same full-table `UPDATE col3 = col3 + 1` while base/index
merges replace objects. It committed without RowID mismatch or rollback.
Final oracle: `count(*)=50000000`, `min(id)=1`, `max(id)=50000000`,
`min(col3)=max(col3)=2`. Transfer evidence: base table transferred
33,222,784 rows from 20 deleted objects; hidden secondary index
transferred 35,704,960 rows from 23 deleted objects.
- post-commit persistence and index-integrity verification after two
clean service restarts: the base table has no row where `id != col4` or
`col3 != 2`, and `sum(id)=sum(col4)=1250000025000000`; the hidden
unique-index table has exactly 50,000,000 rows, key/ref range
`1..50000000`, equal sums, and zero key/ref mismatches; the hidden
secondary-index table has exactly 50,000,000 rows with the same ref
range and sum, zero dangling refs, and zero base rows missing an index
ref. A forced secondary-index plan returned 50,000,000 rows for `col3=2`
and zero for `col3=1`.
- bounded batching optimization at `caf968f0f3` (executable production
code identical; the only post-build change was a comment), using the
same fresh-process/fresh-data 50M procedure: UPDATE wall time improved
from 232 s to 203.127 s (-12.5%); base-table transfer improved from
57.166 s to 41.066 s (-28.2%); hidden-secondary-index transfer improved
from 107.035 s to 95.839 s (-10.5%). All exact aggregate and
forced-index oracles passed with no mismatch or rollback. The full
script, including loading and validation queries, took 4m00.64s versus
4m24.22s before batching.
VioletQwQ-0 pushed a commit to VioletQwQ-0/matrixone that referenced this pull request Sep 17, 2026
…atrixorigin#27879)

## What type of PR is this?

- [ ] API-change
- [x] BUG
- [ ] Improvement
- [ ] Documentation
- [ ] Feature
- [ ] Test and CI
- [ ] Code Refactoring

## Which issue(s) this PR fixes:

issue matrixorigin#27878

## What this PR does / why we need it:

CN tombstone transfer previously used absence from `PartitionState`'s
persisted-object index as evidence that an object had been deleted. Live
appendable objects are intentionally absent from that index, so their
tombstones could be staged for transfer even though the source object
was still alive. Since the replacement-object list only contains objects
created for objects actually deleted in the transfer window, RowID
lookup then failed its cardinality invariant and rolled back large
indexed DML.

This PR:

- classifies a tombstone for transfer only when its source ObjectID
belongs to the exact `deletedObjects` set returned for the current
`(start, end]` window;
- makes both in-memory and persisted transfer paths use the same
`(start, end]` transition window: objects created exactly at `start` are
already visible snapshot state, while a later delete of that object must
still be transferred;
- removes the unsafe absent-means-deleted helper;
- preserves existing ZoneMap/Bloom pruning and the cardinality guard,
with no fallback or global scan;
- keeps PK-conflict object discovery on the same `(from, end]` contract
without advancing `from` twice, so an object transition committed at the
valid HLC timestamp `from.Next()` is not skipped;
- amortizes the existing RowID lookup setup over at most eight source
blocks, with an 8 MiB soft size limit. Table processing and each
transaction's transfer path remain serial: no goroutine, worker pool, or
additional concurrent S3 I/O is introduced. With `N` concurrent
transactions the node still has `N` serial transfer flows, not `2N`
per-transaction workers;
- adds mixed live/deleted, previous-window, zero-width-window,
exact-HLC-successor, and lifecycle-boundary coverage.

The set is allocated only after the coarse tombstone filter finds
affected work and changes per-row classification to O(1) membership.

## QA test plan

Run on a fresh standalone instance and an empty data directory. Execute
the statements continuously so the final insert/UPDATE overlaps the
normal background object merges, matching the original 100%-repro
procedure:

```sql
drop database if exists transfer_rowid_qa;
create database transfer_rowid_qa;
use transfer_rowid_qa;

create table write_100m (
  id bigint primary key,
  col3 int,
  col4 bigint,
  key(col3),
  unique key(col4)
);

insert into write_100m values (1, 1, 1);
insert into write_100m select id + 1,        col3, col4 + 1        from write_100m;
insert into write_100m select id + 2,        col3, col4 + 2        from write_100m;
insert into write_100m select id + 4,        col3, col4 + 4        from write_100m;
insert into write_100m select id + 8,        col3, col4 + 8        from write_100m;
insert into write_100m select id + 16,       col3, col4 + 16       from write_100m;
insert into write_100m select id + 32,       col3, col4 + 32       from write_100m;
insert into write_100m select id + 64,       col3, col4 + 64       from write_100m;
insert into write_100m select id + 128,      col3, col4 + 128      from write_100m;
insert into write_100m select id + 256,      col3, col4 + 256      from write_100m;
insert into write_100m select id + 512,      col3, col4 + 512      from write_100m;
insert into write_100m select id + 1024,     col3, col4 + 1024     from write_100m;
insert into write_100m select id + 2048,     col3, col4 + 2048     from write_100m;
insert into write_100m select id + 4096,     col3, col4 + 4096     from write_100m;
insert into write_100m select id + 8192,     col3, col4 + 8192     from write_100m;
insert into write_100m select id + 16384,    col3, col4 + 16384    from write_100m;
insert into write_100m select id + 32768,    col3, col4 + 32768    from write_100m;
insert into write_100m select id + 65536,    col3, col4 + 65536    from write_100m;
insert into write_100m select id + 131072,   col3, col4 + 131072   from write_100m;
insert into write_100m select id + 262144,   col3, col4 + 262144   from write_100m;
insert into write_100m select id + 524288,   col3, col4 + 524288   from write_100m;
insert into write_100m select id + 1048576,  col3, col4 + 1048576  from write_100m;
insert into write_100m select id + 2097152,  col3, col4 + 2097152  from write_100m;
insert into write_100m select id + 4194304,  col3, col4 + 4194304  from write_100m;
insert into write_100m select id + 8388608,  col3, col4 + 8388608  from write_100m;
insert into write_100m select id + 16777216, col3, col4 + 16777216 from write_100m;
insert into write_100m
  select id + 33554432, col3, col4 + 33554432
  from write_100m where id <= 16445568;

select count(*) from write_100m; -- 50000000
update write_100m set col3 = col3 + 1;

select count(*), min(id), max(id), min(col3), max(col3),
       sum(id), sum(col4),
       sum(if(id <> col4 or col3 <> 2, 1, 0)) as invalid_rows
from write_100m;
-- 50000000, 1, 50000000, 2, 2,
-- 1250000025000000, 1250000025000000, 0

explain select * from write_100m force index(col3) where col3 = 2 limit 10;
select count(*) from write_100m force index(col3) where col3 = 2; -- 50000000
select count(*) from write_100m force index(col3) where col3 = 1; -- 0
select * from write_100m where col4 in (1, 8192, 8388608, 33554432, 50000000)
order by col4;
```

Acceptance:

1. The UPDATE commits without `TRANSFER-ROWIDS-ERROR-LEN-MISMATCH`, PK
mismatch, retry exhaustion, or transaction rollback.
2. All exact SQL oracles above match, and `EXPLAIN` contains `Index
Table Scan on write_100m.col3` plus `Join Type: INDEX`.
3. Stop the service cleanly, restart it on the same data directory, and
rerun the final aggregate, forced-secondary-index counts, and
unique-index point lookups; results must remain identical.
4. For performance, record UPDATE client wall time and each
`CN-TRANSFER-TOMBSTONE-OBJ time-spent` on the same host/configuration.
Compare with this PR's pre-optimization baseline: UPDATE 232 s
end-to-end, base-table transfer 57 s, hidden-secondary-index transfer
107 s. Performance is judged by same-machine before/after comparison,
not an absolute CI timeout.

Validation:

- `go test ./pkg/vm/engine/disttae/logtailreplay ./pkg/vm/engine/disttae
-count=1`
- focused `logtailreplay` window tests and `disttae` transfer tests
passed 10 times under `-race`;
- the root regression now exercises `HandleObjectEntry` (live appendable
omitted from the persisted index), real window object collection, and
mixed-row staging in one deterministic chain; restoring the old
absent-means-deleted behavior makes it fail with 2 staged rows instead
of 1;
- `make build`;
- lookup microbenchmark: approximately 6.2–6.7 ns/op with 0 B/op and 0
allocs/op; building a 4096-object set takes approximately 52 us and 164
KB once per actual transfer;
- exact original 100%-repro 50M black-box procedure on a fresh process
and fresh data directory at `160d8ca207`: create the same
primary/secondary/unique-indexed table, use the same doubling inserts
plus the final `id <= 16445568` insert to reach exactly 50,000,000 rows,
then run the same full-table `UPDATE col3 = col3 + 1` while base/index
merges replace objects. It committed without RowID mismatch or rollback.
Final oracle: `count(*)=50000000`, `min(id)=1`, `max(id)=50000000`,
`min(col3)=max(col3)=2`. Transfer evidence: base table transferred
33,222,784 rows from 20 deleted objects; hidden secondary index
transferred 35,704,960 rows from 23 deleted objects.
- post-commit persistence and index-integrity verification after two
clean service restarts: the base table has no row where `id != col4` or
`col3 != 2`, and `sum(id)=sum(col4)=1250000025000000`; the hidden
unique-index table has exactly 50,000,000 rows, key/ref range
`1..50000000`, equal sums, and zero key/ref mismatches; the hidden
secondary-index table has exactly 50,000,000 rows with the same ref
range and sum, zero dangling refs, and zero base rows missing an index
ref. A forced secondary-index plan returned 50,000,000 rows for `col3=2`
and zero for `col3=1`.
- bounded batching optimization at `caf968f0f3` (executable production
code identical; the only post-build change was a comment), using the
same fresh-process/fresh-data 50M procedure: UPDATE wall time improved
from 232 s to 203.127 s (-12.5%); base-table transfer improved from
57.166 s to 41.066 s (-28.2%); hidden-secondary-index transfer improved
from 107.035 s to 95.839 s (-10.5%). All exact aggregate and
forced-index oracles passed with no mismatch or rollback. The full
script, including loading and validation queries, took 4m00.64s versus
4m24.22s before batching.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants