Skip to content

Revert "sql: don't drop table constraints after a nullable UNIQUE" - #39660

Merged
antiguru merged 1 commit into
mainfrom
revert-39598-claude/gracious-keller-yg92qg
Oct 7, 2026
Merged

antiguru merged 1 commit into
mainfrom
revert-39598-claude/gracious-keller-yg92qg

Conversation

@antiguru

@antiguru antiguru commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Reverts #39598.

#39598 changes the RelationDesc that replanning produces for existing tables and source exports whose constraints the old planner dropped. It can add keys and flip primary key columns to NOT NULL. Persist stores the full RelationDesc, keys included, as the shard schema and finds writer schemas by equality, and tables and source exports have no schema evolution at bootstrap. After an upgrade, affected tables panic environmentd at boot in register_table_collections, and affected Postgres and MySQL source exports crash-loop the replica that ingests them. Re-landing the fix needs a catalog migration or a per-export flag that keeps existing descs stable, plus platform-checks for both shapes. Details are in the post-merge review on #39598.

🤖 Generated with Claude Code

@antiguru
antiguru requested a review from a team as a code owner October 7, 2026 18:02
@linear-code

linear-code Bot commented Oct 7, 2026

Copy link
Copy Markdown

SQL-759

@bosconi bosconi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Claude verified it's a clean revert.

@antiguru
antiguru merged commit ca3d68b into main Oct 7, 2026
86 checks passed
@antiguru
antiguru deleted the revert-39598-claude/gracious-keller-yg92qg branch October 7, 2026 18:30
@def-

def- commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

QA LLM Review (Post Merge)

@antiguru — an automated review of commit ca3d68bf4a found the following potential MEDIUM+ issue(s) after this PR was merged.

1. MEDIUM -- Revert re-opens the unsafe-flag bypass for CHECK and FOREIGN KEY constraints

src/sql/src/plan/statement/ddl.rs:408

The restored break 'c exits the one loop that also enforces unsafe_enable_table_check_constraint and unsafe_enable_table_foreign_key (ddl.rs:425, ddl.rs:430). Any CHECK or FOREIGN KEY placed after a nullable UNIQUE is never gated, so production users can create tables with constraints Materialize does not enforce. #39598 had closed this as a side effect by validating every constraint before the key pass, and that part does not change any RelationDesc.

Details

Reproduced with the image built from 134006e, whose ddl.rs matches this commit byte for byte, using default (production) flags:

  • CREATE TABLE t (a int, CHECK (a > 0)) fails with "CREATE TABLE with a check constraint is not supported ... unsafe".
  • CREATE TABLE t (a int, UNIQUE (a), CHECK (a > 0)) succeeds. INSERT INTO t VALUES (-5) succeeds, and SHOW CREATE TABLE still displays CHECK (a > 0).
  • With RBAC on, a role that lacks USAGE on schema private can run CREATE TABLE att.t (a int, UNIQUE (a), FOREIGN KEY (a) REFERENCES private.victim (x)). This records a catalog dependency on another role's table, and a plain DROP TABLE private.victim then fails with "still depended upon by table att.t".

The crash that motivated the revert came from changes to nullability and keys. The gate checks can be restored on their own without bringing it back: run the ForeignKey/Check arms in a separate pass over constraints, outside the 'c loop, and leave the key logic and its break 'c as they are. Re-planning existing items at boot stays safe because both flags are enable_for_item_parsing: true (src/sql/src/session/vars/definitions.rs:1995, :2001). Keep the other per-constraint checks that the break skips ("unknown column in constraint", "multiple primary keys") out of that pass. Existing items created through the bypass may break those checks, and they are not flag-gated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants