Skip to content

Conversation

mgartner
Copy link
Collaborator

@mgartner mgartner commented Oct 3, 2025

This commit fixes a bug that was caused by incorrectly modifying the set
and list of target columns when re-projecting computed columns after
building a BEFORE trigger.

Fixes #154672

Release note (bug fix): A bug has been fixed that caused internal errors
for INSERT .. ON CONFLICT .. DO UPDATE statements when the target
table had both a computed column and a BEFORE trigger. This bug has
been present since triggers were introduced in v24.3.0.

@mgartner mgartner requested review from a team and DrewKimball October 3, 2025 19:52
@mgartner mgartner requested a review from a team as a code owner October 3, 2025 19:52
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@mgartner
Copy link
Collaborator Author

mgartner commented Oct 3, 2025

This feels a bit hacky, but gets the job done. Probably worth thinking if there is a better way to fix this.

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

It seems that targetCols is only used to keep track of the columns directly being inserted into or being set by an update. I think the right way to fix it is actually to have addSynthesizedComputedCols skip appending to targetCols when called for a trigger. What do you think?

I think the mutationBuilder could use some refactoring. That field is really only meant to be used "locally" while building the input for an insert or an update (and is unset after that's done), but that's not really clear from the comments.

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained

@mgartner
Copy link
Collaborator Author

mgartner commented Oct 6, 2025

It seems that targetCols is only used to keep track of the columns directly being inserted into or being set by an update. I think the right way to fix it is actually to have addSynthesizedComputedCols skip appending to targetCols when called for a trigger. What do you think?

I like it. I tried something similar on Friday, but I forgot why I abandoned it. Let me try again.

@mgartner mgartner force-pushed the 154672-trigger-computed-col branch from 8f13f74 to f88f3b1 Compare October 6, 2025 23:08
@mgartner mgartner requested a review from DrewKimball October 6, 2025 23:09
Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm: Thanks!

@DrewKimball reviewed 7 of 7 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @mgartner)

func (mb *mutationBuilder) addSynthesizedComputedCols(colIDs opt.OptionalColList, restrict bool) {
mb.targetColSet = mb.projectSynthesizedComputedCols(colIDs, restrict)
for col, ok := mb.targetColSet.Next(0); ok; col, ok = mb.targetColSet.Next(col + 1) {
mb.targetColList = append(mb.targetColList, col)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think this assumes that table columns have ascending IDs... which is true right now, but maybe not something we want to rely on.

Here's one place where we assume that the order of columns in targetColList is the same as the order of columns in mb.outScope after projectSynthesizedComputedCols returns:

for i := range mb.outScope.cols {
inCol := &mb.outScope.cols[i]
ord := mb.tabID.ColumnOrdinal(mb.targetColList[i])

The need for targetColList is dubious... Maybe we can get rid of it in the near future...

In the meantime I'm going to attempt a safer fix.

This commit fixes a bug that was caused by incorrectly modifying the set
and list of target columns when re-projecting computed columns after
building a `BEFORE` trigger.

Fixes cockroachdb#154672

Release note (bug fix): A bug has been fixed that caused internal errors
for `INSERT .. ON CONFLICT .. DO UPDATE` statements when the target
table had both a computed column and a `BEFORE` trigger. This bug has
been present since triggers were introduced in v24.3.0.
@mgartner mgartner force-pushed the 154672-trigger-computed-col branch from f88f3b1 to 30a89a7 Compare October 7, 2025 14:08
@mgartner mgartner requested a review from DrewKimball October 7, 2025 14:08
@mgartner
Copy link
Collaborator Author

mgartner commented Oct 7, 2025

Ok, third time's the charm. PTAL!

Copy link
Collaborator

@DrewKimball DrewKimball left a comment

Choose a reason for hiding this comment

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

:lgtm:

@DrewKimball reviewed 4 of 4 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

@mgartner mgartner added backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.3.x Flags PRs that need to be backported to 25.3 backport-25.4.x Flags PRs that need to be backported to 25.4 labels Oct 8, 2025
@mgartner
Copy link
Collaborator Author

mgartner commented Oct 8, 2025

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Oct 8, 2025

@craig craig bot merged commit fb3523e into cockroachdb:master Oct 8, 2025
28 checks passed
Copy link

blathers-crl bot commented Oct 8, 2025

Based on the specified backports for this PR, I applied new labels to the following linked issue(s). Please adjust the labels as needed to match the branches actually affected by the issue(s), including adding any known older branches.


Issue #154672: branch-release-24.3, branch-release-25.3, branch-release-25.4.


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

@mgartner mgartner deleted the 154672-trigger-computed-col branch October 8, 2025 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.3.x Flags PRs that need to be backported to 25.3 backport-25.4.x Flags PRs that need to be backported to 25.4 target-release-26.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: v25.2.6: addTargetColsForUpdate cannot be called more than once

3 participants