Skip to content

Commit

Permalink
sql: add schema.sql and stats.sql back to redacted statement bundles
Browse files Browse the repository at this point in the history
Use `SHOW CREATE ... WITH REDACT` to generate `CREATE` statements for
schema.sql in redacted statement diagnostics bundles.

Also use statistics without histograms in redacted bundles.

Part of: cockroachdb#68570

Epic: CRDB-19756

Release note: None
  • Loading branch information
michae2 committed Mar 16, 2023
1 parent 4fe1100 commit a4601cc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
32 changes: 20 additions & 12 deletions pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,6 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {

b.z.AddFile("env.sql", buf.String())

if b.flags.RedactValues {
b.z.AddFile("schema.sql", "-- schema redacted\n")
return
}

mem := b.plan.mem
if mem == nil {
// No optimizer plans; an error must have occurred during planning.
Expand Down Expand Up @@ -466,13 +461,13 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {
}
for i := range tables {
blankLine()
if err := c.PrintCreateTable(&buf, &tables[i]); err != nil {
if err := c.PrintCreateTable(&buf, &tables[i], b.flags.RedactValues); err != nil {
fmt.Fprintf(&buf, "-- error getting schema for table %s: %v\n", tables[i].String(), err)
}
}
for i := range views {
blankLine()
if err := c.PrintCreateView(&buf, &views[i]); err != nil {
if err := c.PrintCreateView(&buf, &views[i], b.flags.RedactValues); err != nil {
fmt.Fprintf(&buf, "-- error getting schema for view %s: %v\n", views[i].String(), err)
}
}
Expand All @@ -482,7 +477,8 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {
b.z.AddFile("schema.sql", buf.String())
for i := range tables {
buf.Reset()
if err := c.PrintTableStats(&buf, &tables[i], false /* hideHistograms */); err != nil {
hideHistograms := b.flags.RedactValues
if err := c.PrintTableStats(&buf, &tables[i], hideHistograms); err != nil {
fmt.Fprintf(&buf, "-- error getting statistics for table %s: %v\n", tables[i].String(), err)
}
b.z.AddFile(fmt.Sprintf("stats-%s.sql", tables[i].String()), buf.String())
Expand Down Expand Up @@ -771,9 +767,15 @@ func (c *stmtEnvCollector) PrintClusterSettings(w io.Writer) error {
return nil
}

func (c *stmtEnvCollector) PrintCreateTable(w io.Writer, tn *tree.TableName) error {
func (c *stmtEnvCollector) PrintCreateTable(
w io.Writer, tn *tree.TableName, redactValues bool,
) error {
var formatOption string
if redactValues {
formatOption = " WITH REDACT"
}
createStatement, err := c.query(
fmt.Sprintf("SELECT create_statement FROM [SHOW CREATE TABLE %s]", tn.String()),
fmt.Sprintf("SELECT create_statement FROM [SHOW CREATE TABLE %s%s]", tn.String(), formatOption),
)
if err != nil {
return err
Expand All @@ -793,9 +795,15 @@ func (c *stmtEnvCollector) PrintCreateSequence(w io.Writer, tn *tree.TableName)
return nil
}

func (c *stmtEnvCollector) PrintCreateView(w io.Writer, tn *tree.TableName) error {
func (c *stmtEnvCollector) PrintCreateView(
w io.Writer, tn *tree.TableName, redactValues bool,
) error {
var formatOption string
if redactValues {
formatOption = " WITH REDACT"
}
createStatement, err := c.query(fmt.Sprintf(
"SELECT create_statement FROM [SHOW CREATE VIEW %s]", tn.String(),
"SELECT create_statement FROM [SHOW CREATE VIEW %s%s]", tn.String(), formatOption,
))
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
}
}
return nil
}, "env.sql plan.txt schema.sql statement.sql vec-v.txt vec.txt",
}, "env.sql plan.txt schema.sql statement.sql stats-defaultdb.public.pterosaur.sql vec-v.txt vec.txt",
)
})
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/sql/opt_exec_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,9 @@ func (ef *execFactory) showEnv(plan string, envOpts exec.ExplainEnvData) (exec.N
// statements for tables referenced via FKs in these tables.
for i := range envOpts.Tables {
out.writef("")
if err := c.PrintCreateTable(&out.buf, &envOpts.Tables[i]); err != nil {
if err := c.PrintCreateTable(
&out.buf, &envOpts.Tables[i], false, /* redactValues */
); err != nil {
return nil, err
}
out.writef("")
Expand All @@ -1252,7 +1254,7 @@ func (ef *execFactory) showEnv(plan string, envOpts exec.ExplainEnvData) (exec.N

for i := range envOpts.Views {
out.writef("")
if err := c.PrintCreateView(&out.buf, &envOpts.Views[i]); err != nil {
if err := c.PrintCreateView(&out.buf, &envOpts.Views[i], false /* redactValues */); err != nil {
return nil, err
}
}
Expand Down

0 comments on commit a4601cc

Please sign in to comment.