Skip to content

Conversation

@DrewKimball
Copy link
Collaborator

@DrewKimball DrewKimball commented Dec 10, 2025

sql: still more routine dependency improvements

I missed in #158935 that check constraints and partial index predicates
are additional places where we unnecessarily track transitive column
dependencies when creating a routine. The fix is gated behind the same
session setting as #158935.

Fixes #158154

Release note (sql change): Fixed two cases where creation of a routine
resolves unnecessary column dependencies, which can prevent drop of the
column without first dropping the routine. Here, the unnecessary
dependencies are due to references within CHECK constraints, including
those for RLS policies and hash-sharded indexes, as well as those in
partial index predicates. The fix is gated behind the session setting
use_improved_routine_deps_triggers_and_computed_cols, which is off by
default before 26.1.

@DrewKimball DrewKimball requested a review from a team as a code owner December 10, 2025 05:27
@cockroach-teamcity
Copy link
Member

This change is Reviewable

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: Nice catch!

Does this fix the case for enums, which also synthesize check constraints? Is it worth adding this test:

CREATE TYPE typ AS ENUM ('foo', 'bar');

CREATE TABLE t (a INT, b typ);

CREATE PROCEDURE p() LANGUAGE SQL AS $$
  INSERT INTO t (a) VALUES (1);
  UPDATE t SET a = a + 1;
$$;

CALL p();

ALTER TABLE t DROP COLUMN b;

CALL p();

We don't have to worry about partial indexes for now, because of #97813. I'll add a note to that issue pointing this out. If you think it's worth a TODO in the code where we build partial index predicates, then you can add one.

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

Copy link
Collaborator

@ZhouXing19 ZhouXing19 left a comment

Choose a reason for hiding this comment

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

:lgtm: Nice find!!

@ZhouXing19 reviewed 4 of 4 files at r1, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @DrewKimball)

I missed in cockroachdb#158935 that check constraints and partial index predicates
are additional places where we unnecessarily track transitive column
dependencies when creating a routine. The fix is gated behind the same
session setting as cockroachdb#158935.

Fixes cockroachdb#158154

Release note (sql change): Fixed two cases where creation of a routine
resolves unnecessary column dependencies, which can prevent drop of the
column without first dropping the routine. Here, the unnecessary
dependencies are due to references within CHECK constraints, including
those for RLS policies and hash-sharded indexes, as well as those in
partial index predicates. The fix is gated behind the session setting
`use_improved_routine_deps_triggers_and_computed_cols`, which is off by
default before 26.1.
@DrewKimball
Copy link
Collaborator Author

@mgartner good point about enums and partial indexes. I added an enum test (already fixed), but noticed that there were several more cases we didn't handle for partial indexes. Those are now fixed, with test cases added.

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:

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


pkg/sql/opt/optbuilder/partial_index.go line 128 at r2 (raw file):

		// dependencies on any UDTs or columns referenced by the predicate. The
		// partial index will already prevent dropping such UDTs or columns.
		defer b.DisableSchemaDepTracking()()

I'm wondering if this is the only addition you need for partial indexes. It looks like the three others you added all lead down to this method - the metadata and two arbiter cases. Did you try making just this one addition?

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.

optbuilder: don't add transitive column dependencies to routines

4 participants