Skip to content

4.0: fix: restore database delete skip for already deleted relations - #24747

Merged
XuPeng-SH merged 1 commit into
matrixorigin:4.0-devfrom
gouhongshen:codex/restore-skip-deleted-relations-4.0
Jun 1, 2026
Merged

XuPeng-SH merged 1 commit into
matrixorigin:4.0-devfrom
gouhongshen:codex/restore-skip-deleted-relations-4.0

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

This reverts commit aac3d03.

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #24711

What this PR does / why we need it:

Restore the 4.0-dev fix from #24716 after #24724 reverted it.

The original fix is still needed for restore-account database delete: when a relation was already deleted in the current transaction, Engine.Delete(database) should skip that relation only when tableOps.existAndDeleted proves it was deleted, while preserving other errors.

Tested with:

source $HOME/.zshrc && moenv && go test ./pkg/vm/engine/disttae

@qodo-code-review

Copy link
Copy Markdown

Review Summary by Qodo

Restore database delete skip for already deleted relations

🐞 Bug fix

Grey Divider

Walkthroughs

Description
• Restore database delete skip for already deleted relations
• Add accountId field to txnDatabase struct
• Filter out relations already deleted in transaction
• Add comprehensive unit tests for relation deletion logic
Diagram
flowchart LR
  A["Engine.Delete database"] --> B["Get relations to delete"]
  B --> C["Filter already deleted relations"]
  C --> D["Check tableOps.existAndDeleted"]
  D --> E["Skip deleted, delete active"]
  E --> F["Update txn cache"]

Loading

Grey Divider

File Changes

1. pkg/vm/engine/disttae/engine.go 🐞 Bug fix +30/-1

Add relation deletion filtering logic to engine

• Add accountId field initialization in Create method for txnDatabase
• Add accountId field initialization in Database method for txnDatabase
• Implement filterDeleteDatabaseRelations function to skip already-deleted relations during
 database delete
• Implement isDeleteDatabaseRelationDeletedInTxn helper function to check if relation was deleted
 in current transaction
• Apply filtering logic before deleting relations in Engine.Delete method

pkg/vm/engine/disttae/engine.go


2. pkg/vm/engine/disttae/engine_test.go 🧪 Tests +88/-0

Add comprehensive tests for relation deletion filtering

• Add TestFilterDeleteDatabaseRelationsSkipsAlreadyDeletedRelation to verify deleted relations are
 filtered out
• Add TestIsDeleteDatabaseRelationDeletedInTxn to test relation deletion detection logic
• Add TestIsDeleteDatabaseRelationDeletedInTxnUsesSystemAccount to verify system account handling
 for catalog tables
• Import necessary testing dependencies and mock utilities

pkg/vm/engine/disttae/engine_test.go


3. pkg/vm/engine/disttae/types.go ✨ Enhancement +1/-0

Add accountId field to txnDatabase struct

• Add accountId field to txnDatabase struct to track account ownership

pkg/vm/engine/disttae/types.go


Grey Divider

Qodo Logo

@qodo-code-review

qodo-code-review Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (2) 📘 Rule violations (0)

Grey Divider


Remediation recommended

1. mo_catalog accountId missing 🐞 Bug ≡ Correctness
Description
filterDeleteDatabaseRelations uses txnDatabase.accountId to decide whether a table was already
deleted in the current txn, but Engine.Database constructs the mo_catalog txnDatabase without
setting accountId. If Engine.Delete is ever invoked for mo_catalog in a non-system account, skip
detection for non-"system tables" (most mo_catalog tables) can look under accountId=0 and fail,
re-triggering the original “already deleted” errors.
Code

pkg/vm/engine/disttae/engine.go[R758-762]

Evidence
The skip filter uses db.accountId to build the tableKey, but the mo_catalog txnDatabase
returned by Engine.Database does not initialize accountId. Additionally, IsSystemTableByName
only covers 4 tables, so most mo_catalog tables will not trigger the System_Account override and
will use the (unset) db.accountId instead.

pkg/vm/engine/disttae/engine.go[414-421]
pkg/vm/engine/disttae/engine.go[758-781]
pkg/catalog/types.go[196-202]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`filterDeleteDatabaseRelations` relies on `txnDatabase.accountId` to check `tableOps.existAndDeleted(key)`.
However, `Engine.Database` creates the `mo_catalog` database handle without setting `accountId`, leaving it as `0`. For `mo_catalog` tables that are *not* covered by `catalog.IsSystemTableByName` (which only includes `mo_database`, `mo_tables`, `mo_columns`, and `__mo_index_unique_mo_tables_logical_id`), the skip-check will use `accountId=0` and can miss tables deleted under the caller’s tenant account.

### Issue Context
- The new skip behavior is correct for normal tenant databases where `txnDatabase.accountId` is set.
- `mo_catalog` is special-cased and currently does **not** set `accountId`, but it can contain many per-tenant catalog tables.

### Fix Focus Areas
- pkg/vm/engine/disttae/engine.go[397-421]
- pkg/vm/engine/disttae/engine.go[758-781]
- pkg/catalog/types.go[196-202]

### Suggested fix
In `Engine.Database`, when `name == catalog.MO_CATALOG`, populate `accountId` from `defines.GetAccountId(ctx)` (similar to the non-catalog branch), and set it on the returned `txnDatabase`. Keep the existing override in `isDeleteDatabaseRelationDeletedInTxn` for `IsSystemTableByName` to force `System_Account` for the true system tables.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Advisory comments

2. Skip logging too noisy 🐞 Bug ◔ Observability
Description
filterDeleteDatabaseRelations emits an Info log per skipped table, which can generate high log
volume during restore-account/drop-database flows when many tables were already deleted in the txn.
This can obscure other logs and increase operational cost without changing behavior.
Code

pkg/vm/engine/disttae/engine.go[R762-767]

Evidence
The new code logs an Info message inside the per-relation loop whenever a relation is skipped, so
log count scales with number of skipped relations.

pkg/vm/engine/disttae/engine.go[758-771]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

### Issue description
`filterDeleteDatabaseRelations` logs at Info level for every skipped table.
When many relations are skipped, this can flood logs.

### Issue Context
This path can run once per table during `Engine.Delete(database)`.

### Fix Focus Areas
- pkg/vm/engine/disttae/engine.go[758-771]

### Suggested fix
Change the per-table log to Debug, or aggregate (count skipped tables and log once per database delete). Keep `txn`/db/table context available at Debug for troubleshooting.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Qodo Logo

@matrix-meow matrix-meow added the size/M Denotes a PR that changes [100,499] lines label Jun 1, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Jun 1, 2026
@XuPeng-SH
XuPeng-SH merged commit 198f1ad into matrixorigin:4.0-dev Jun 1, 2026
23 checks passed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants