Skip to content

Commit

Permalink
opt: update statement bundle and EXPLAIN (opt, env) to include FK tables
Browse files Browse the repository at this point in the history
This commit updates the statement bundle and EXPLAIN (opt, env) output
to include the schema and stats for tables that are referenced with foreign
keys by tables used in the query. This is useful when trying to re-create
the schema for debugging purposes (e.g., with cockroach debug
statement-bundle recreate), since otherwise you would get an error
"relation ... does not exist".

Epic: CRDB-14510

Release note: None
  • Loading branch information
rytaft committed Nov 11, 2022
1 parent f87728f commit 48bf2ff
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {
err := b.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
var err error
tables, sequences, views, err = mem.Metadata().AllDataSourceNames(
func(ds cat.DataSource) (cat.DataSourceName, error) {
ctx, b.plan.catalog, func(ds cat.DataSource) (cat.DataSourceName, error) {
return b.plan.catalog.fullyQualifiedNameWithTxn(ctx, ds, txn)
},
)
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,26 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
t.Fatalf("warning not found in %v", rows)
}
})

t.Run("foreign keys", func(t *testing.T) {
r.Exec(t, "CREATE TABLE parent (pk INT PRIMARY KEY, v INT);")
r.Exec(t, "CREATE TABLE child (pk INT PRIMARY KEY, fk INT REFERENCES parent(pk));")
rows := r.QueryStr(t, "EXPLAIN ANALYZE (DEBUG) SELECT * FROM child")
checkBundle(
t, fmt.Sprint(rows), "child", func(name, contents string) error {
if name == "schema.sql" {
reg := regexp.MustCompile("CREATE TABLE public.parent")
if reg.FindString(contents) == "" {
return errors.Newf(
"could not find 'CREATE TABLE public.parent' in schema.sql:\n%s", contents)
}
}
return nil
},
base, plans, "stats-defaultdb.public.parent.sql", "stats-defaultdb.public.child.sql",
"distsql.html vec.txt vec-v.txt",
)
})
}

// checkBundle searches text strings for a bundle URL and then verifies that the
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/opt/exec/execbuilder/relational.go
Original file line number Diff line number Diff line change
Expand Up @@ -3220,6 +3220,7 @@ func (b *Builder) getEnvData() exec.ExplainEnvData {
envOpts := exec.ExplainEnvData{ShowEnv: true}
var err error
envOpts.Tables, envOpts.Sequences, envOpts.Views, err = b.mem.Metadata().AllDataSourceNames(
b.ctx, b.catalog,
func(ds cat.DataSource) (cat.DataSourceName, error) {
return b.catalog.FullyQualifiedName(context.TODO(), ds)
},
Expand Down
Loading

0 comments on commit 48bf2ff

Please sign in to comment.