Skip to content

fix: preserve sequences in database clone and restore - #27401

Merged
mergify[bot] merged 6 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27049-1771549f
Aug 21, 2026
Merged

mergify[bot] merged 6 commits into
matrixorigin:mainfrom
gouhongshen:agent/issue-27049-1771549f

Conversation

@gouhongshen

@gouhongshen gouhongshen commented Aug 20, 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:

Related to #27049

What this PR does / why we need it:

Root cause

Database clone and restore source collection unconditionally filtered out relkind = 'S'. Views were still restored, so a sequence-backed view survived while its sequence did not.

Sequences also cannot use the ordinary table-clone path: their catalog DDL may contain the latest ALTER SEQUENCE, and their current value / is_called state lives in the sequence relation row. In addition, nested background CREATE/DROP SEQUENCE previously finished the enclosing transaction early, which would make Data Branch fail and leave partial clone state on a later error.

The initial sequence restore path also assumed that a current same-named object was a sequence. DROP SEQUENCE IF EXISTS intentionally does nothing for a table or view, so a historical sequence restore collided at CREATE SEQUENCE after a valid sequence→table/view generation change.

After sequences entered the shared source list, DATA BRANCH preflight quota counting treated them as receipt-backed tables even though sequence restore deliberately creates no mo_branch_metadata receipt. The preflight increment therefore exceeded the persisted quota usage.

Changes

  • Include sequences in the shared database clone/snapshot/PITR object collector while preserving consumer-specific exclusions for Data Branch deletion metadata.
  • Reconstruct canonical CREATE SEQUENCE SQL from the historical column type and sequence definition at the selected timestamp, including signed/unsigned types, increment, bounds, start, and cycle mode.
  • Restore sequences before dependent tables and views, then copy the complete historical sequence row so last_seq_num and is_called are preserved exactly.
  • Reuse the same sequence restore primitive for live clone, named-snapshot clone, Data Branch, snapshot restore, PITR, and cross-account timestamp restore.
  • Before recreating a historical sequence, resolve the current destination object's actual relkind and execute exactly one matching DROP TABLE, DROP VIEW, or DROP SEQUENCE; an absent object needs no drop, while inconsistent duplicate metadata fails explicitly.
  • Keep nested background sequence DDL inside the enclosing clone/restore transaction; direct user sequence-DDL transaction behavior is unchanged.
  • Count only receipt-backed ordinary tables toward the DATA BRANCH table quota; sequences and views consume no quota unit because neither creates a branch receipt.
  • Add focused UTs for metadata reconstruction and its malformed/missing/error responses, SQL rewriting, ordering, exact state copy, destination relkind resolution, every restore-step error, transaction ownership, all three restore entry points, and missing-account errors.
  • Add a self-contained BVT covering sequence-only sources, dependent views/defaults, live/snapshot/Data Branch/cross-account paths, database/table restore, sequence→table/view replacement, altered and uncalled sequences, and failure atomicity.

Issue-to-test proof

clone_database_sequence.sql directly proves #27049 by asserting:

  • destination catalog entries remain relkind = 'S';
  • definition and all seven state columns match live or historical source state;
  • sequence-only database clone is non-empty;
  • dependent views and sequence-backed defaults execute after clone;
  • live clone, named-snapshot clone, Data Branch, cross-account clone, database snapshot restore, and table snapshot restore all preserve the intended state;
  • table-level snapshot restore replaces current same-named tables and views with the historical sequence, preserving definition, state, and next-value behavior;
  • a fault after sequence creation rolls back the destination database and every destination relation.

TestSequenceRestoreEntryPoints executes the same-account snapshot, PITR, and cross-account snapshot sequence branches and asserts the exact relkind-specific drop and restore statements. It directly proves snapshot sequence→table, PITR sequence→view, cross-account sequence→sequence, and missing-account behavior.

TestGetCreateSequenceSQLErrors proves type/state query failures, missing historical metadata, and invalid cycle state are returned rather than silently constructing bad DDL. The extended TestRestoreSequenceState covers absent/current/ambiguous destination metadata plus lookup, drop, create, delete, and historical-state-copy failures, proving each path stops at the exact failed operation.

TestCloneDatabaseSourceBranchTableCount proves the DATA BRANCH quota increment uses the same unit as persisted receipts: a mixed ordinary-table/sequence/view source counts as one, sequence-only/view-only/empty sources count as zero, and ordinary tables continue to count individually.

