Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: add session variable to set schema_locked by default on new tables #143892

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

fqazi
Copy link
Collaborator

@fqazi fqazi commented Apr 3, 2025

This patch will add a new session variable / cluster setting for automatically setting schema_locked:

  1. Adds a new session variable create_table_with_schema_locked and cluster setting sql.defaults.create_table_with_schema_locked, which will create all new tables with schema_locked by default
  2. Updates the schema changer to make sure rollback stages are always non-revertible. Previously, this was implicitly done, and is required for having schema_locked in the entire declarative schema changer test suite.
  3. Update the sql/schemachanger package to always create tables wit schema_locked. This requires not only updating expected files, but also executing setup statements with the declarative schema changer and adjusting some rollback points for DML injection

Release note (sql change): The create_table_with_schema_locked session variable can be used to control if newly created tables should have schema_locked set.
Informs: #129694
Fixes: #143891

@fqazi fqazi requested a review from a team as a code owner April 3, 2025 16:31
Copy link

blathers-crl bot commented Apr 3, 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

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

nice work!

Reviewed 32 of 840 files at r4, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained


-- commits line 11 at r4:
this should get a release note that describes the session variable. (let's not mention the cluster setting)


pkg/ccl/schemachangerccl/testdata/end_to_end/add_column_subzones/add_column_subzones.side_effects line 631 at r4 (raw file):

begin transaction #12
## PostCommitNonRevertiblePhase stage 3 of 4 with 5 MutationType ops
upsert descriptor #104

just curious, this seems fine, but do you know why the order of side effects changed? is it because of the NonRevertible change?


pkg/sql/create_table.go line 2574 at r1 (raw file):

	if !ret.IsView() && !ret.IsSequence() &&
		params.p.SessionData().CreateTableWithSchemaLocked &&
		params.p.IsActive(params.ctx, clusterversion.V25_2) {

if we add another check here for p.SessionData().Internal to skip this for internal executors, could we avoid having to disable it for backup/restore tests?


pkg/sql/schemachanger/sctest/decomp.go line 47 at r4 (raw file):

			// We need to disable the declarative schema changer so that we don't end
			// up high-fiving ourselves here.
			tdb.Exec(t, `SET CLUSTER SETTING sql.defaults.use_declarative_schema_changer = 'off'`)

why is this getting removed? (and also, do you know why was this even here before?)


pkg/sql/catalog/rewrite/rewrite.go line 1180 at r4 (raw file):

		}
		// Rewrite any function descriptors in the schema change state.
		if err := rewriteSchemaChangerState(fnDesc, descriptorRewrites); err != nil {

how did this turn up? was there a bug lurking here from before?

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi)


-- commits line 11 at r4:

Previously, rafiss (Rafi Shamim) wrote…

this should get a release note that describes the session variable. (let's not mention the cluster setting)

if you want to wait for the release ntoe until we toggle the default to true, that could be fine too

@fqazi fqazi force-pushed the createTableLockDefault branch 2 times, most recently from ae070ef to d26ec10 Compare April 10, 2025 20:40
Copy link
Collaborator Author

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)


-- commits line 11 at r4:

Previously, rafiss (Rafi Shamim) wrote…

if you want to wait for the release ntoe until we toggle the default to true, that could be fine too

Done.

Added:

Release note (sql change): Added a new session variable create_table_with_schema_locked, which can be used to ensure all tables created by a session have the storage parameter schema_locked set.


pkg/sql/create_table.go line 2574 at r1 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

if we add another check here for p.SessionData().Internal to skip this for internal executors, could we avoid having to disable it for backup/restore tests?

Done.


pkg/sql/schemachanger/sctest/decomp.go line 47 at r4 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

why is this getting removed? (and also, do you know why was this even here before?)

Its being removed so that the entire setup phase uses the declarative schema changer, previously it would run in the legacy schema changer. This prevents schema_locked errors from getting hit.

The test doesn't use any testing knobs, so this seems completely unnecessary. I'm guessing we had broken fallbacks or something, which is no longer an issue.


pkg/ccl/schemachangerccl/testdata/end_to_end/add_column_subzones/add_column_subzones.side_effects line 631 at r4 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

just curious, this seems fine, but do you know why the order of side effects changed? is it because of the NonRevertible change?

This is linked to schema_locked. Stage 3 is doing the same but not unsetting job state since its not the final stage. Stage 4 clears all the job stuff and flips schema_locked back on again


pkg/sql/catalog/rewrite/rewrite.go line 1180 at r4 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

how did this turn up? was there a bug lurking here from before?

Done.

I can't seem to hit this anymore and I wonder if it was just because of a bad testing know when I was developing. Unless we use transactions we really shouldn't need to persist anything in the function descriptor yet. So removing this for now.

Copy link
Collaborator Author

@fqazi fqazi left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)


pkg/sql/catalog/rewrite/rewrite.go line 1180 at r4 (raw file):

Previously, fqazi (Faizan Qazi) wrote…

Done.

I can't seem to hit this anymore and I wonder if it was just because of a bad testing know when I was developing. Unless we use transactions we really shouldn't need to persist anything in the function descriptor yet. So removing this for now.

*bad testing knob

@fqazi fqazi requested a review from rafiss April 11, 2025 15:55
Previously, schema_locked had to be manually set on objects during
creation. This meant that to gain performance improvements on CDC users
would manually have to toggle this on. To address this, this patch adds
a cluster setting and session variable for creating new tables with
schema_locked set.

Informs: cockroachdb#129694
Release note (sql change): Added a new session variable
create_table_with_schema_locked, which can be used to ensure all tables
created by a session have the storage parameter schema_locked set.
fqazi added 3 commits April 11, 2025 12:06
Previously, we would plan rollbacks starting from "PostCommitPhase", and
relied on opgen to label things as non-revertible. This would ensure
that majority of plans would get non-revertible implicitly. This can be
problematic if we automatically set/unset schema_locked and certain
plans will no longer start as non-revertible on rollback. To address
this, this patch explicitly ensures that rollbacks start as
non-revertible.

Informs: cockroachdb#129694

Release note: None
Previously, trigger dependents used the NotImplementedForPublicObjects
operation to indicate that no operations were needed. Sadly because
triggers are not descriptors, this could lead to assertions in more
complex plans, when schema_locked is involved. To address this, this
patch modifies this operation to accept a trigger ID to detect if the
trigger is dropped.

Release note: None
Previously, there were individual tests for confirming if schema_locked
worked correctly in the declarative schema changer. This patch flips
create_table_with_schema_locked and update tests to handle this.
Specifically it does the following:

1) All tables are created as schema_locked in the declarative schema
   changer tests
2) All setup statements are now run via the declarative schema changer
   so that schema_locked is almost never manually toggled
3) Updates all test outputs to expected schema_locked

Informs: cockroachdb#129694
Release note: None
@fqazi fqazi force-pushed the createTableLockDefault branch from d26ec10 to dabb49e Compare April 11, 2025 16:23
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/schemachanger: CREATE INDEX falls back for index expressions
3 participants