Skip to content

Commit

Permalink
Add memory accounting for show_create_all_tables builtin.
Browse files Browse the repository at this point in the history
Release justification: None

Release note: None
  • Loading branch information
RichardJCai committed Mar 4, 2021
1 parent 07b98a5 commit e974b23
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 49 deletions.
2 changes: 1 addition & 1 deletion docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2365,7 +2365,7 @@ SQL statement omitting multi-region related zone configuration fields.
If the CONFIGURE ZONE statement can be inferred by the database’s or
table’s zone configuration this will return NULL.</p>
</span></td></tr>
<tr><td><a name="crdb_internal.show_create_all_tables"></a><code>crdb_internal.show_create_all_tables(dbName: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns a rows CREATE table statements followed by
<tr><td><a name="crdb_internal.show_create_all_tables"></a><code>crdb_internal.show_create_all_tables(database_name: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Returns a rows CREATE table statements followed by
ALTER table statements that add table constraints. The rows are ordered
by dependencies. All foreign keys are added after the creation of the table
in the alter statements.
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/opt/norm/project_set_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (c *CustomFuncs) ConstructValuesFromZips(zip memo.ZipExpr) memo.RelExpr {
val := c.f.ConstructConstVal(vals[0], vals[0].ResolvedType())
addValToOutRows(val, j, i)
}
generator.Close()
generator.Close(c.f.evalCtx.Context)

default:
panic(errors.AssertionFailedf("invalid parameter type"))
Expand Down
23 changes: 22 additions & 1 deletion pkg/sql/rowexec/project_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ func newProjectSetProcessor(
processorID,
output,
nil, /* memMonitor */
execinfra.ProcStateOpts{InputsToDrain: []execinfra.RowSource{ps.input}},
execinfra.ProcStateOpts{
InputsToDrain: []execinfra.RowSource{ps.input},
TrailingMetaCallback: func() []execinfrapb.ProducerMetadata {
ps.close()
return nil
},
},
); err != nil {
return nil, err
}
Expand Down Expand Up @@ -278,6 +284,21 @@ func (ps *projectSetProcessor) toEncDatum(d tree.Datum, colIdx int) rowenc.EncDa
return rowenc.DatumToEncDatum(ctyp, d)
}

func (ps *projectSetProcessor) close() {
if ps.InternalClose() {
for _, gen := range ps.gens {
if gen != nil {
gen.Close(ps.Ctx)
}
}
}
}

// ConsumerClosed is part of the RowSource interface.
func (ps *projectSetProcessor) ConsumerClosed() {
ps.close()
}

// ChildCount is part of the execinfra.OpNode interface.
func (ps *projectSetProcessor) ChildCount(verbose bool) int {
if _, ok := ps.input.(execinfra.OpNode); ok {
Expand Down
71 changes: 45 additions & 26 deletions pkg/sql/sem/builtins/generator_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package builtins
import (
"bytes"
"context"
"fmt"
"strings"
"time"

Expand All @@ -33,6 +32,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/errorutil"
"github.com/cockroachdb/cockroach/pkg/util/json"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
Expand Down Expand Up @@ -86,7 +86,7 @@ type aclexplodeGenerator struct{}

func (aclexplodeGenerator) ResolvedType() *types.T { return aclexplodeGeneratorType }
func (aclexplodeGenerator) Start(_ context.Context, _ *kv.Txn) error { return nil }
func (aclexplodeGenerator) Close() {}
func (aclexplodeGenerator) Close(_ context.Context) {}
func (aclexplodeGenerator) Next(_ context.Context) (bool, error) { return false, nil }
func (aclexplodeGenerator) Values() (tree.Datums, error) { return nil, nil }

Expand Down Expand Up @@ -371,7 +371,7 @@ var generators = map[string]builtinDefinition{
},
makeGeneratorOverload(
tree.ArgTypes{
{"dbName", types.String},
{"database_name", types.String},
},
showCreateAllTablesGeneratorType,
makeShowCreateAllTablesGenerator,
Expand Down Expand Up @@ -442,7 +442,7 @@ func makeRegexpSplitToTableGeneratorFactory(hasFlags bool) tree.GeneratorFactory
func (*regexpSplitToTableGenerator) ResolvedType() *types.T { return types.String }

// Close implements the tree.ValueGenerator interface.
func (*regexpSplitToTableGenerator) Close() {}
func (*regexpSplitToTableGenerator) Close(_ context.Context) {}

// Start implements the tree.ValueGenerator interface.
func (g *regexpSplitToTableGenerator) Start(_ context.Context, _ *kv.Txn) error {
Expand Down Expand Up @@ -479,7 +479,7 @@ func makeKeywordsGenerator(_ *tree.EvalContext, _ tree.Datums) (tree.ValueGenera
func (*keywordsValueGenerator) ResolvedType() *types.T { return keywordsValueGeneratorType }

// Close implements the tree.ValueGenerator interface.
func (*keywordsValueGenerator) Close() {}
func (*keywordsValueGenerator) Close(_ context.Context) {}

// Start implements the tree.ValueGenerator interface.
func (k *keywordsValueGenerator) Start(_ context.Context, _ *kv.Txn) error {
Expand Down Expand Up @@ -631,7 +631,7 @@ func (s *seriesValueGenerator) Start(_ context.Context, _ *kv.Txn) error {
}

// Close implements the tree.ValueGenerator interface.
func (s *seriesValueGenerator) Close() {}
func (s *seriesValueGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (s *seriesValueGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -682,7 +682,7 @@ func (s *multipleArrayValueGenerator) Start(_ context.Context, _ *kv.Txn) error
}

// Close implements the tree.ValueGenerator interface.
func (s *multipleArrayValueGenerator) Close() {}
func (s *multipleArrayValueGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (s *multipleArrayValueGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -731,7 +731,7 @@ func (s *arrayValueGenerator) Start(_ context.Context, _ *kv.Txn) error {
}

// Close implements the tree.ValueGenerator interface.
func (s *arrayValueGenerator) Close() {}
func (s *arrayValueGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (s *arrayValueGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -780,7 +780,7 @@ func (s *expandArrayValueGenerator) Start(_ context.Context, _ *kv.Txn) error {
}

// Close implements the tree.ValueGenerator interface.
func (s *expandArrayValueGenerator) Close() {}
func (s *expandArrayValueGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (s *expandArrayValueGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -853,7 +853,7 @@ func (s *subscriptsValueGenerator) Start(_ context.Context, _ *kv.Txn) error {
}

// Close implements the tree.ValueGenerator interface.
func (s *subscriptsValueGenerator) Close() {}
func (s *subscriptsValueGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (s *subscriptsValueGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -898,7 +898,7 @@ func (s *unaryValueGenerator) Start(_ context.Context, _ *kv.Txn) error {
}

// Close implements the tree.ValueGenerator interface.
func (s *unaryValueGenerator) Close() {}
func (s *unaryValueGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (s *unaryValueGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -1003,7 +1003,7 @@ func (g *jsonArrayGenerator) Start(_ context.Context, _ *kv.Txn) error {
}

// Close implements the tree.ValueGenerator interface.
func (g *jsonArrayGenerator) Close() {}
func (g *jsonArrayGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (g *jsonArrayGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -1072,7 +1072,7 @@ func (g *jsonObjectKeysGenerator) ResolvedType() *types.T {
func (g *jsonObjectKeysGenerator) Start(_ context.Context, _ *kv.Txn) error { return nil }

// Close implements the tree.ValueGenerator interface.
func (g *jsonObjectKeysGenerator) Close() {}
func (g *jsonObjectKeysGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (g *jsonObjectKeysGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -1168,7 +1168,7 @@ func (g *jsonEachGenerator) Start(_ context.Context, _ *kv.Txn) error {
}

// Close implements the tree.ValueGenerator interface.
func (g *jsonEachGenerator) Close() {}
func (g *jsonEachGenerator) Close(_ context.Context) {}

// Next implements the tree.ValueGenerator interface.
func (g *jsonEachGenerator) Next(_ context.Context) (bool, error) {
Expand Down Expand Up @@ -1300,7 +1300,7 @@ func (c *checkConsistencyGenerator) Values() (tree.Datums, error) {
}

// Close is part of the tree.ValueGenerator interface.
func (c *checkConsistencyGenerator) Close() {}
func (c *checkConsistencyGenerator) Close(_ context.Context) {}

// rangeKeyIteratorChunkSize is the number of K/V pairs that the
// rangeKeyIterator requests at a time. If this changes, make sure
Expand Down Expand Up @@ -1422,7 +1422,7 @@ func (rk *rangeKeyIterator) Values() (tree.Datums, error) {
}

// Close implements the tree.ValueGenerator interface.
func (rk *rangeKeyIterator) Close() {}
func (rk *rangeKeyIterator) Close(_ context.Context) {}

var payloadsForSpanGeneratorLabels = []string{"payload_type", "payload_jsonb"}

Expand Down Expand Up @@ -1552,7 +1552,7 @@ func (p *payloadsForSpanGenerator) Values() (tree.Datums, error) {
}

// Close implements the tree.ValueGenerator interface.
func (p *payloadsForSpanGenerator) Close() {}
func (p *payloadsForSpanGenerator) Close(_ context.Context) {}

var payloadsForTraceGeneratorLabels = []string{"span_id", "payload_type", "payload_jsonb"}

Expand Down Expand Up @@ -1629,7 +1629,7 @@ func (p *payloadsForTraceGenerator) Values() (tree.Datums, error) {
}

// Close implements the tree.ValueGenerator interface.
func (p *payloadsForTraceGenerator) Close() {
func (p *payloadsForTraceGenerator) Close(_ context.Context) {
err := p.it.Close()
if err != nil {
// TODO(angelapwen, yuzefovich): The iterator's error should be surfaced here.
Expand Down Expand Up @@ -1657,6 +1657,7 @@ type showCreateAllTablesGenerator struct {
timestamp string
ids []int64
dbName string
acc mon.BoundAccount

// The following variables are updated during
// calls to Next() and change throughout the lifecycle of
Expand All @@ -1676,7 +1677,20 @@ func (s *showCreateAllTablesGenerator) ResolvedType() *types.T {

// Start implements the tree.ValueGenerator interface.
func (s *showCreateAllTablesGenerator) Start(ctx context.Context, txn *kv.Txn) error {
ids, err := getTopologicallySortedTableIDs(ctx, s.ie, txn, s.dbName, s.timestamp)
// Note: All the table ids are accumulated in ram before the generator
// starts generating values.
// This is reasonable under the assumption that:
// This uses approximately the same amount of memory as required when
// generating the vtable crdb_internal.show_create_statements. If generating
// and reading from the vtable succeeds which we do to retrieve the ids, then
// it is reasonable to use the same amount of memory to hold the ids in
// ram during the lifecycle of showCreateAllTablesGenerator.
//
// We also account for the memory in the BoundAccount memory monitor in
// showCreateAllTablesGenerator.
ids, err := getTopologicallySortedTableIDs(
ctx, s.ie, txn, s.dbName, s.timestamp, &s.acc,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -1707,16 +1721,14 @@ func (s *showCreateAllTablesGenerator) Next(ctx context.Context) (bool, error) {
return false, err
}
createStmtStr := string(tree.MustBeDString(createStmt))
s.curr = tree.NewDString(fmt.Sprintf("%s;", createStmtStr))
s.curr = tree.NewDString(createStmtStr + ";")
case alterAddFks, alterValidateFks:
// We have existing alter statements to generate for the current
// table id.
s.alterArrIdx++
if s.alterArrIdx < len(s.alterArr) {
alterStmt := tree.MustBeDString(s.alterArr[s.alterArrIdx])
s.curr = tree.NewDString(
fmt.Sprintf("%s;", alterStmt),
)
alterStmtStr := string(tree.MustBeDString(s.alterArr[s.alterArrIdx]))
s.curr = tree.NewDString(alterStmtStr + ";")

// At least one FK was added, we must validate the FK.
s.shouldValidate = true
Expand Down Expand Up @@ -1773,7 +1785,9 @@ func (s *showCreateAllTablesGenerator) Values() (tree.Datums, error) {
}

// Close implements the tree.ValueGenerator interface.
func (s *showCreateAllTablesGenerator) Close() {}
func (s *showCreateAllTablesGenerator) Close(ctx context.Context) {
s.acc.Close(ctx)
}

// makeShowCreateAllTablesGenerator creates a generator to support the
// crdb_internal.show_create_all_tables(dbName) builtin.
Expand All @@ -1789,5 +1803,10 @@ func makeShowCreateAllTablesGenerator(
return nil, err
}
ts := tsI.String()
return &showCreateAllTablesGenerator{timestamp: ts, dbName: dbName, ie: ctx.InternalExecutor.(sqlutil.InternalExecutor)}, nil
return &showCreateAllTablesGenerator{
timestamp: ts,
dbName: dbName,
ie: ctx.InternalExecutor.(sqlutil.InternalExecutor),
acc: ctx.Mon.MakeBoundAccount(),
}, nil
}
4 changes: 2 additions & 2 deletions pkg/sql/sem/builtins/geo_builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func (m *minimumBoundRadiusGen) Values() (tree.Datums, error) {
tree.NewDFloat(tree.DFloat(m.radius))}, nil
}

func (m *minimumBoundRadiusGen) Close() {}
func (m *minimumBoundRadiusGen) Close(_ context.Context) {}

func makeSubdividedGeometriesGeneratorFactory(expectMaxVerticesArg bool) tree.GeneratorFactory {
return func(
Expand Down Expand Up @@ -441,7 +441,7 @@ type subdividedGeometriesGen struct {

func (s *subdividedGeometriesGen) ResolvedType() *types.T { return types.Geometry }

func (s *subdividedGeometriesGen) Close() {}
func (s *subdividedGeometriesGen) Close(_ context.Context) {}

func (s *subdividedGeometriesGen) Start(_ context.Context, _ *kv.Txn) error {
s.curr = -1
Expand Down
Loading

0 comments on commit e974b23

Please sign in to comment.