Skip to content

Commit

Permalink
Merge #68540
Browse files Browse the repository at this point in the history
68540: backupccl: add option to display descriptor ids to SHOW BACKUP r=rhu713 a=rhu713

Previously there weren't any descriptor ID information displayed by SHOW BACKUP. These IDs provide
useful information during triage and general debugging. To address this, a WITH option debug_ids
was added to populate additional columns in SHOW BACKUP with descriptor IDS of all objects along
with the IDs of its database and parent schema.

Release note (enterprise change): Descriptor IDs of every object are now visible in SHOW BACKUP,
along with the descriptor IDs of the object's database and parent schema. SHOW BACKUP will display
these IDs if the `WITH debug_ids` option is specified.

Co-authored-by: Rui Hu <[email protected]>
  • Loading branch information
craig[bot] and Rui Hu committed Aug 6, 2021
2 parents cab185f + ab27b06 commit 59b2dd7
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/ccl/backupccl/backup_planning.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const (
backupOptEncKMS = "kms"
backupOptWithPrivileges = "privileges"
backupOptAsJSON = "as_json"
backupOptWithDebugIDs = "debug_ids"
localityURLParam = "COCKROACH_LOCALITY"
defaultLocalityValue = "default"
)
Expand Down
58 changes: 58 additions & 0 deletions pkg/ccl/backupccl/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func showBackupPlanHook(
backupOptEncKMS: sql.KVStringOptRequireValue,
backupOptWithPrivileges: sql.KVStringOptRequireNoValue,
backupOptAsJSON: sql.KVStringOptRequireNoValue,
backupOptWithDebugIDs: sql.KVStringOptRequireNoValue,
}
optsFn, err := p.TypeAsStringOpts(ctx, backup.Options, expected)
if err != nil {
Expand Down Expand Up @@ -260,6 +261,20 @@ func backupShowerHeaders(showSchemas bool, opts map[string]string) colinfo.Resul
baseHeaders = append(baseHeaders, colinfo.ResultColumn{Name: "privileges", Typ: types.String})
baseHeaders = append(baseHeaders, colinfo.ResultColumn{Name: "owner", Typ: types.String})
}

if _, shouldShowIDs := opts[backupOptWithDebugIDs]; shouldShowIDs {
baseHeaders = append(
colinfo.ResultColumns{
baseHeaders[0],
{Name: "database_id", Typ: types.Int},
baseHeaders[1],
{Name: "parent_schema_id", Typ: types.Int},
baseHeaders[2],
{Name: "object_id", Typ: types.Int},
},
baseHeaders[3:]...,
)
}
return baseHeaders
}

