cockroachdb / cockroachdb/cockroach
sql: CRDB is much stricter than Postgres about the contents of STABLE and IMMUTABLE functions
- Dominant language
- Go
- Stars
- 32.5k
- Forks
- 4.1k
- PR merge metrics
- PR metrics pending
Description
**Describe the problem**
CRDB is much stricter than Postgres about the contents of `STABLE` and `IMMUTABLE` functions.
**To Reproduce**
In `cockroach demo` on master / 23.1, run:
```
CREATE FUNCTION vol (x INT) RETURNS float AS $$
SELECT random();
$$ LANGUAGE SQL STABLE;
```
This succeeds on Postgres, but fails on CRDB with the error `volatile statement not allowed in stable function: SELECT random()`.
Similarly, if I make the function IMMUTABLE, it succeeds on Postgres, but on CRDB I get the error `volatile statement not allowed in immutable function: SELECT random()`.
Postgres docs advise against using `STABLE` and `IMMUTABLE` functions this way, but PG doesn't prevent users from doing it. Similarly, Postgres docs advise against referencing relations from IMMUTABLE functions, but PG doesn't disallow it. CRDB causes such statements to fail with the error: `ERROR: referencing relations is not allowed in immutable function`.
**Expected behavior**
I'm not sure we want to fix this, but if we really want to be consistent with Postgres we should allow these functions to be created.
Jira issue: CRDB-26317
Contributor guide
Research direction
Start by reproducing the CREATE FUNCTION examples in cockroach demo and compare the results with Postgres for STABLE and IMMUTABLE functions. Trace the validation that produces the volatile-statement and relation-reference errors, then establish whether the intended outcome is compatibility or retaining the stricter behavior. Done means the decision is implemented and covered for both examples.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- sql
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100