cockroachdb / cockroachdb/cockroach

sql/schemachanger: constraint validation exposes internal placeholder names in error messages

Open
#173,911 1 comment 0 reactions 0 assignees View on GitHub
A-schema-changer-impl C-bug O-agent T-sql-foundations
Dominant language
Go
Stars
32.5k
Forks
4.1k
PR merge metrics
PR metrics pending

Description

**Describe the problem**

The declarative schema changer uses internal placeholder names
(`crdb_internal_constraint__name_placeholder`) while constraints
transition through intermediate states. During the `Validating` window, if
a DML statement violates the constraint, the error message surfaces this
internal name instead of the user-specified constraint name.

For example, if a user runs `ALTER DOMAIN d VALIDATE CONSTRAINT chk`, a
concurrent `INSERT` that violates `chk` would report a violation against
`crdb_internal_constraint_2_name_placeholder`.

**To Reproduce**

1. Create a table or domain with an unvalidated check constraint named `chk`.
2. Run `ALTER TABLE t VALIDATE CONSTRAINT chk` (or `ALTER DOMAIN d VALIDATE CONSTRAINT chk`).
3. While validation is in progress, insert a row that violates the constraint from another session.
4. The error references `crdb_internal_constraint_2_name_placeholder` instead of `chk`.

**Expected behavior**

Error messages should reference the user-specified constraint name (`chk`),
not the internal placeholder.

**Additional data**

The placeholder name originates in `scmutationexec` where constraints are
initially created with `tabledesc.ConstraintNamePlaceholder(id)`. The real
name is set later by a separate `SetConstraintName` operation driven by the
`ConstraintWithoutIndexName` element
(`pkg/sql/schemachanger/scplan/internal/opgen/opgen_constraint_name.go`).

This affects all constraint types in the declarative schema changer:

- **Table check constraints** — `pkg/sql/schemachanger/scexec/scmutationexec/constraint.go:75`
- **Foreign key constraints** — `pkg/sql/schemachanger/scexec/scmutationexec/constraint.go:398`
- **Unique without index constraints** — `pkg/sql/schemachanger/scexec/scmutationexec/constraint.go:530`
- **Domain check constraints** — `pkg/sql/schemachanger/scexec/scmutationexec/type.go:271`
- **Hash-sharded column constraints** — visible e.g. in `pkg/ccl/schemachangerccl/testdata/end_to_end/alter_table_alter_primary_key_rbr/alter_table_alter_primary_key_rbr.side_effects:1977`

87 end-to-end test side_effects files show this pattern.

**Possible approaches**

1. Set the real constraint name eagerly when adding the constraint, and use the `ConstraintWithoutIndexName` element only for rename operations.
2. Reorder the plan so the name-set operation runs before the constraint enters `Validating`.
3. Accept the current behavior as a brief transient window (validation is typically fast).

Epic: none

Jira issue: CRDB-67210

Epic CRDB-66030

Contributor guide

Open the contributing guide

Research direction

Start with the constraint creation paths in pkg/sql/schemachanger/scexec/scmutationexec/constraint.go and type.go, then read the name operation in pkg/sql/schemachanger/scplan/internal/opgen/opgen_constraint_name.go. Reproduce concurrent validation for a named constraint and inspect the affected end_to_end side_effects files. Done means violations during validation report the user-specified name instead of the internal placeholder across the listed constraint types.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.