Skip to content

Commit

Permalink
sql: introduce index_recommendations_enabled session variable
Browse files Browse the repository at this point in the history
This commit introduces a session variable to control whether or not index
recommendations are shown at the bottom of the EXPLAIN plan. If
`index_recommendations_enabled` is set to true, index recommendations
are shown. Otherwise, they are not shown. This variable defaults to true.

Release note (sql change): Index recommendations can be omitted from
the EXPLAIN plan if the `index_recommendations_enabled` session variable
is set to false.
  • Loading branch information
Neha George committed Dec 14, 2021
1 parent d411bd0 commit 94f69be
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 124 deletions.
4 changes: 4 additions & 0 deletions pkg/sql/exec_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2710,6 +2710,10 @@ func (m *sessionDataMutator) SetZigzagJoinEnabled(val bool) {
m.data.ZigzagJoinEnabled = val
}

func (m *sessionDataMutator) SetIndexRecommendationsEnabled(val bool) {
m.data.IndexRecommendationsEnabled = val
}

func (m *sessionDataMutator) SetExperimentalDistSQLPlanning(
val sessiondatapb.ExperimentalDistSQLPlanningMode,
) {
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 @@ -4659,6 +4659,7 @@ force_savepoint_restart off
foreign_key_cascades_limit 10000
idle_in_session_timeout 0
idle_in_transaction_session_timeout 0
index_recommendations_enabled on
inject_retry_errors_enabled off
integer_datetimes on
intervalstyle postgres
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 @@ -4062,6 +4062,7 @@ force_savepoint_restart off NULL
foreign_key_cascades_limit 10000 NULL NULL NULL string
idle_in_session_timeout 0 NULL NULL NULL string
idle_in_transaction_session_timeout 0 NULL NULL NULL string
index_recommendations_enabled on NULL NULL NULL string
inject_retry_errors_enabled off NULL NULL NULL string
integer_datetimes on NULL NULL NULL string
intervalstyle postgres NULL NULL NULL string
Expand Down Expand Up @@ -4168,6 +4169,7 @@ force_savepoint_restart off NULL
foreign_key_cascades_limit 10000 NULL user NULL 10000 10000
idle_in_session_timeout 0 NULL user NULL 0s 0s
idle_in_transaction_session_timeout 0 NULL user NULL 0s 0s
index_recommendations_enabled on NULL user NULL on on
inject_retry_errors_enabled off NULL user NULL off off
integer_datetimes on NULL user NULL on on
intervalstyle postgres NULL user NULL postgres postgres
Expand Down Expand Up @@ -4269,6 +4271,7 @@ force_savepoint_restart NULL NULL NULL
foreign_key_cascades_limit NULL NULL NULL NULL NULL
idle_in_session_timeout NULL NULL NULL NULL NULL
idle_in_transaction_session_timeout NULL NULL NULL NULL NULL
index_recommendations_enabled NULL NULL NULL NULL NULL
inject_retry_errors_enabled NULL NULL NULL NULL NULL
integer_datetimes NULL NULL NULL NULL NULL
intervalstyle 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 @@ -558,6 +558,12 @@ SET disable_plan_gists = 'true'
statement ok
SET disable_plan_gists = 'false'

statement ok
SET index_recommendations_enabled = 'true'

statement ok
SET index_recommendations_enabled = 'false'

query T
SHOW LC_COLLATE
----
Expand Down
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 @@ -65,6 +65,7 @@ force_savepoint_restart off
foreign_key_cascades_limit 10000
idle_in_session_timeout 0
idle_in_transaction_session_timeout 0
index_recommendations_enabled on
inject_retry_errors_enabled off
integer_datetimes on
intervalstyle postgres
Expand Down
33 changes: 33 additions & 0 deletions pkg/sql/opt/exec/execbuilder/testdata/explain
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,39 @@ index recommendations: 1
1. type: index replacement
SQL commands: DROP INDEX tc@c; CREATE INDEX ON tc (a) STORING (b);

# Ensure that the query above no longer has index recommendations if they are
# disabled.
statement ok
SET index_recommendations_enabled = 'false'

query T
EXPLAIN (VERBOSE) SELECT * FROM tc WHERE a = 10 ORDER BY b
----
distribution: local
vectorized: true
·
• sort
│ columns: (a, b)
│ ordering: +b
│ estimated row count: 10 (missing stats)
│ order: +b
└── • index join
│ columns: (a, b)
│ estimated row count: 10 (missing stats)
│ table: tc@tc_pkey
│ key columns: rowid
└── • scan
columns: (a, rowid)
estimated row count: 10 (missing stats)
table: tc@c
spans: /10-/11

# Reset index_recommendations_enabled to its default setting.
statement ok
SET index_recommendations_enabled = 'true'

query T
EXPLAIN (TYPES) INSERT INTO t VALUES (1, 2)
----
Expand Down
3 changes: 2 additions & 1 deletion pkg/sql/plan_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ func (opc *optPlanningCtx) buildExecMemo(ctx context.Context) (_ *memo.Memo, _ e

// For index recommendations, after building we must interrupt the flow to
// find potential index candidates in the memo.
if _, isExplain := opc.p.stmt.AST.(*tree.Explain); isExplain {
_, isExplain := opc.p.stmt.AST.(*tree.Explain)
if isExplain && p.SessionData().IndexRecommendationsEnabled {
if err := opc.makeQueryIndexRecommendation(); err != nil {
return nil, err
}
Expand Down

0 comments on commit 94f69be

Please sign in to comment.