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 schema_name,table_id to crdb_internal.ranges #59865

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2494,7 +2494,9 @@ CREATE VIEW crdb_internal.ranges AS SELECT
start_pretty,
end_key,
end_pretty,
table_id,
database_name,
schema_name,
table_name,
index_name,
replicas,
Expand All @@ -2512,7 +2514,9 @@ FROM crdb_internal.ranges_no_leases
{Name: "start_pretty", Typ: types.String},
{Name: "end_key", Typ: types.Bytes},
{Name: "end_pretty", Typ: types.String},
{Name: "table_id", Typ: types.Int},
{Name: "database_name", Typ: types.String},
{Name: "schema_name", Typ: types.String},
{Name: "table_name", Typ: types.String},
{Name: "index_name", Typ: types.String},
{Name: "replicas", Typ: types.Int2Vector},
Expand All @@ -2537,7 +2541,9 @@ CREATE TABLE crdb_internal.ranges_no_leases (
start_pretty STRING NOT NULL,
end_key BYTES NOT NULL,
end_pretty STRING NOT NULL,
table_id INT NOT NULL,
database_name STRING NOT NULL,
schema_name STRING NOT NULL,
table_name STRING NOT NULL,
index_name STRING NOT NULL,
replicas INT[] NOT NULL,
Expand All @@ -2557,20 +2563,25 @@ CREATE TABLE crdb_internal.ranges_no_leases (
// TODO(knz): maybe this could use internalLookupCtx.
dbNames := make(map[uint32]string)
tableNames := make(map[uint32]string)
schemaNames := make(map[uint32]string)
indexNames := make(map[uint32]map[uint32]string)
schemaParents := make(map[uint32]uint32)
parents := make(map[uint32]uint32)
for _, desc := range descs {
id := uint32(desc.GetID())
switch desc := desc.(type) {
case catalog.TableDescriptor:
parents[id] = uint32(desc.GetParentID())
schemaParents[id] = uint32(desc.GetParentSchemaID())
tableNames[id] = desc.GetName()
indexNames[id] = make(map[uint32]string)
for _, idx := range desc.PublicNonPrimaryIndexes() {
indexNames[id][uint32(idx.GetID())] = idx.GetName()
}
case *dbdesc.Immutable:
dbNames[id] = desc.GetName()
case *schemadesc.Immutable:
schemaNames[id] = desc.GetName()
}
}
ranges, err := kvclient.ScanMetaKVs(ctx, p.txn, roachpb.Span{
Expand Down Expand Up @@ -2637,8 +2648,18 @@ CREATE TABLE crdb_internal.ranges_no_leases (
}
}

var dbName, tableName, indexName string
if _, tableID, err := p.ExecCfg().Codec.DecodeTablePrefix(desc.StartKey.AsRawKey()); err == nil {
var dbName, schemaName, tableName, indexName string
var tableID uint32
if _, tableID, err = p.ExecCfg().Codec.DecodeTablePrefix(desc.StartKey.AsRawKey()); err == nil {
schemaParent := schemaParents[tableID]
if schemaParent != 0 {
schemaName = schemaNames[schemaParent]
} else {
// This case shouldn't happen - all schema ids should be available in the
// schemaParents map. If it's not, just assume the name of the schema
// is public to avoid problems.
schemaName = string(tree.PublicSchemaName)
}
parent := parents[tableID]
if parent != 0 {
tableName = tableNames[tableID]
Expand All @@ -2662,7 +2683,9 @@ CREATE TABLE crdb_internal.ranges_no_leases (
tree.NewDString(keys.PrettyPrint(nil /* valDirs */, desc.StartKey.AsRawKey())),
tree.NewDBytes(tree.DBytes(desc.EndKey)),
tree.NewDString(keys.PrettyPrint(nil /* valDirs */, desc.EndKey.AsRawKey())),
tree.NewDInt(tree.DInt(tableID)),
tree.NewDString(dbName),
tree.NewDString(schemaName),
tree.NewDString(tableName),
tree.NewDString(indexName),
votersArr,
Expand Down
8 changes: 4 additions & 4 deletions pkg/sql/logictest/testdata/logic_test/crdb_internal
Original file line number Diff line number Diff line change
Expand Up @@ -255,15 +255,15 @@ SELECT * FROM crdb_internal.node_inflight_trace_spans WHERE span_id < 0
----
trace_id parent_span_id span_id goroutine_id start_time duration operation

query ITTTTTTTTTTTTI colnames
query ITTTTITTTTTTTTTI colnames
SELECT * FROM crdb_internal.ranges WHERE range_id < 0
----
range_id start_key start_pretty end_key end_pretty database_name table_name index_name replicas replica_localities learner_replicas split_enforced_until lease_holder range_size
range_id start_key start_pretty end_key end_pretty table_id database_name schema_name table_name index_name replicas replica_localities learner_replicas split_enforced_until lease_holder range_size

query ITTTTTTTTTTT colnames
query ITTTTITTTTTTTT colnames
SELECT * FROM crdb_internal.ranges_no_leases WHERE range_id < 0
----
range_id start_key start_pretty end_key end_pretty database_name table_name index_name replicas replica_localities learner_replicas split_enforced_until
range_id start_key start_pretty end_key end_pretty table_id database_name schema_name table_name index_name replicas replica_localities learner_replicas split_enforced_until

statement ok
INSERT INTO system.zones (id, config) VALUES
Expand Down