Skip to content

sql/opt: handle non-unique parent for RBR region inference#171056

Merged
trunk-io[bot] merged 1 commit into
cockroachdb:masterfrom
DrewKimball:duplicate-match
Jun 2, 2026
Merged

sql/opt: handle non-unique parent for RBR region inference#171056
trunk-io[bot] merged 1 commit into
cockroachdb:masterfrom
DrewKimball:duplicate-match

Conversation

@DrewKimball

Copy link
Copy Markdown
Collaborator

When a REGIONAL BY ROW child sets infer_rbr_region_col_using_constraint, the optimizer plans a lookup join into the parent on the FK's non-region columns to infer the region. If those columns aren't a lax key on the parent, the join could return multiple rows per child input row, which tripped an assertion failure in mutation_builder.

Allow this configuration: wrap the LeftJoin in an unordered DistinctOn grouped by the child input's key (added via EnsureKey when needed), selecting an arbitrary matching parent row via FirstAgg on the parent's region column.

Also moves EnsureKey from decorrelate_funcs.go to general_funcs.go so it can be shared with optbuilder.

Fixes #170768

Release note (bug fix): Fixed an internal error that could occur during INSERT/UPDATE/UPSERT on a REGIONAL BY ROW table configured with the infer_rbr_region_col_using_constraint storage parameter when the referenced parent table did not have a unique constraint covering the foreign key's non-region columns. The optimizer now picks an arbitrary matching parent row to infer the region from.

When a REGIONAL BY ROW child sets infer_rbr_region_col_using_constraint,
the optimizer plans a lookup join into the parent on the FK's non-region
columns to infer the region. If those columns aren't a lax key on the
parent, the join could return multiple rows per child input row, which
tripped an assertion failure in mutation_builder.

Allow this configuration: wrap the LeftJoin in an unordered DistinctOn
grouped by the child input's key (added via EnsureKey when needed),
selecting an arbitrary matching parent row via FirstAgg on the parent's
region column.

Also moves EnsureKey from decorrelate_funcs.go to general_funcs.go so it
can be shared with optbuilder.

Fixes cockroachdb#170768

Release note (bug fix): Fixed an internal error that could occur during
INSERT/UPDATE/UPSERT on a REGIONAL BY ROW table configured with the
infer_rbr_region_col_using_constraint storage parameter when the
referenced parent table did not have a unique constraint covering the
foreign key's non-region columns. The optimizer now picks an arbitrary
matching parent row to infer the region from.
@DrewKimball DrewKimball requested a review from a team as a code owner May 27, 2026 23:56
@DrewKimball DrewKimball requested review from michae2 and removed request for a team May 27, 2026 23:56
@trunk-io

trunk-io Bot commented May 27, 2026

Copy link
Copy Markdown
Contributor

😎 Merged successfully - details.

@cockroach-teamcity

Copy link
Copy Markdown
Member

This change is Reviewable

@DrewKimball DrewKimball requested review from a team and mw5h and removed request for a team and michae2 May 27, 2026 23:58

@mw5h mw5h left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

@mw5h made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained.

@DrewKimball

Copy link
Copy Markdown
Collaborator Author

TFTR!

/trunk merge

@blathers-crl

blathers-crl Bot commented Jun 2, 2026

Copy link
Copy Markdown

Detected infrastructure failure on trunk-merge branch (matched: self-hosted runner lost communication with the server). Automatically resubmitting to merge queue (attempt 1 of 2). (run link)

@blathers-crl

blathers-crl Bot commented Jun 2, 2026

Copy link
Copy Markdown

/trunk merge

@trunk-io trunk-io Bot merged commit 366f3ff into cockroachdb:master Jun 2, 2026
25 checks passed
@DrewKimball DrewKimball deleted the duplicate-match branch June 2, 2026 03:43
@DrewKimball DrewKimball added backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 backport-26.2.x Flags PRs that need to be backported to 26.2 labels Jul 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 backport-26.2.x Flags PRs that need to be backported to 26.2 target-release-26.3.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql,opt: infer_rbr_region_col_using_constraint can panic when parent's non-region FK columns are not unique

3 participants