Tests run

  • .agents/skills/mo-dev/scripts/mo-cgo-test -mod=readonly -count=1 -timeout=120s -run 'Test(SequenceRestoreMetadataAndOrdering|RestoreSequenceState|SequenceRestoreEntryPoints)$' ./pkg/frontend
  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=120s -run 'Test(GetCreateSequenceSQLErrors|RestoreSequenceState)$' ./pkg/frontend
  • .agents/skills/mo-dev/scripts/mo-cgo-test -mod=readonly -count=1 -timeout=240s ./pkg/frontend
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=15m ./pkg/frontend
  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=180s -run '^(TestGetFkDepsFromTableInfos|TestSequenceRestoreMetadataAndOrdering|TestRestoreSequenceState|TestRewriteCloneSequenceCreateSQL)$' ./pkg/frontend
  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=180s -run '^Test_statement_type$' ./pkg/frontend
  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=180s -run '^TestBranchDeleteDatabaseTableIDsSQLReusesCloneObjectFilter$' ./pkg/frontend
  • mo-tester ... -m run -r 100 -i clone_database_sequence -g — 108/108 passed; the pre-rebase change was also stable across 3/3 runs
  • mo-tester ... -m run -n -r 100 -i clone_sequence,clone_can_rollback -g — 88/88 passed
  • GOWORK=off go list -mod=readonly ./pkg/frontend
  • GOWORK=off ... go vet -mod=readonly ./pkg/frontend
  • make build
  • GOWORK=off go build -mod=readonly ./pkg/frontend
  • GOWORK=off go vet -mod=readonly ./pkg/frontend
  • .agents/skills/mo-dev/scripts/mo-cgo-test -v -count=1 -timeout=180s -run '^(TestRestoreSequenceState|TestSequenceRestoreEntryPoints)$' ./pkg/frontend — all 12 subtests passed
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=300s ./pkg/frontend — passed
  • CGO_CFLAGS=... CGO_LDFLAGS=... GOWORK=off go vet -mod=readonly ./pkg/frontend — passed
  • make build after rebasing onto origin/main — passed
  • mo-tester ... -m genrs -i clone_database_sequence, followed by manual semantic inspection of every new result
  • mo-tester ... -i clone_database_sequence -r 100 — 131/131 passed
  • PR CI: frontend/full UT, SCA, shared build, BVT groups, UT coverage, and changed-code coverage passed. The changed-code coverage policy first reported 42/73 (57.53%), then 49/73 (67.12%); artifact inspection identified the exact remaining blocks, and the final run passed at 56/73 (76.71%, required >75%).
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=120s -run '^TestCloneDatabaseSourceBranchTableCount$' ./pkg/frontend — passed all empty, sequence-only, view-only, mixed, and ordinary-table cases
  • .agents/skills/mo-dev/scripts/mo-cgo-test -count=1 -timeout=600s ./pkg/frontend — passed after the quota correction
  • CGO_CFLAGS=... CGO_LDFLAGS=... GOWORK=off go build -mod=readonly ./pkg/frontend — passed
  • CGO_CFLAGS=... CGO_LDFLAGS=... GOWORK=off go vet -mod=readonly ./pkg/frontend — passed

Residual risk

There is no storage-format or wire-protocol change. Clone/restore adds three catalog reads and up to four transactional restore statements per sequence; this is bounded by the number of sequence objects and is outside query hot paths. Table-level CREATE TABLE ... CLONE sequence remains rejected as a non-base-table source.

@matrix-meow matrix-meow added the size/L Denotes a PR that changes [500,999] lines label Aug 20, 2026
@mergify mergify Bot added the kind/bug Something isn't working label Aug 20, 2026
@gouhongshen
gouhongshen force-pushed the agent/issue-27049-1771549f branch from 2cde2d1 to b66d61a Compare August 20, 2026 10:14
@gouhongshen
gouhongshen force-pushed the agent/issue-27049-1771549f branch from b66d61a to daaf727 Compare August 20, 2026 11:09
@gouhongshen
gouhongshen marked this pull request as ready for review August 20, 2026 12:21
@gouhongshen

Copy link
Copy Markdown
Contributor Author

@XuPeng-SH The quota mismatch was valid and is addressed in 6a61422.

branchTableCount() now excludes both views and sequences, so DATA BRANCH preflight counts exactly the ordinary tables that produce cloneReceipt / live mo_branch_metadata rows. No receipt or quota semantics were added for sequences.

Regression proof: TestCloneDatabaseSourceBranchTableCount covers the reported ordinary-table + sequence + view case (1), plus empty, sequence-only, view-only (0), and multiple ordinary tables (2). The counterexample failed before the production change with actual 2 vs expected 1; the focused test and full pkg/frontend suite now pass. CGo-configured build and vet also pass.

The branch is rebased onto current origin/main, the complete diff was self-reviewed, and the PR description is updated.

@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.

Reviewed exact head 6a614227bbbd2cfc6923da6a16294596ca0d5f9e against base 0e5db4ce1d0eafdc06923b80bbb740f1205566ba.

Both prior blockers are closed:

  • restore now resolves the current destination relkind and drops the matching sequence/table/view before recreating the historical sequence;
  • DATA BRANCH quota now excludes receipt-less sequences, matching both cloneReceipt creation and persisted mo_branch_metadata usage. The regression matrix covers empty, mixed, sequence-only, view-only, and ordinary-only sources.

The rebase-only commits are patch-equivalent to the previously reviewed stack (git range-diff), with the quota fix as the sole semantic addition. Q1-Q3 found no new resource, wait, or unbounded-growth edge in that predicate-only change.

Validation on this exact head:

  • go list, go build, and go vet for ./pkg/frontend pass;
  • focused sequence restore, entry-point, clone rewrite, transaction, and branch quota tests pass;
  • full go test ./pkg/frontend -count=1 passes;
  • git diff --check is clean and all 26 latest checks are completed with allowed conclusions.

No blocking findings remain.

@mergify mergify Bot added the queued label Aug 21, 2026
@mergify

mergify Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Merge Queue Status

  • ✅ Entered queue — 2026-08-21 01:59 UTC · Rule: main · triggered by rule Automatic queue on approval for main
  • ✅ Checks passed · in-place
  • ✅ Merged — 2026-08-21 02:46 UTC · at af7c5d92d2b8db6ae304f13463f5cbf5c7307a2c · squash

This pull request spent 46 minutes 48 seconds in the queue, including 46 minutes 16 seconds running CI.

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

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/XL Denotes a PR that changes [1000, 1999] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants