Skip to content

Commit

Permalink
sql: add a session setting to disable gists
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
cucaroach committed Oct 26, 2021
1 parent ab839c6 commit 3c7855b
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 125 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2720,6 +2720,10 @@ func (m *sessionDataMutator) SetSynchronousCommit(val bool) {
m.data.SynchronousCommit = val
}

func (m *sessionDataMutator) SetDisablePlanGists(val bool) {
m.data.DisablePlanGists = val
}

func (m *sessionDataMutator) SetDistSQLMode(val sessiondatapb.DistSQLExecMode) {
m.data.DistSQLMode = val
}
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -4625,6 +4625,7 @@ default_transaction_priority normal
default_transaction_read_only off
default_transaction_use_follower_reads off
disable_partially_distributed_plans off
disable_plan_gists off
disallow_full_table_scans off
distsql_workmem 64 MiB
enable_copying_partitioning_when_deinterleaving_table off
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -3994,6 +3994,7 @@ default_transaction_priority normal NULL
default_transaction_read_only off NULL NULL NULL string
default_transaction_use_follower_reads off NULL NULL NULL string
disable_partially_distributed_plans off NULL NULL NULL string
disable_plan_gists off NULL NULL NULL string
disallow_full_table_scans off NULL NULL NULL string
distsql off NULL NULL NULL string
distsql_workmem 64 MiB NULL NULL NULL string
Expand Down Expand Up @@ -4091,6 +4092,7 @@ default_transaction_priority normal NULL
default_transaction_read_only off NULL user NULL off off
default_transaction_use_follower_reads off NULL user NULL off off
disable_partially_distributed_plans off NULL user NULL off off
disable_plan_gists off NULL user NULL off off
disallow_full_table_scans off NULL user NULL off off
distsql off NULL user NULL off off
distsql_workmem 64 MiB NULL user NULL 64 MiB 64 MiB
Expand Down Expand Up @@ -4184,6 +4186,7 @@ default_transaction_priority NULL NULL NULL
default_transaction_read_only NULL NULL NULL NULL NULL
default_transaction_use_follower_reads NULL NULL NULL NULL NULL
disable_partially_distributed_plans NULL NULL NULL NULL NULL
disable_plan_gists NULL NULL NULL NULL NULL
disallow_full_table_scans NULL NULL NULL NULL NULL
distsql NULL NULL NULL NULL NULL
distsql_workmem NULL NULL NULL NULL NULL
Expand Down
6 changes: 6 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/set
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,9 @@ SET cluster setting sql.trace.txn.enable_threshold='1s'

statement ok
SET cluster setting sql.trace.txn.enable_threshold='0s'

statement ok
SET disable_plan_gists = 'true'

statement ok
SET disable_plan_gists = 'false'
1 change: 1 addition & 0 deletions pkg/sql/logictest/testdata/logic_test/show_source
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ default_transaction_priority normal
default_transaction_read_only off
default_transaction_use_follower_reads off
disable_partially_distributed_plans off
disable_plan_gists off
disallow_full_table_scans off
distsql off
distsql_workmem 64 MiB
Expand Down
25 changes: 25 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/explain_gist
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,31 @@ SELECT crdb_internal.decode_plan_gist('$gist')
table: t@t_pkey
spans: FULL SCAN

# Test that EXPLAIN (GIST) still works if automatic gists are disabled.
statement ok
SET disable_plan_gists = 'true'

let $gist
EXPLAIN (GIST) SELECT count(*) FROM t

query T
SELECT * FROM crdb_internal.decode_plan_gist('$gist')
----
• group (scalar)
└── • scan
table: t@t_pkey
spans: FULL SCAN

query T
SELECT crdb_internal.decode_plan_gist('$gist')
----
• group (scalar)
└── • scan
table: t@t_pkey
spans: FULL SCAN

statement error pq: unknown signature: crdb_internal\.decode_plan_gist\(int\)
SELECT * FROM crdb_internal.decode_plan_gist(10)

Expand Down
14 changes: 10 additions & 4 deletions pkg/sql/plan_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,13 @@ func (opc *optPlanningCtx) runExecBuilder(
var containsLargeFullTableScan bool
var containsLargeFullIndexScan bool
var containsMutation bool
gf := explain.NewPlanGistFactory(f)
var gf *explain.PlanGistFactory
if !opc.p.SessionData().DisablePlanGists {
gf = explain.NewPlanGistFactory(f)
f = gf
}
if !planTop.instrumentation.ShouldBuildExplainPlan() {
bld := execbuilder.New(gf, &opc.optimizer, mem, &opc.catalog, mem.RootExpr(), evalCtx, allowAutoCommit)
bld := execbuilder.New(f, &opc.optimizer, mem, &opc.catalog, mem.RootExpr(), evalCtx, allowAutoCommit)
plan, err := bld.Build()
if err != nil {
return err
Expand All @@ -580,7 +584,7 @@ func (opc *optPlanningCtx) runExecBuilder(
containsMutation = bld.ContainsMutation
} else {
// Create an explain factory and record the explain.Plan.
explainFactory := explain.NewFactory(gf)
explainFactory := explain.NewFactory(f)
bld := execbuilder.New(
explainFactory, &opc.optimizer, mem, &opc.catalog, mem.RootExpr(), evalCtx, allowAutoCommit,
)
Expand All @@ -599,7 +603,9 @@ func (opc *optPlanningCtx) runExecBuilder(

planTop.instrumentation.RecordExplainPlan(explainPlan)
}
planTop.instrumentation.planGist = gf.PlanGist()
if gf != nil {
planTop.instrumentation.planGist = gf.PlanGist()
}

if stmt.ExpectedTypes != nil {
cols := result.main.planColumns()
Expand Down
64 changes: 64 additions & 0 deletions pkg/sql/plan_opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"time"

"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
Expand Down Expand Up @@ -642,3 +646,63 @@ func BenchmarkQueryCache(b *testing.B) {
})
}
}

func TestPlanGistControl(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()

s, _, db := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
execCfg := s.ExecutorConfig().(ExecutorConfig)
internalPlanner, cleanup := NewInternalPlanner(
"test",
kv.NewTxn(ctx, db, s.NodeID()),
security.RootUserName(),
&MemoryMetrics{},
&execCfg,
sessiondatapb.SessionData{},
)
defer cleanup()

p := internalPlanner.(*planner)
stmt, err := parser.ParseOne("SELECT 1")
if err != nil {
t.Fatal(err)
}
p.stmt = makeStatement(stmt, ClusterWideID{})
if err := p.makeOptimizerPlan(ctx); err != nil {
t.Fatal(err)
}

if p.SessionData().DisablePlanGists {
t.Error("expected gists to be enabled by default")
}

if p.instrumentation.planGist.String() == "" {
t.Error("expected gist by default")
}

internalPlanner, cleanup = NewInternalPlanner(
"test",
kv.NewTxn(ctx, db, s.NodeID()),
security.RootUserName(),
&MemoryMetrics{},
&execCfg,
sessiondatapb.SessionData{},
)
defer cleanup()

p = internalPlanner.(*planner)
p.SessionData().DisablePlanGists = true

p.stmt = makeStatement(stmt, ClusterWideID{})
if err := p.makeOptimizerPlan(ctx); err != nil {
t.Fatal(err)
}

if p.instrumentation.planGist.String() != "" {
t.Error("expected no gist")
}
}
Loading

0 comments on commit 3c7855b

Please sign in to comment.