Skip to content

Commit

Permalink
sql: omit virtual tables from stmt bundles
Browse files Browse the repository at this point in the history
This commit makes it so that we do not include virtual tables into
`schema.sql` file (as well as don't create the stats files for them)
into the stmt bundle. This should make it easier to recreate the bundles
that access virtual tables.

Note that in the future, once we support stats on virtual tables, this
logic will need to be update so that only the create statements are
omitted.

Release note: None
  • Loading branch information
yuzefovich committed Dec 7, 2023
1 parent 2b620c6 commit 8fa4171
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/sql/explain_bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,17 @@ func (b *stmtBundleBuilder) addEnv(ctx context.Context) {
}
buf.Reset()

// TODO(#27611): when we support stats on virtual tables, we'll need to
// update this logic to not include virtual tables into schema.sql but still
// create stats files for them.
var tables, sequences, views []tree.TableName
err := b.db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
var err error
tables, sequences, views, err = mem.Metadata().AllDataSourceNames(
ctx, b.plan.catalog, func(ds cat.DataSource) (cat.DataSourceName, error) {
return b.plan.catalog.fullyQualifiedNameWithTxn(ctx, ds, txn)
},
false, /* includeVirtualTables */
)
return err
})
Expand Down
22 changes: 22 additions & 0 deletions pkg/sql/explain_bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,28 @@ CREATE TABLE users(id UUID DEFAULT gen_random_uuid() PRIMARY KEY, promo_id INT R
base, plans, "distsql.html vec.txt vec-v.txt inflight-trace-n1.txt inflight-trace-jaeger-n1.json",
)
})

t.Run("virtual table", func(t *testing.T) {
rows := r.QueryStr(t, "EXPLAIN ANALYZE (DEBUG) SELECT count(*) FROM pg_catalog.pg_class;")
// tableName is empty since we expect that the table is not included
// into schema.sql.
var tableName string
contentCheck := func(name, contents string) error {
if name != "schema.sql" {
return nil
}
if strings.Contains(contents, "CREATE TABLE pg_catalog.pg_class") {
return errors.New("virtual tables should be omitted from schema.sql")
}
return nil
}
checkBundle(
t, fmt.Sprint(rows), tableName, contentCheck, false, /* expectErrors */
// Note that the list of files doesn't include stats for the virtual
// table - this will probably change when #27611 is addressed.
base, plans, "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 @@ -3619,6 +3619,7 @@ func (b *Builder) getEnvData() (exec.ExplainEnvData, error) {
func(ds cat.DataSource) (cat.DataSourceName, error) {
return b.catalog.FullyQualifiedName(context.TODO(), ds)
},
true, /* includeVirtualTables */
)
return envOpts, err
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/sql/opt/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -987,11 +987,13 @@ func (md *Metadata) getAllReferencedTables(
// AllDataSourceNames returns the fully qualified names of all datasources
// referenced by the metadata. This includes all tables, sequences, and views
// that are directly stored in the metadata, as well as tables that are
// recursively referenced from foreign keys.
// recursively referenced from foreign keys. If includeVirtualTables is false,
// then virtual tables are not returned.
func (md *Metadata) AllDataSourceNames(
ctx context.Context,
catalog cat.Catalog,
fullyQualifiedName func(ds cat.DataSource) (cat.DataSourceName, error),
includeVirtualTables bool,
) (tables, sequences, views []tree.TableName, _ error) {
// Catalog objects can show up multiple times in our lists, so deduplicate
// them.
Expand All @@ -1014,6 +1016,17 @@ func (md *Metadata) AllDataSourceNames(
}
var err error
refTables := md.getAllReferencedTables(ctx, catalog)
if !includeVirtualTables {
// Update refTables in-place to remove all virtual tables.
i := 0
for j := 0; j < len(refTables); j++ {
if t, ok := refTables[j].(cat.Table); !ok || !t.IsVirtualTable() {
refTables[i] = refTables[j]
i++
}
}
refTables = refTables[:i]
}
tables, err = getNames(len(refTables), func(i int) cat.DataSource {
return refTables[i]
})
Expand Down

0 comments on commit 8fa4171

Please sign in to comment.