Skip to content

Conversation

@rafiss
Copy link
Collaborator

@rafiss rafiss commented Feb 10, 2026

Note: This is a draft PR to prototype a suggestion from the review of #163210


Previously, CREATE OR REPLACE FUNCTION was not supported in the
declarative schema changer and fell back to the legacy schema changer
via NotImplementedError.

This commit implements CREATE OR REPLACE using the drop+add pattern
already established by TableStorageParam, RowLevelTTL, and zone configs.
Each mutable sub-element (FunctionBody, FunctionVolatility,
FunctionLeakProof, FunctionNullInputBehavior, FunctionSecurity,
FunctionParams) is dropped and re-added with the new values, while
Function, SchemaChild, FunctionName, Owner, and UserPrivileges remain
untouched.

Key changes:

  • Add replaceFunction in create_function.go with validation: routine
    kind match, return type match (including UDT equivalence), parameter
    name/type constraints, trigger function checks. Options not specified
    are reset to defaults, matching legacy resetFuncOption behavior.
  • Introduce FunctionParams as a standalone element owning the
    SetFunctionParams mutation, rather than only having params in
    scpb.Function.
  • Add SeqNum to FunctionBody and FunctionParams for element key
    uniqueness when content is unchanged during replace.
  • Add SameStagePrecedence dep rules ensuring old elements are dropped
    before new ones are added.
  • Enhance UpdateFunctionRelationReferences execution to clean up stale
    back-references from relations and functions no longer depended on.

Epic: CRDB-42942
Release note: None

rafiss and others added 3 commits February 10, 2026 01:18
Add support for bool fields in the rel package's schema system, alongside
new UInt32Value, BoolValue, and Int32Value attributes in screl. These
attributes enable function element properties (Volatility, LeakProof,
NullInputBehavior, Security, Body) to be mapped with distinguishing keys,
which is a prerequisite for using the drop+add pattern for CREATE OR
REPLACE FUNCTION.

The rel package changes include:
- Bool field support in schema, comparison, and value conversion
- Relaxed checkType to compare scalar kinds by their comparable base
  type rather than named type, allowing different named types of the
  same kind (e.g. Function_Volatility and Function_NullInputBehavior)
  to share a single attribute

The screl changes add entity attr mappings for:
- FunctionVolatility (Int32Value)
- FunctionLeakProof (BoolValue)
- FunctionNullInputBehavior (Int32Value)
- FunctionBody (Value)
- FunctionSecurity (Int32Value)

Release note: none
Epic: None
Co-Authored-By: Claude Opus 4.6 <[email protected]>
Previously, CREATE OR REPLACE FUNCTION was not supported in the
declarative schema changer and fell back to the legacy schema changer
via NotImplementedError.

This commit implements CREATE OR REPLACE using the drop+add pattern
already established by TableStorageParam, RowLevelTTL, and zone configs.
Each mutable sub-element (FunctionBody, FunctionVolatility,
FunctionLeakProof, FunctionNullInputBehavior, FunctionSecurity,
FunctionParams) is dropped and re-added with the new values, while
Function, SchemaChild, FunctionName, Owner, and UserPrivileges remain
untouched.

Key changes:
- Add replaceFunction in create_function.go with validation: routine
kind match, return type match (including UDT equivalence), parameter
name/type constraints, trigger function checks. Options not specified
are reset to defaults, matching legacy resetFuncOption behavior.
- Introduce FunctionParams as a standalone element owning the
SetFunctionParams mutation, rather than only having params in
scpb.Function.
- Add SeqNum to FunctionBody and FunctionParams for element key
uniqueness when content is unchanged during replace.
- Add SameStagePrecedence dep rules ensuring old elements are dropped
before new ones are added.
- Enhance UpdateFunctionRelationReferences execution to clean up stale
back-references from relations and functions no longer depended on.

Epic: CRDB-42942
Release note: None
This helps reduce noise in error output. If an error with a PG code
occurs during this stage, it means that it's because of a user action,
so we shoul avoid showing additional debug info.

Release note: None
@trunk-io
Copy link
Contributor

trunk-io bot commented Feb 10, 2026

Merging to master in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

@blathers-crl
Copy link

blathers-crl bot commented Feb 10, 2026

Your pull request contains more than 1000 changes. It is strongly encouraged to split big PRs into smaller chunks.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@rafiss
Copy link
Collaborator Author

rafiss commented Feb 10, 2026

closing since this was just a proof of concept; we're merging #163210 instead

@rafiss rafiss closed this Feb 10, 2026
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.

2 participants