cockroachdb / cockroachdb/cockroach
sql: can't use UDF touching a table in a CHECK constraint on the same table
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
This issue seems to be effectively the same as #108223.
Here is something that's allowed in PG:
```sql
CREATE TABLE t (k INT PRIMARY KEY);
INSERT INTO t SELECT generate_series(1, 10);
CREATE FUNCTION reads() RETURNS INT AS 'SELECT count(*) FROM t' LANGUAGE SQL;
ALTER TABLE t ADD CONSTRAINT c CHECK (reads() > 1);
```
whereas we fail the schema change with
```
ERROR: error executing StatementPhase stage 1 of 1 with 4 MutationType ops: *scop.AddTableConstraintBackReferencesInFunctions: &{{{}} 104 2 [105]}: cannot add dependency from descriptor 104 to function reads (105) because there will be a dependency cycle
SQLSTATE: 42P13
```
Probably we should track this as a known limitation but will defer to Foundations on this.
Jira issue: CRDB-55740
Epic CRDB-60817
Contributor guide
Research direction
Reproduce the SQL sequence in the issue and compare the behavior with PostgreSQL and duplicate issue #108223. Start at the reported scop.AddTableConstraintBackReferencesInFunctions mutation and trace the dependency-cycle handling; done requires an agreed resolution, either supported behavior with coverage or a documented known limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100