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 internal tables cross db refs and interleaved indexes/tables #61629

Merged
merged 1 commit into from
Mar 12, 2021

Conversation

fqazi
Copy link
Collaborator

@fqazi fqazi commented Mar 8, 2021

Fixes: #58867

Previously, users had no way of determining which objects in
their database utilized either deprecated features like
interleaved indexes/tables or cross DB references. This was
inadequate since users need to know which object utilize
this functionality. To address this, this patch introduces
the crdb_internal tables: cross_db_references, interleaved_indexes,
interleaved_tables.

Release justification: Low risk internal tables for detecting
deprecated features.
Release note (sql change): Added crdb_internal tables cross_db_references,
interleaved_indexes, and interleaved_tables for detecting the
deprecated features cross db references and interleaved tables
/ indexes within a given database.

@fqazi fqazi requested a review from a team March 8, 2021 16:56
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@fqazi fqazi force-pushed the InterleavedDbRefUsage branch from 175bf56 to f27053f Compare March 8, 2021 19:08
@fqazi fqazi requested a review from a team as a code owner March 8, 2021 19:08
@fqazi fqazi force-pushed the InterleavedDbRefUsage branch 5 times, most recently from 736baf3 to 3d6a1f2 Compare March 9, 2021 00:56
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 @fqazi)


pkg/cli/zip_test.go, line 76 at r1 (raw file):

Quoted 6 lines of code…
  'cross_db_references',
	'databases',
	'forward_dependencies',
	'index_columns',
  'interleaved_indexes',
  'interleaved_tables',

I think this needs a gofmt


pkg/sql/crdb_internal.go, line 4034 at r1 (raw file):

}

var crdbInternalInterleavedTables = virtualSchemaTable{

Is this table useful? I worry that it might be confusing. When would you ever want this and not interleaved_indexes?


pkg/sql/crdb_internal.go, line 4042 at r1 (raw file):

	parent_table  STRING NOT NULL
)`,
	populate: func(ctx context.Context, p *planner, dbContext *dbdesc.Immutable, addRow func(...tree.Datum) error) error {

I feel like this dbContext may surprise you. Are you familiar with the "".crdb_internal.interleaved_tables weird syntax we have for all databases. I think it's just something to be aware of. I suspect that we're going to end up recommending it so we should probably focus on it.


pkg/sql/crdb_internal.go, line 4045 at r1 (raw file):

		return forEachTableDescAll(ctx, p, dbContext, hideVirtual,
			func(db *dbdesc.Immutable, schemaName string, table catalog.TableDescriptor) error {
				if table.IsInterleaved() {

total and complete nit: invert this condition and early return? I feel like the code base tends to prefer that pattern.


pkg/sql/crdb_internal.go, line 4050 at r1 (raw file):

						ancestor := index.GetInterleaveAncestor(index.NumInterleaveAncestors() - 1)
						parentID := ancestor.TableID
						parentTable, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, parentID,

maybe forEachTableDescWithTableLookup which will use the already fetched descriptors. Another thing is that we probably want to look up the parent's database and schema.

@fqazi fqazi force-pushed the InterleavedDbRefUsage branch from 3d6a1f2 to da064d2 Compare March 9, 2021 16:17
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 and @fqazi)


pkg/sql/crdb_internal.go, line 4034 at r1 (raw file):

Previously, ajwerner wrote…

Is this table useful? I worry that it might be confusing. When would you ever want this and not interleaved_indexes?

Done.


pkg/sql/crdb_internal.go, line 4050 at r1 (raw file):

Previously, ajwerner wrote…

maybe forEachTableDescWithTableLookup which will use the already fetched descriptors. Another thing is that we probably want to look up the parent's database and schema.

Done.

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 6 of 13 files at r2.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @fqazi)


pkg/sql/crdb_internal.go, line 4052 at r2 (raw file):

					if index.NumInterleaveAncestors() == 0 {
						return nil
					}

should this be continue? Maybe worth adding a test that would have caught this by having a table with some indexes which are not interleaved?

Perhaps built yourself a closure to invoke per index (I don't feel strongly about that suggestion).


pkg/sql/logictest/testdata/logic_test/pg_catalog, line 2102 at r2 (raw file):

4294967277  4294967189  0         index columns for all indexes accessible by current user in current database (KV scan)
4294967247  4294967189  0         virtual table with interleaved index information
4294967248  4294967189  0         virtual table with interleaved table information

seems stale, may need a --rewrite


pkg/sql/logictest/testdata/logic_test/views, line 905 at r2 (raw file):

statement ok
DROP TABLE t

can you check on views which reference types, sequences, and views in other databases?

@fqazi fqazi force-pushed the InterleavedDbRefUsage branch from da064d2 to 4810e05 Compare March 9, 2021 21:12
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/crdb_internal.go, line 4052 at r2 (raw file):

Previously, ajwerner wrote…
					if index.NumInterleaveAncestors() == 0 {
						return nil
					}

should this be continue? Maybe worth adding a test that would have caught this by having a table with some indexes which are not interleaved?

Perhaps built yourself a closure to invoke per index (I don't feel strongly about that suggestion).

Yes should have been a continue. Done


pkg/sql/logictest/testdata/logic_test/pg_catalog, line 2102 at r2 (raw file):

Previously, ajwerner wrote…

seems stale, may need a --rewrite

Done.


pkg/sql/logictest/testdata/logic_test/views, line 905 at r2 (raw file):

Previously, ajwerner wrote…

can you check on views which reference types, sequences, and views in other databases?

Done.

@fqazi fqazi force-pushed the InterleavedDbRefUsage branch 2 times, most recently from 95da0cc to bea7196 Compare March 11, 2021 18:35
@fqazi fqazi requested a review from ajwerner March 11, 2021 18:35
@fqazi fqazi force-pushed the InterleavedDbRefUsage branch 2 times, most recently from cdbaed1 to a1fc264 Compare March 11, 2021 21:19
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/crdb_internal.go, line 4092 at r3 (raw file):

	comment: `virtual table with cross db references`,
	schema: `
