Skip to content

sql/schemachanger: implement CREATE OR REPLACE TRIGGER#162633

Merged
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:trigger-replace
Feb 13, 2026
Merged

sql/schemachanger: implement CREATE OR REPLACE TRIGGER#162633
trunk-io[bot] merged 1 commit intocockroachdb:masterfrom
rafiss:trigger-replace

Conversation

@rafiss
Copy link
Copy Markdown
Collaborator

@rafiss rafiss commented Feb 7, 2026

Previously, CREATE OR REPLACE TRIGGER returned an unimplemented error. This commit implements the feature by resolving any existing trigger with the same name on the table and dropping it before creating the replacement trigger with a new trigger ID.

The approach follows the same pattern as DropTrigger: only the Trigger and TriggerDeps elements are explicitly dropped, and the dependent elements (TriggerName, TriggerEnabled, TriggerTiming, etc.) are cleaned up by the existing dependency rules. A new trigger ID is allocated for the replacement because the declarative schema changer does not allow re-adding an element with the same key as one dropped in the same transaction.

The intermediate drop is safe because triggers cannot have dependents (nothing depends on a trigger), so there is no cascade concern.

Fixes: #128422

Release note (sql change): CREATE OR REPLACE TRIGGER is now supported. If a trigger with the same name already exists on the same table, it is replaced with the new definition. If no trigger with that name exists, a new trigger is created.

@trunk-io
Copy link
Copy Markdown
Contributor

trunk-io Bot commented Feb 7, 2026

😎 Merged successfully - details.

@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

Previously, CREATE OR REPLACE TRIGGER returned an unimplemented error.
This commit implements the feature by resolving any existing trigger
with the same name on the table and dropping it before creating the
replacement trigger with a new trigger ID.

The approach follows the same pattern as DropTrigger: only the Trigger
and TriggerDeps elements are explicitly dropped, and the dependent
elements (TriggerName, TriggerEnabled, TriggerTiming, etc.) are cleaned
up by the existing dependency rules. A new trigger ID is allocated for
the replacement because the declarative schema changer does not allow
re-adding an element with the same key as one dropped in the same
transaction.

The intermediate drop is safe because triggers cannot have dependents
(nothing depends on a trigger), so there is no cascade concern.

Fixes: cockroachdb#128422
Epic: CRDB-42942

Release note (sql change): CREATE OR REPLACE TRIGGER is now supported.
If a trigger with the same name already exists on the same table, it is
replaced with the new definition. If no trigger with that name exists, a
new trigger is created.
@rafiss rafiss marked this pull request as ready for review February 11, 2026 19:23
@rafiss rafiss requested a review from a team as a code owner February 11, 2026 19:23
@rafiss rafiss requested review from fqazi and removed request for fqazi February 11, 2026 19:23
Copy link
Copy Markdown
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm_strong:

@fqazi reviewed 4 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @rafiss).

@rafiss
Copy link
Copy Markdown
Collaborator Author

rafiss commented Feb 13, 2026

/trunk merge

tftr!

@trunk-io trunk-io Bot merged commit 0e643f5 into cockroachdb:master Feb 13, 2026
28 checks passed
@rafiss rafiss deleted the trigger-replace branch February 14, 2026 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: support CREATE OR REPLACE for triggers

3 participants