starrocks icon indicating copy to clipboard operation
starrocks copied to clipboard

[BugFix] Fix foreign key constraints lost after FE restart (backport #66474)

Open mergify[bot] opened this issue 2 weeks ago • 3 comments

Why I'm doing:

Foreign key constraints are lost after FE restart due to table loading order issues during image deserialization.

Root Cause:

When FE loads tables from image file, each table is deserialized sequentially. During deserialization, TableProperty.gsonPostProcess() is automatically called, which invokes buildMvProperties() -> buildConstraint() to parse foreign key constraints.

The problem is that buildConstraint() calls ForeignKeyConstraint.parse(), which tries to look up the referenced parent table from GlobalStateMgr. However, at this point, the parent table may not have been loaded yet (it might be later in the serialization stream).

When ForeignKeyConstraint.getTableBaseInfo() cannot find the parent table, it throws an IllegalArgumentException. This exception is caught and silently ignored in buildConstraint() (only logs a warning), leaving foreignKeyConstraints as null.

What I'm doing:

Add a safety net by calling tableProperty.buildConstraint() in OlapTable.onReload(), which is invoked after all tables have been loaded into memory.

Fixes #issue

What type of PR is this:

  • [x] BugFix
  • [ ] Feature
  • [ ] Enhancement
  • [ ] Refactor
  • [ ] UT
  • [ ] Doc
  • [ ] Tool

Does this PR entail a change in behavior?

  • [ ] Yes, this PR will result in a change in behavior.
  • [x] No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • [ ] Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • [ ] Parameter changes: default values, similar parameters but with different default values
  • [ ] Policy changes: use new policy to replace old one, functionality automatically enabled
  • [ ] Feature removed
  • [ ] Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • [ ] I have added test cases for my bug fix or my new feature
  • [ ] This pr needs user documentation (for new or modified features or behaviors)
    • [ ] I have added documentation for my new feature or new function
  • [x] This is a backport pr

Bugfix cherry-pick branch check:

  • [x] I have checked the version labels which the pr will be auto-backported to the target branch
    • [x] 4.0
    • [x] 3.5
    • [x] 3.4
    • [x] 3.3

[!NOTE] Rebuild table properties constraints during reload for OlapTable and MaterializedView to preserve FKs after FE restart, and add tests verifying SHOW CREATE outputs unique/foreign key constraints.

  • Catalog (reload behavior):
    • MaterializedView.onReloadImpl(): call tableProperty.buildConstraint() before registering with GlobalConstraintManager.
    • OlapTable.onReload(): call tableProperty.buildConstraint() before registering with GlobalConstraintManager.
  • Tests:
    • Add testShowCreateTableWithUniqueAndForeignKeyConstraints in ShowCreateTableStmtTest to assert SHOW CREATE includes unique_constraints and foreign_key_constraints with correct columns and referenced tables.

Written by Cursor Bugbot for commit 9a0cb2de118543318ac01e0de2280c5294dd0557. This will update automatically on new commits. Configure here.


This is an automatic backport of pull request #66474 done by [Mergify](https://mergify.com).

mergify[bot] avatar Dec 11 '25 06:12 mergify[bot]

Cherry-pick of eb4b6f1edcaefe33e7f5aeed1337377155792cd0 has failed:

On branch mergify/bp/branch-3.3/pr-66474
Your branch is up to date with 'origin/branch-3.3'.

You are currently cherry-picking commit eb4b6f1edc.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	modified:   fe/fe-core/src/main/java/com/starrocks/catalog/OlapTable.java
	modified:   fe/fe-core/src/test/java/com/starrocks/analysis/ShowCreateTableStmtTest.java

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   fe/fe-core/src/main/java/com/starrocks/catalog/MaterializedView.java

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

mergify[bot] avatar Dec 11 '25 06:12 mergify[bot]

@mergify[bot]: Backport conflict, please reslove the conflict and resubmit the pr

mergify[bot] avatar Dec 11 '25 06:12 mergify[bot]

🧪 CI Insights

Here's what we observed from your CI run for a641e78d.

🟢 All jobs passed!

But CI Insights is watching 👀

mergify[bot] avatar Dec 11 '25 06:12 mergify[bot]

@cursor review

alvin-celerdata avatar Dec 12 '25 17:12 alvin-celerdata