Skip to content

Conversation

@DrewKimball
Copy link
Collaborator

sql: track SET columns in routine dependencies

Previously, we did not track dependencies on columns in the SET clause
of an UPDATE statement while creating a routine. This meant that it was
possible to break a routine by dropping such a column. This commit fixes
the bug by adding target columns that are explicitly referenced by an
UPDATE statement to the routine dependencies. The new behavior is gated
behind the preexisting variable use_improved_routine_dependency_tracking.

Fixes #158898

Release note (bug fix): Fixed a bug that allowed a column to be dropped
from its table even if a routine referenced that column in the SET clause
of an UPDATE statement. This fix only applies to newly-created routines.
In versions prior to v25.3, the fix must be enabled by setting the session
variable use_improved_routine_dependency_tracking.

sql/logictest: rearrange trigger logic test

This commit pulls out some re-arranging of the triggers logic test
to avoid a confusing diff in the following commmit.

Epic: None
Release note: None

sql: avoid unnecessary column dependencies in routines

Previously, there were several cases where routines would add unnecessary
column dependencies:

  • Columns referenced by a computed column expression would be added to
    the list of columns referenced by the routine.
  • BEFORE triggers would confuse the logic used to distinguish explicit
    from implicit INSERT columns, because triggers change the column IDs
    to be inserted. This could cause the routine to add implicitly referenced
    columns to its dependencies.
  • Dependencies from statements within a trigger would transitively apply
    to a routine that invoked trigger on a mutation.

This commit prevents newly-created routines from adding unnecessary
column dependencies in those cases, gated behind the preexisting
session setting use_improved_routine_dependency_tracking.

Fixes #158154

Release note (bug fix): Fixed a bug that caused routines to prevent
dropping more columns than necessary, most notably columns referenced
by computed column expressions. The fix is gated behind the session
setting use_improved_routine_dependency_tracking, which is off by
default prior to v25.3.

Previously, we did not track dependencies on columns in the SET clause
of an UPDATE statement while creating a routine. This meant that it was
possible to break a routine by dropping such a column. This commit fixes
the bug by adding target columns that are explicitly referenced by an
UPDATE statement to the routine dependencies. The new behavior is gated
behind the preexisting variable `use_improved_routine_dependency_tracking`.

Fixes cockroachdb#158898

Release note (bug fix): Fixed a bug that allowed a column to be dropped
from its table even if a routine referenced that column in the SET clause
of an UPDATE statement. This fix only applies to newly-created routines.
In versions prior to v25.3, the fix must be enabled by setting the session
variable `use_improved_routine_dependency_tracking`.
This commit pulls out some re-arranging of the `triggers` logic test
to avoid a confusing diff in the following commmit.

Epic: None
Release note: None
Previously, there were several cases where routines would add unnecessary
column dependencies:
* Columns referenced by a computed column expression would be added to
  the list of columns referenced by the routine.
* BEFORE triggers would confuse the logic used to distinguish explicit
  from implicit INSERT columns, because triggers change the column IDs
  to be inserted. This could cause the routine to add implicitly referenced
  columns to its dependencies.
* Dependencies from statements within a trigger would transitively apply
  to a routine that invoked trigger on a mutation.

This commit prevents newly-created routines from adding unnecessary
column dependencies in those cases, gated behind the preexisting
session setting `use_improved_routine_dependency_tracking`.

Fixes cockroachdb#158154

Release note (bug fix): Fixed a bug that caused routines to prevent
dropping more columns than necessary, most notably columns referenced
by computed column expressions. The fix is gated behind the session
setting `use_improved_routine_dependency_tracking`, which is off by
default prior to v25.3.
@DrewKimball DrewKimball requested a review from a team as a code owner December 5, 2025 23:57
@DrewKimball DrewKimball requested review from ZhouXing19 and removed request for a team December 5, 2025 23:57
@blathers-crl
Copy link

blathers-crl bot commented Dec 5, 2025

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

@DrewKimball DrewKimball requested a review from a team December 6, 2025 00:02
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

:lgtm: Very nice!

Unfortunately, I think the safe thing to do is make a new session setting given that use_improved_routine_dependency_tracking is already on by default in 25.3.

@mgartner reviewed 3 of 3 files at r1, 1 of 1 files at r2, 9 of 9 files at r3, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @DrewKimball and @ZhouXing19)


pkg/sql/logictest/testdata/logic_test/udf_update line 382 at r1 (raw file):

ALTER TABLE table_drop DROP COLUMN c;

statement error pgcode 2BP01 pq: cannot drop column "b" because function "f_update" depends on it

Looks like we were already handling the case were column is referenced on the RHS of the assignment, correct? It might be good to make the release note and commit message a little more specific about this.


pkg/sql/opt/optbuilder/mutation_builder.go line 80 at r1 (raw file):

	// explicitTargetColOrds contains the ordinals of the columns in targetColSet
	// that were explicitly specified by the user, e.g. in the target column list
	// of an INSERT or the SET clause of an UPDATE. This is used to determine

Was there a reproducible bug in INSERT too? Or just UPDATE?

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: routine deps don't track SET columns for update optbuilder: don't add transitive column dependencies to routines

3 participants