Skip to content

sql/schemachanger: implement CREATE OR REPLACE TRIGGER#162633

Draft
rafiss wants to merge 1 commit intocockroachdb:masterfrom
rafiss:trigger-replace
Draft

sql/schemachanger: implement CREATE OR REPLACE TRIGGER#162633
rafiss wants to merge 1 commit intocockroachdb:masterfrom
rafiss:trigger-replace

Conversation

@rafiss
Copy link
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.

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

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.

Epic: None

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@trunk-io
Copy link
Contributor

trunk-io bot commented Feb 7, 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.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

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.

sql: support CREATE OR REPLACE for triggers

2 participants