cockroachdb / cockroachdb/cockroach

sql: can't use UDF touching a table in a CHECK constraint on the same table

Open
#156,014 0 comments 0 reactions 0 assignees View on GitHub
A-schema-changer-impl A-sql-pgcompat A-sql-routine C-cleanup T-sql-foundations
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

Open the contributing 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.