Expand Down Expand Up @@ -325,6 +340,9 @@ func backupShowerDefault(
var parentSchemaName string
var descriptorType string

var dbID descpb.ID
var parentSchemaID descpb.ID

createStmtDatum := tree.DNull
dataSizeDatum := tree.DNull
rowCountDatum := tree.DNull
Expand All @@ -338,14 +356,19 @@ func backupShowerDefault(
case catalog.SchemaDescriptor:
descriptorType = "schema"
dbName = dbIDToName[desc.GetParentID()]
dbID = desc.GetParentID()
case catalog.TypeDescriptor:
descriptorType = "type"
dbName = dbIDToName[desc.GetParentID()]
dbID = desc.GetParentID()
parentSchemaName = schemaIDToName[desc.GetParentSchemaID()]
parentSchemaID = desc.GetParentSchemaID()
case catalog.TableDescriptor:
descriptorType = "table"
dbName = dbIDToName[desc.GetParentID()]
dbID = desc.GetParentID()
parentSchemaName = schemaIDToName[desc.GetParentSchemaID()]
parentSchemaID = desc.GetParentSchemaID()
descSize := descSizes[desc.GetID()]
dataSizeDatum = tree.NewDInt(tree.DInt(descSize.DataSize))
rowCountDatum = tree.NewDInt(tree.DInt(descSize.Rows))
Expand Down Expand Up @@ -386,6 +409,20 @@ func backupShowerDefault(
owner := desc.GetPrivileges().Owner().SQLIdentifier()
row = append(row, tree.NewDString(owner))
}
if _, shouldShowIDs := opts[backupOptWithDebugIDs]; shouldShowIDs {
// If showing debug IDs, interleave the IDs with the corresponding object names.
row = append(
tree.Datums{
row[0],
nullIfZero(dbID),
row[1],
nullIfZero(parentSchemaID),
row[2],
nullIfZero(desc.GetID()),
},
row[3:]...,
)
}
rows = append(rows, row)
}
for _, t := range manifest.Tenants {
Expand All @@ -407,6 +444,20 @@ func backupShowerDefault(
if _, shouldShowPrivileges := opts[backupOptWithPrivileges]; shouldShowPrivileges {
row = append(row, tree.DNull)
}
if _, shouldShowIDs := opts[backupOptWithDebugIDs]; shouldShowIDs {
// If showing debug IDs, interleave the IDs with the corresponding object names.
row = append(
tree.Datums{
row[0],
tree.DNull, // Database ID
row[1],
tree.DNull, // Parent Schema ID
row[2],
tree.NewDInt(tree.DInt(t.ID)), // Object ID
},
row[3:]...,
)
}
rows = append(rows, row)
}
}
Expand All @@ -422,6 +473,13 @@ func nullIfEmpty(s string) tree.Datum {
return tree.NewDString(s)
}

func nullIfZero(i descpb.ID) tree.Datum {
if i == 0 {
return tree.DNull
}
return tree.NewDInt(tree.DInt(i))
}

func showPrivileges(descriptor *descpb.Descriptor) string {
var privStringBuilder strings.Builder

Expand Down
65 changes: 65 additions & 0 deletions pkg/ccl/backupccl/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ func TestShowBackupTenants(t *testing.T) {
require.Equal(t, [][]string{
{"/Tenant/10", "/Tenant/11"},
}, res)

res = systemDB.QueryStr(t, `SELECT database_id, parent_schema_id, object_id FROM [SHOW BACKUP 'nodelocal://1/t10' WITH debug_ids]`)
require.Equal(t, [][]string{
{"NULL", "NULL", "10"},
}, res)
}

func TestShowBackupPrivileges(t *testing.T) {
Expand Down Expand Up @@ -586,3 +591,63 @@ func showUpgradedForeignKeysTest(exportDir string) func(t *testing.T) {
}
}
}

func TestShowBackupWithDebugIDs(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

const numAccounts = 11
// Create test database with bank table
_, _, sqlDB, _, cleanupFn := BackupRestoreTestSetup(t, singleNode, numAccounts, InitManualReplication)
defer cleanupFn()

// add 1 type, 1 schema, and 2 tables to the database
sqlDB.Exec(t, `
SET CLUSTER SETTING sql.cross_db_fks.enabled = TRUE;
CREATE TYPE data.welcome AS ENUM ('hello', 'hi');
USE data; CREATE SCHEMA sc;
CREATE TABLE data.sc.t1 (a INT);
CREATE TABLE data.sc.t2 (a data.welcome);
`)

const full = LocalFoo + "/full"

beforeTS := sqlDB.QueryStr(t, `SELECT now()::timestamp::string`)[0][0]
sqlDB.Exec(t, fmt.Sprintf(`BACKUP DATABASE data TO $1 AS OF SYSTEM TIME '%s'`, beforeTS), full)

// extract the object IDs for the database and public schema
databaseRow := sqlDB.QueryStr(t, `
SELECT database_name, database_id, parent_schema_name, parent_schema_id, object_name, object_id, object_type
FROM [SHOW BACKUP $1 WITH debug_ids]
WHERE object_name = 'bank'`, full)
require.NotEmpty(t, databaseRow)
dbID, err := strconv.Atoi(databaseRow[0][1])
require.NoError(t, err)
publicID, err := strconv.Atoi(databaseRow[0][3])
require.NoError(t, err)

require.Greater(t, dbID, 0)
require.Greater(t, publicID, 0)

res := sqlDB.QueryStr(t, `
SELECT database_name, database_id, parent_schema_name, parent_schema_id, object_name, object_id, object_type
FROM [SHOW BACKUP $1 WITH debug_ids]
ORDER BY object_id`, full)

dbIDStr := strconv.Itoa(dbID)
publicIDStr := strconv.Itoa(publicID)
schemaIDStr := strconv.Itoa(dbID + 4)

expectedObjects := [][]string{
{"NULL", "NULL", "NULL", "NULL", "data", dbIDStr, "database"},
{"data", dbIDStr, "public", publicIDStr, "bank", strconv.Itoa(dbID + 1), "table"},
{"data", dbIDStr, "public", publicIDStr, "welcome", strconv.Itoa(dbID + 2), "type"},
{"data", dbIDStr, "public", publicIDStr, "_welcome", strconv.Itoa(dbID + 3), "type"},
{"data", dbIDStr, "NULL", "NULL", "sc", schemaIDStr, "schema"},
{"data", dbIDStr, "sc", schemaIDStr, "t1", strconv.Itoa(dbID + 5), "table"},
{"data", dbIDStr, "sc", schemaIDStr, "t2", strconv.Itoa(dbID + 6), "table"},
}

require.Equal(t, expectedObjects, res)

}

0 comments on commit 59b2dd7

Please sign in to comment.