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

backupccl: add option to display descriptor ids to SHOW BACKUP #68540

Merged
merged 1 commit into from
Aug 9, 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
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)

}