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: disallow cross-database sequence references #70581

Merged
merged 1 commit into from
Sep 28, 2021

Conversation

fqazi
Copy link
Collaborator

@fqazi fqazi commented Sep 22, 2021

Fixes: #69992

Previously, we incorrectly allowed cross DB references
for sequences. This was inadequate because we are going
to drop support for cross DB references. To address this
this patch will disable support for cross DB sequence
references by default.

Release note (sql change): Disallow cross DB references for sequences by
default. This can be enabled with the cluster setting
sql.cross_db_sequence_references.enabled.

@fqazi fqazi requested review from ajwerner and a team September 22, 2021 16:44
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@fqazi fqazi force-pushed the blockCrossDBSeq branch 3 times, most recently from 5de2540 to 2820a82 Compare September 23, 2021 02:48
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 10 files at r1, all commit messages.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner and @fqazi)


pkg/sql/rename_table.go, line 337 at r1 (raw file):

	// lead to a cross DB reference, and an appropriate
	// error is generated.
	checkDepForCrossDbRef := func(depID descpb.ID, isOwner bool) error {

my preference is that you create some consts like:

type crossDBDepType int
const owner, reference crossDBDepType = 0, 1

or something like that.


pkg/sql/rename_table.go, line 353 at r1 (raw file):

		// For tables return an error based on if we are depending
		// on a view or sequence.
		if tableDesc.IsTable() {

this cascade of conditionals is pretty hard to follow. Can we rework the conditions, perhaps in a switch or nested switches, to be clearer?

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

Seems like this change broke a test:

=== CONT  TestLogic/local/rename_table/rename_table_using_types_to_different_database_not_allowed_55709
    logic.go:1867: 
         
        testdata/logic_test/rename_table:432: 
        expected "this sequence will be OWNED BY a table \"t1s1ref\" in a different database after rename", but no error occurred
--- done: testdata/logic_test/rename_table with config local: 114 tests, 1 failures

Reviewed 1 of 3 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @ajwerner and @fqazi)


pkg/sql/rename_table.go, line 413 at r2 (raw file):

Quoted 11 lines of code…
		case tableDesc.IsSequence() && depType == reference:
			// For sequences dependent objects can only be
			// a relations.
			return errors.WithHintf(
				pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
					"this sequence will be referenced BY a table %q in a different database after rename "+
						"(see the '%s' cluster setting)",
					dependentObject.GetName(),
					allowCrossDatabaseSeqReferencesSetting),
				crossDBReferenceDeprecationHint(),
			)

I think you lost the case for this sequence will be OWNED BY.


pkg/sql/rename_table.go, line 464 at r2 (raw file):

		// check if any column owns a sequence.
		for _, columnDesc := range tableDesc.Columns {
			if !allowCrossDatabaseSeqOwner.Get(&p.execCfg.Settings.SV) {

this two-level setting checking feels like overkill. You already check the setting in checkDepForCrossDbRef, let that be the only check.


pkg/sql/rename_table.go, line 467 at r2 (raw file):

					err := checkDepForCrossDbRef(ownsSequenceID, owner)
					if err != nil {

uber-nit: you can inline the error definitions of all of these checks.

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 @ajwerner)


pkg/sql/rename_table.go, line 413 at r2 (raw file):

Previously, ajwerner wrote…
		case tableDesc.IsSequence() && depType == reference:
			// For sequences dependent objects can only be
			// a relations.
			return errors.WithHintf(
				pgerror.Newf(pgcode.ObjectNotInPrerequisiteState,
					"this sequence will be referenced BY a table %q in a different database after rename "+
						"(see the '%s' cluster setting)",
					dependentObject.GetName(),
					allowCrossDatabaseSeqReferencesSetting),
				crossDBReferenceDeprecationHint(),
			)

I think you lost the case for this sequence will be OWNED BY.

Yes, it was accidentally dropped from the switch, can't believe I missed that 🤦‍♂️

@fqazi fqazi requested a review from ajwerner September 28, 2021 13:49
Copy link
Contributor

@ajwerner ajwerner 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 @ajwerner and @fqazi)


pkg/sql/rename_table.go, line 475 at r3 (raw file):

		// check if any column owns a sequence.
		for _, columnDesc := range tableDesc.Columns {
			if !allowCrossDatabaseSeqOwner.Get(&p.execCfg.Settings.SV) {

I'm not super fond of the mixing of levels where we check the settings.

Fixes: cockroachdb#69992

Previously, we incorrectly allowed cross DB references
for sequences. This was inadequate because we are going
to drop support for cross DB references. To address this
this patch will disable support for cross DB sequence
references by default.

Release note (sql change): Disallow cross DB references for sequences by
default. This can be enabled with the cluster setting
sql.cross_db_sequence_references.enabled.
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 @ajwerner)


pkg/sql/rename_table.go, line 475 at r3 (raw file):

Previously, ajwerner wrote…

I'm not super fond of the mixing of levels where we check the settings.

Done.

@fqazi fqazi requested a review from ajwerner September 28, 2021 17:21
Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

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

:lgtm:

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

@fqazi
Copy link
Collaborator Author

fqazi commented Sep 28, 2021

@ajwerner TFTR!

bors r+

@craig
Copy link
Contributor

craig bot commented Sep 28, 2021

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Sep 28, 2021

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Sep 28, 2021

Build succeeded:

@craig craig bot merged commit f1b2494 into cockroachdb:master Sep 28, 2021
@blathers-crl
Copy link

blathers-crl bot commented Sep 28, 2021

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


error creating merge commit from 3b8d7f7 to blathers/backport-release-21.2-70581: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 21.2.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@rafiss
Copy link
Collaborator

rafiss commented Nov 19, 2021

@fqazi @ajwerner friendly ping - does this still need to be backported?

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: disallow cross-database sequence references
4 participants