CREATE TABLE crdb_internal.cross_db_references (

nit: this is ugly.


pkg/sql/crdb_internal.go, line 4174 at r4 (raw file):

					// For sequences check if the sequence is owned by
					// a different database.

I think we also want to check the set of used descriptors. Say you do:

CREATE DATABASE db;
CREATE SEQUENCE db.s;
CREATE TABLE t (i INT PRIMARY KEY DEFAULT (nextval('db.s')));

We'll have a use reference but not an ownership.

@fqazi fqazi force-pushed the InterleavedDbRefUsage branch from a1fc264 to cd1a7bb Compare March 11, 2021 22:05
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/crdb_internal.go, line 4174 at r4 (raw file):

Previously, ajwerner wrote…
					// For sequences check if the sequence is owned by
					// a different database.

I think we also want to check the set of used descriptors. Say you do:

CREATE DATABASE db;
CREATE SEQUENCE db.s;
CREATE TABLE t (i INT PRIMARY KEY DEFAULT (nextval('db.s')));

We'll have a use reference but not an ownership.

Done.

@fqazi fqazi requested a review from ajwerner March 11, 2021 22:07
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:

Unfortunately I think you've got some more tests to rewrite due to the new crdb_internal tables. I'm happy when CI is happy.

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


pkg/sql/crdb_internal.go, line 4129 at r5 (raw file):

Quoted 9 lines of code…
								if referencedTable.GetParentSchemaID() == keys.PublicSchemaID {
									refSchemaName = tree.PublicSchema
								} else {
									schema, err := lookupFn.getSchemaByID(referencedTable.GetParentSchemaID())
									if err != nil {
										return err
									}
									refSchemaName = schema.GetName()
								}

factor out the schema name retrieval onto the internalLookupCtx? That code shows up like 5 times.

@fqazi fqazi force-pushed the InterleavedDbRefUsage branch 2 times, most recently from b0ae19b to 2db51e6 Compare March 12, 2021 15:51
Fixes: cockroachdb#58867

Previously, users had no way of determining which objects in
their database utilized either deprecated features like
interleaved indexes/tables or cross DB references. This was
inadequate since users need to know which object utilize
this functionality. To address this, this patch introduces
the crdb_internal tables: cross_db_references, interleaved,

Release justification: Low risk internal tables for detecting
deprecated features.
Release note (sql change): Added crdb_internal tables cross_db_references
and interleaved for detecting the deprecated features cross db
references and interleaved tables / indexes.
@fqazi fqazi force-pushed the InterleavedDbRefUsage branch from 2db51e6 to 52e45e0 Compare March 12, 2021 16:06
@fqazi
Copy link
Collaborator Author

fqazi commented Mar 12, 2021

bors r+

@craig
Copy link
Contributor

craig bot commented Mar 12, 2021

Build succeeded:

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: Add SQL command to view existing interleaved and cross database reference usage
3 participants