Skip to content

Commit 240ad78

Browse files
committed
sql: disallow creating partial stats by default
This commit adds the `enable_create_stats_using_extremes` session setting which allows users to run `CREATE STATISTICS .. USING EXTREMES` when enabled. It is disabled by default. This commit also presents a "feature not supported" error when a user tries to create partial statistics with a `WHERE` clause. This feature has not yet been implemented. Epic: CRDB-19449 Release note: None
1 parent 36d39aa commit 240ad78

File tree

9 files changed

+54
-0
lines changed

9 files changed

+54
-0
lines changed

pkg/ccl/backupccl/testdata/backup-restore/metadata

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ exec-sql
1414
SET CLUSTER SETTING sql.stats.automatic_collection.enabled = false;
1515
----
1616

17+
exec-sql
18+
SET enable_create_stats_using_extremes = true;
19+
----
20+
1721
exec-sql
1822
CREATE DATABASE db1;
1923
USE db1;

pkg/sql/create_stats.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ func (n *createStatsNode) makeJobRecord(ctx context.Context) (*jobs.Record, erro
250250
)
251251
}
252252

253+
if n.Options.UsingExtremes && !n.p.SessionData().EnableCreateStatsUsingExtremes {
254+
return nil, pgerror.New(pgcode.FeatureNotSupported,
255+
"creating partial statistics at extremes is not yet supported",
256+
)
257+
}
258+
259+
if n.Options.Where != nil {
260+
return nil, pgerror.New(pgcode.FeatureNotSupported,
261+
"creating partial statistics with a WHERE clause is not yet supported",
262+
)
263+
}
264+
253265
if err := n.p.CheckPrivilege(ctx, tableDesc, privilege.SELECT); err != nil {
254266
return nil, err
255267
}

pkg/sql/exec_util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3470,6 +3470,10 @@ func (m *sessionDataMutator) SetOptimizerAlwaysUseHistograms(val bool) {
34703470
m.data.OptimizerAlwaysUseHistograms = val
34713471
}
34723472

3473+
func (m *sessionDataMutator) SetEnableCreateStatsUsingExtremes(val bool) {
3474+
m.data.EnableCreateStatsUsingExtremes = val
3475+
}
3476+
34733477
// Utility functions related to scrubbing sensitive information on SQL Stats.
34743478

34753479
// quantizeCounts ensures that the Count field in the

pkg/sql/logictest/testdata/logic_test/distsql_stats

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,6 +1998,15 @@ INSERT INTO abcd VALUES
19981998
statement ok
19991999
INSERT INTO xy VALUES (-1, 9), (-2, 8), (5, 15), (6, 16)
20002000

2001+
statement error pgcode 0A000 creating partial statistics with a WHERE clause is not yet supported
2002+
CREATE STATISTICS abcd_a_partial ON a FROM abcd WHERE a > 1;
2003+
2004+
statement error pgcode 0A000 creating partial statistics at extremes is not yet supported
2005+
CREATE STATISTICS abcd_a_partial ON a FROM abcd USING EXTREMES;
2006+
2007+
statement ok
2008+
SET enable_create_stats_using_extremes = on
2009+
20012010
statement ok
20022011
CREATE STATISTICS abcd_a_partial ON a FROM abcd USING EXTREMES;
20032012

pkg/sql/logictest/testdata/logic_test/information_schema

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4961,6 +4961,7 @@ disable_partially_distributed_plans off
49614961
disable_plan_gists off
49624962
disallow_full_table_scans off
49634963
enable_auto_rehoming off
4964+
enable_create_stats_using_extremes off
49644965
enable_drop_enum_value on
49654966
enable_experimental_alter_column_type_general off
49664967
enable_implicit_select_for_update on

pkg/sql/logictest/testdata/logic_test/pg_catalog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,7 @@ disable_plan_gists off NULL
25952595
disallow_full_table_scans off NULL NULL NULL string
25962596
distsql off NULL NULL NULL string
25972597
enable_auto_rehoming off NULL NULL NULL string
2598+
enable_create_stats_using_extremes off NULL NULL NULL string
25982599
enable_experimental_alter_column_type_general off NULL NULL NULL string
25992600
enable_implicit_select_for_update on NULL NULL NULL string
26002601
enable_implicit_transaction_for_batch_statements on NULL NULL NULL string
@@ -2743,6 +2744,7 @@ disable_plan_gists off NULL
27432744
disallow_full_table_scans off NULL user NULL off off
27442745
distsql off NULL user NULL off off
27452746
enable_auto_rehoming off NULL user NULL off off
2747+
enable_create_stats_using_extremes off NULL user NULL off off
27462748
enable_experimental_alter_column_type_general off NULL user NULL off off
27472749
enable_implicit_select_for_update on NULL user NULL on on
27482750
enable_implicit_transaction_for_batch_statements on NULL user NULL on on
@@ -2889,6 +2891,7 @@ disallow_full_table_scans NULL NULL NULL
28892891
distsql NULL NULL NULL NULL NULL
28902892
distsql_workmem NULL NULL NULL NULL NULL
28912893
enable_auto_rehoming NULL NULL NULL NULL NULL
2894+
enable_create_stats_using_extremes NULL NULL NULL NULL NULL
28922895
enable_experimental_alter_column_type_general NULL NULL NULL NULL NULL
28932896
enable_implicit_select_for_update NULL NULL NULL NULL NULL
28942897
enable_implicit_transaction_for_batch_statements NULL NULL NULL NULL NULL

pkg/sql/logictest/testdata/logic_test/show_source

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ disable_plan_gists off
5757
disallow_full_table_scans off
5858
distsql off
5959
enable_auto_rehoming off
60+
enable_create_stats_using_extremes off
6061
enable_experimental_alter_column_type_general off
6162
enable_implicit_select_for_update on
6263
enable_implicit_transaction_for_batch_statements on

pkg/sql/sessiondatapb/local_only_session_data.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,9 @@ message LocalOnlySessionData {
346346
// OptimizerAlwaysUseHistograms, when true, ensures that the optimizer
347347
// always uses histograms to calculate statistics if available.
348348
bool optimizer_always_use_histograms = 94;
349+
// EnableCreateStatsUsingExtremes, when true, allows the use of CREATE
350+
// STATISTICS .. USING EXTREMES.
351+
bool enable_create_stats_using_extremes = 95;
349352

350353
///////////////////////////////////////////////////////////////////////////
351354
// WARNING: consider whether a session parameter you're adding needs to //

pkg/sql/vars.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2532,6 +2532,23 @@ var varGen = map[string]sessionVar{
25322532
},
25332533
GlobalDefault: globalTrue,
25342534
},
2535+
2536+
// CockroachDB extension.
2537+
`enable_create_stats_using_extremes`: {
2538+
GetStringVal: makePostgresBoolGetStringValFn(`enable_create_stats_using_extremes`),
2539+
Set: func(_ context.Context, m sessionDataMutator, s string) error {
2540+
b, err := paramparse.ParseBoolVar("enable_create_stats_using_extremes", s)
2541+
if err != nil {
2542+
return err
2543+
}
2544+
m.SetEnableCreateStatsUsingExtremes(b)
2545+
return nil
2546+
},
2547+
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
2548+
return formatBoolAsPostgresSetting(evalCtx.SessionData().EnableCreateStatsUsingExtremes), nil
2549+
},
2550+
GlobalDefault: globalFalse,
2551+
},
25352552
}
25362553

25372554
// We want test coverage for this on and off so make it metamorphic.

0 commit comments

Comments
 (0)