Skip to content

release-26.1: sql: clean up FK back-references during rollback of CREATE TABLE#165994

Merged
rafiss merged 2 commits intocockroachdb:release-26.1from
spilchen:blathers/backport-release-26.1-165551
Mar 17, 2026
Merged

release-26.1: sql: clean up FK back-references during rollback of CREATE TABLE#165994
rafiss merged 2 commits intocockroachdb:release-26.1from
spilchen:blathers/backport-release-26.1-165551

Conversation

@spilchen
Copy link
Copy Markdown
Contributor

@spilchen spilchen commented Mar 17, 2026

Backport 2/2 commits from #165551 on behalf of @spilchen.


Previously, when a CREATE TABLE with inline FOREIGN KEY constraints had its schema change job fail permanently, rollbackSchemaChange() would mark the new table as Dropped and queue it for GC, but never remove the InboundFK entries that had been added to the referenced table(s) during the SQL transaction. This left orphaned FK back-references pointing to a descriptor that no longer exists, causing descriptor validation errors like:

relation "parent" (209): invalid foreign key backreference: missing table=264: referenced descriptor not found

The fix adds FK back-reference cleanup in the ADD-state rollback path of rollbackSchemaChange(), alongside the existing view dependency cleanup. For each outbound FK on the table being rolled back, we remove the corresponding InboundFK from the referenced table. Similarly, for any inbound FKs (other tables referencing this one), we remove the corresponding OutboundFK from the origin table.

This reuses the existing removeFKBackReferenceFromTable() helper already used by dropTableImpl() and maybeReverseMutations(). Errors during cleanup are logged as warnings and skipped (best-effort), matching the pattern used by dropViewDeps().

Closes #165548

Epic: None
Release note (bug fix): Fixed a bug in the legacy schema changer where rolling back a CREATE TABLE with inline FOREIGN KEY constraints could leave orphaned FK back-references on the referenced table, causing descriptor validation errors.


Release justification: simple bug fix that has been hit a few times in support escalations

spilchen and others added 2 commits March 12, 2026 14:57
…DD state

Previously, when a CREATE TABLE with inline FOREIGN KEY constraints had
its schema change job fail permanently, rollbackSchemaChange() would
mark the new table as Dropped and queue it for GC, but never remove the
InboundFK entries that had been added to the referenced table(s) during
the SQL transaction. This left orphaned FK back-references pointing to a
descriptor that no longer exists, causing descriptor validation errors
like:

  relation "parent" (209): invalid foreign key backreference:
  missing table=264: referenced descriptor not found

The fix adds FK back-reference cleanup in the ADD-state rollback path of
rollbackSchemaChange(), alongside the existing view dependency cleanup.
For each outbound FK on the table being rolled back, we remove the
corresponding InboundFK from the referenced table. Similarly, for any
inbound FKs (other tables referencing this one), we remove the
corresponding OutboundFK from the origin table.

This reuses the existing removeFKBackReferenceFromTable() helper already
used by dropTableImpl() and maybeReverseMutations(). Errors during
cleanup are logged as warnings and skipped (best-effort), matching the
pattern used by dropViewDeps().

Epic: None
Release note (bug fix): Fixed a bug in the legacy schema changer where
rolling back a CREATE TABLE with inline FOREIGN KEY constraints could
leave orphaned FK back-references on the referenced table, causing
descriptor validation errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add TestCreateTableFKRollbackCleansBackrefs which verifies that when a
CREATE TABLE with an inline FOREIGN KEY constraint has its async schema
change job fail, the rollback properly cleans up the InboundFK
back-reference on the referenced table.

The test:
1. Creates a parent table.
2. Creates a child table with an inline FK referencing the parent, while
   injecting a permanent error into the child's schema change job via
   the RunBeforeResume testing knob.
3. Waits for the schema change job to fail.
4. Verifies the child table no longer exists (rolled back from ADD
   state).
5. Waits for the GC job to fully delete the child descriptor, so
   that the validation error matches the production error exactly:
   "invalid foreign key backreference: missing table=N: referenced
   descriptor not found".
6. Asserts the parent does not appear in crdb_internal.invalid_objects.
7. Asserts the parent table has no orphaned InboundFKs.

Closes cockroachdb#165551

Epic: None
Release note: None

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@spilchen spilchen requested a review from a team as a code owner March 17, 2026 19:06
@blathers-crl blathers-crl bot added blathers-backport This is a backport that Blathers created automatically. O-robot Originated from a bot. labels Mar 17, 2026
@blathers-crl
Copy link
Copy Markdown

blathers-crl bot commented Mar 17, 2026

Thanks for opening a backport.

Before merging, please confirm that the change does not break backwards compatibility and otherwise complies with the backport policy. Include a brief release justification in the PR description explaining why the backport is appropriate. All backports must be reviewed by the TL for the owning area. While the stricter LTS policy does not yet apply, please exercise judgment and consider gating non-critical changes behind a disabled-by-default feature flag when appropriate.

@blathers-crl blathers-crl bot requested a review from rafiss March 17, 2026 19:06
@blathers-crl blathers-crl bot added backport Label PR's that are backports to older release branches T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) labels Mar 17, 2026
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

@rafiss rafiss merged commit dc8b930 into cockroachdb:release-26.1 Mar 17, 2026
21 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Label PR's that are backports to older release branches blathers-backport This is a backport that Blathers created automatically. O-robot Originated from a bot. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) target-release-26.1.2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants