-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
vars.go
2483 lines (2313 loc) · 86.7 KB
/
vars.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package sql
import (
"bytes"
"context"
"fmt"
"math"
"sort"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/delegate"
"github.com/cockroachdb/cockroach/pkg/sql/lex"
"github.com/cockroachdb/cockroach/pkg/sql/paramparse"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgnotice"
"github.com/cockroachdb/cockroach/pkg/sql/rowexec"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondatapb"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil/pgdate"
"github.com/cockroachdb/errors"
)
const (
// PgServerVersion is the latest version of postgres that we claim to support.
PgServerVersion = "13.0.0"
// PgServerVersionNum is the latest version of postgres that we claim to support in the numeric format of "server_version_num".
PgServerVersionNum = "130000"
// PgCompatLocale is the locale string we advertise in `LC_*` session
// variables. C.UTF-8 is the only locale that is allowed in CREATE DATABASE
// at the time of writing.
// See https://www.postgresql.org/docs/14/locale.html
PgCompatLocale = "C.UTF-8"
)
type getStringValFn = func(
ctx context.Context, evalCtx *extendedEvalContext, values []tree.TypedExpr, kv *kv.Txn,
) (string, error)
// sessionVar provides a unified interface for performing operations on
// variables such as the selected database, or desired syntax.
type sessionVar struct {
// Hidden indicates that the variable should not show up in the output of SHOW ALL.
Hidden bool
// Get returns a string representation of a given variable to be used
// either by SHOW or in the pg_catalog table.
Get func(evalCtx *extendedEvalContext, kv *kv.Txn) (string, error)
// GetFromSessionData returns a string representation of a given variable to
// be used by BufferParamStatus. This is only required if the variable
// is expected to send updates through ParamStatusUpdate in pgwire.
GetFromSessionData func(sd *sessiondata.SessionData) string
// GetStringVal converts the provided Expr to a string suitable
// for Set() or RuntimeSet().
// If this method is not provided,
// `getStringVal(evalCtx, varName, values)`
// will be used instead.
//
// The reason why variable sets work in two phases like this is that
// the Set() method has to operate on strings, because it can be
// invoked at a point where there is no evalContext yet (e.g.
// upon session initialization in pgwire).
GetStringVal getStringValFn
// Set performs mutations to effect the change desired by SET commands.
// This method should be provided for variables that can be overridden
// in pgwire.
Set func(ctx context.Context, m sessionDataMutator, val string) error
// RuntimeSet is like Set except it can only be used in sessions
// that are already running (i.e. not during session
// initialization). Currently only used for transaction_isolation.
RuntimeSet func(_ context.Context, evalCtx *extendedEvalContext, local bool, s string) error
// SetWithPlanner is like Set except it can only be used in sessions
// that are already running and the planner is passed in.
// The planner can be used to check privileges before setting.
SetWithPlanner func(_ context.Context, p *planner, local bool, s string) error
// GlobalDefault is the string value to use as default for RESET or
// during session initialization when no default value was provided
// by the client.
GlobalDefault func(sv *settings.Values) string
// Equal returns whether the value of the given variable is equal between the
// two SessionData references. This is only required if the variable is
// expected to send updates through ParamStatusUpdate in pgwire.
Equal func(a, b *sessiondata.SessionData) bool
}
func formatBoolAsPostgresSetting(b bool) string {
if b {
return "on"
}
return "off"
}
func formatFloatAsPostgresSetting(f float64) string {
return strconv.FormatFloat(f, 'G', -1, 64)
}
// makeDummyBooleanSessionVar generates a sessionVar for a bool session setting.
// These functions allow the setting to be changed, but whose values are not used.
// They are logged to telemetry and output a notice that these are unused.
func makeDummyBooleanSessionVar(
name string,
getFunc func(*extendedEvalContext, *kv.Txn) (string, error),
setFunc func(sessionDataMutator, bool),
sv func(_ *settings.Values) string,
) sessionVar {
return sessionVar{
GetStringVal: makePostgresBoolGetStringValFn(name),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar(name, s)
if err != nil {
return err
}
setFunc(m, b)
return nil
},
Get: getFunc,
GlobalDefault: sv,
}
}
// varGen is the main definition array for all session variables.
// Note to maintainers: try to keep this sorted in the source code.
var varGen = map[string]sessionVar{
// Set by clients to improve query logging.
// See https://www.postgresql.org/docs/10/static/runtime-config-logging.html#GUC-APPLICATION-NAME
`application_name`: {
Set: func(
_ context.Context, m sessionDataMutator, s string,
) error {
m.SetApplicationName(s)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.SessionData().ApplicationName, nil
},
GetFromSessionData: func(sd *sessiondata.SessionData) string {
return sd.ApplicationName
},
GlobalDefault: func(_ *settings.Values) string {
return ""
},
Equal: func(a, b *sessiondata.SessionData) bool {
return a.ApplicationName == b.ApplicationName
},
},
// CockroachDB extension.
`avoid_buffering`: {
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().AvoidBuffering), nil
},
GetStringVal: makePostgresBoolGetStringValFn("avoid_buffering"),
Set: func(ctx context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar(`avoid_buffering`, s)
if err != nil {
return err
}
m.SetAvoidBuffering(b)
return nil
},
GlobalDefault: func(sv *settings.Values) string {
return "false"
},
},
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html
// and https://www.postgresql.org/docs/10/static/datatype-binary.html
`bytea_output`: {
Set: func(
_ context.Context, m sessionDataMutator, s string,
) error {
mode, ok := lex.BytesEncodeFormatFromString(s)
if !ok {
return newVarValueError(`bytea_output`, s, "hex", "escape", "base64")
}
m.SetBytesEncodeFormat(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.SessionData().DataConversionConfig.BytesEncodeFormat.String(), nil
},
GlobalDefault: func(sv *settings.Values) string { return lex.BytesEncodeHex.String() },
},
`client_min_messages`: {
Set: func(
_ context.Context, m sessionDataMutator, s string,
) error {
severity, ok := pgnotice.ParseDisplaySeverity(s)
if !ok {
return errors.WithHintf(
pgerror.Newf(
pgcode.InvalidParameterValue,
"%s is not supported",
severity,
),
"Valid severities are: %s.",
strings.Join(pgnotice.ValidDisplaySeverities(), ", "),
)
}
m.SetNoticeDisplaySeverity(severity)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return pgnotice.DisplaySeverity(evalCtx.SessionData().NoticeDisplaySeverity).String(), nil
},
GlobalDefault: func(_ *settings.Values) string { return "notice" },
},
// See https://www.postgresql.org/docs/9.6/static/multibyte.html
// Also aliased to SET NAMES.
`client_encoding`: {
Set: func(
_ context.Context, m sessionDataMutator, s string,
) error {
encoding := builtins.CleanEncodingName(s)
switch encoding {
case "utf8", "unicode", "cp65001":
return nil
default:
return unimplemented.NewWithIssueDetailf(35882,
"client_encoding "+encoding,
"unimplemented client encoding: %q", encoding)
}
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) { return "UTF8", nil },
GlobalDefault: func(_ *settings.Values) string { return "UTF8" },
},
// Supported for PG compatibility only.
// See https://www.postgresql.org/docs/9.6/static/multibyte.html
`server_encoding`: makeReadOnlyVar("UTF8"),
// CockroachDB extension.
`database`: {
GetStringVal: func(
ctx context.Context, evalCtx *extendedEvalContext, values []tree.TypedExpr, txn *kv.Txn,
) (string, error) {
dbName, err := getStringVal(&evalCtx.Context, `database`, values)
if err != nil {
return "", err
}
if len(dbName) == 0 && evalCtx.SessionData().SafeUpdates {
return "", pgerror.DangerousStatementf("SET database to empty string")
}
if len(dbName) != 0 {
// Verify database descriptor exists.
if _, err := evalCtx.Descs.GetImmutableDatabaseByName(
ctx, txn, dbName, tree.DatabaseLookupFlags{Required: true},
); err != nil {
return "", err
}
}
return dbName, nil
},
Set: func(_ context.Context, m sessionDataMutator, dbName string) error {
m.SetDatabase(dbName)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.SessionData().Database, nil
},
GlobalDefault: func(_ *settings.Values) string {
// The "defaultdb" value is set as session default in the pgwire
// connection code. The global default is the empty string,
// which is what internal connections should pick up.
return ""
},
},
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-DATESTYLE
`datestyle`: {
Set: func(ctx context.Context, m sessionDataMutator, s string) error {
ds, err := pgdate.ParseDateStyle(s, m.data.GetDateStyle())
if err != nil {
return newVarValueError("DateStyle", s, pgdate.AllowedDateStyles()...)
}
if ds.Style != pgdate.Style_ISO {
return unimplemented.NewWithIssue(41773, "only ISO style is supported")
}
m.SetDateStyle(ds)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.GetDateStyle().SQLString(), nil
},
GetFromSessionData: func(sd *sessiondata.SessionData) string {
return sd.GetDateStyle().SQLString()
},
GlobalDefault: func(sv *settings.Values) string {
return dateStyleEnumMap[dateStyle.Get(sv)]
},
Equal: func(a, b *sessiondata.SessionData) bool {
return a.GetDateStyle() == b.GetDateStyle()
},
},
// This is only kept for backwards compatibility and no longer has any effect.
`datestyle_enabled`: makeBackwardsCompatBoolVar(
"datestyle_enabled", true,
),
// Controls the subsequent parsing of a "naked" INT type.
// TODO(bob): Remove or no-op this in v2.4: https://github.com/cockroachdb/cockroach/issues/32844
`default_int_size`: {
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return strconv.FormatInt(int64(evalCtx.SessionData().DefaultIntSize), 10), nil
},
GetStringVal: makeIntGetStringValFn("default_int_size"),
Set: func(ctx context.Context, m sessionDataMutator, val string) error {
i, err := strconv.ParseInt(val, 10, 64)
if err != nil {
return wrapSetVarError(err, "default_int_size", val)
}
if i != 4 && i != 8 {
return pgerror.New(pgcode.InvalidParameterValue,
`only 4 or 8 are supported by default_int_size`)
}
// Only record when the value has been changed to a non-default
// value, since we really just want to know how useful int4-mode
// is. If we were to record counts for size.4 and size.8
// variables, we'd have to distinguish cases in which a session
// was opened in int8 mode and switched to int4 mode, versus ones
// set to int4 by a connection string.
// TODO(bob): Change to 8 in v2.3: https://github.com/cockroachdb/cockroach/issues/32534
if i == 4 {
telemetry.Inc(sqltelemetry.DefaultIntSize4Counter)
}
m.SetDefaultIntSize(int32(i))
return nil
},
GlobalDefault: func(sv *settings.Values) string {
return strconv.FormatInt(defaultIntSize.Get(sv), 10)
},
},
// See https://www.postgresql.org/docs/10/runtime-config-client.html.
// Supported only for pg compatibility - CockroachDB has no notion of
// tablespaces.
`default_tablespace`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
if s != "" {
return newVarValueError(`default_tablespace`, s, "")
}
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return "", nil
},
GlobalDefault: func(sv *settings.Values) string { return "" },
},
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-DEFAULT-TRANSACTION-ISOLATION
`default_transaction_isolation`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
switch strings.ToUpper(s) {
case `READ UNCOMMITTED`, `READ COMMITTED`, `SNAPSHOT`, `REPEATABLE READ`, `SERIALIZABLE`, `DEFAULT`:
// Do nothing. All transactions execute with serializable isolation.
default:
return newVarValueError(`default_transaction_isolation`, s, "serializable")
}
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return "serializable", nil
},
GlobalDefault: func(sv *settings.Values) string { return "default" },
},
// CockroachDB extension.
`default_transaction_priority`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
pri, ok := tree.UserPriorityFromString(s)
if !ok {
return newVarValueError(`default_transaction_isolation`, s, "low", "normal", "high")
}
m.SetDefaultTransactionPriority(pri)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
pri := tree.UserPriority(evalCtx.SessionData().DefaultTxnPriority)
if pri == tree.UnspecifiedUserPriority {
pri = tree.Normal
}
return strings.ToLower(pri.String()), nil
},
GlobalDefault: func(sv *settings.Values) string {
return strings.ToLower(tree.Normal.String())
},
},
// See https://www.postgresql.org/docs/9.3/static/runtime-config-client.html#GUC-DEFAULT-TRANSACTION-READ-ONLY
`default_transaction_read_only`: {
GetStringVal: makePostgresBoolGetStringValFn("default_transaction_read_only"),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("default_transaction_read_only", s)
if err != nil {
return err
}
m.SetDefaultTransactionReadOnly(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().DefaultTxnReadOnly), nil
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`default_transaction_use_follower_reads`: {
GetStringVal: makePostgresBoolGetStringValFn("default_transaction_use_follower_reads"),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("default_transaction_use_follower_reads", s)
if err != nil {
return err
}
m.SetDefaultTransactionUseFollowerReads(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().DefaultTxnUseFollowerReads), nil
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`disable_plan_gists`: {
GetStringVal: makePostgresBoolGetStringValFn(`disable_plan_gists`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("disable_plan_gists", s)
if err != nil {
return err
}
m.SetDisablePlanGists(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().DisablePlanGists), nil
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`index_recommendations_enabled`: {
GetStringVal: makePostgresBoolGetStringValFn(`index_recommendations_enabled`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("index_recommendations_enabled", s)
if err != nil {
return err
}
m.SetIndexRecommendationsEnabled(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().IndexRecommendationsEnabled), nil
},
GlobalDefault: globalTrue,
},
// CockroachDB extension.
`distsql`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
mode, ok := sessiondatapb.DistSQLExecModeFromString(s)
if !ok {
return newVarValueError(`distsql`, s, "on", "off", "auto", "always", "2.0-auto", "2.0-off")
}
m.SetDistSQLMode(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.SessionData().DistSQLMode.String(), nil
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondatapb.DistSQLExecMode(DistSQLClusterExecMode.Get(sv)).String()
},
},
// CockroachDB extension.
`distsql_workmem`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
limit, err := humanizeutil.ParseBytes(s)
if err != nil {
return err
}
if limit <= 1 {
return errors.New("distsql_workmem can only be set to a value greater than 1")
}
m.SetDistSQLWorkMem(limit)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return string(humanizeutil.IBytes(evalCtx.SessionData().WorkMemLimit)), nil
},
GlobalDefault: func(sv *settings.Values) string {
return string(humanizeutil.IBytes(settingWorkMemBytes.Get(sv)))
},
},
// CockroachDB extension.
`experimental_distsql_planning`: {
GetStringVal: makePostgresBoolGetStringValFn(`experimental_distsql_planning`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
mode, ok := sessiondatapb.ExperimentalDistSQLPlanningModeFromString(s)
if !ok {
return newVarValueError(`experimental_distsql_planning`, s,
"off", "on", "always")
}
m.SetExperimentalDistSQLPlanning(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.SessionData().ExperimentalDistSQLPlanningMode.String(), nil
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondatapb.ExperimentalDistSQLPlanningMode(experimentalDistSQLPlanningClusterMode.Get(sv)).String()
},
},
// CockroachDB extension.
`disable_partially_distributed_plans`: {
GetStringVal: makePostgresBoolGetStringValFn(`disable_partially_distributed_plans`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("disable_partially_distributed_plans", s)
if err != nil {
return err
}
m.SetPartiallyDistributedPlansDisabled(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().PartiallyDistributedPlansDisabled), nil
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`enable_zigzag_join`: {
GetStringVal: makePostgresBoolGetStringValFn(`enable_zigzag_join`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("enable_zigzag_join", s)
if err != nil {
return err
}
m.SetZigzagJoinEnabled(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().ZigzagJoinEnabled), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(zigzagJoinClusterMode.Get(sv))
},
},
// CockroachDB extension.
`reorder_joins_limit`: {
GetStringVal: makeIntGetStringValFn(`reorder_joins_limit`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
if b < 0 {
return pgerror.Newf(pgcode.InvalidParameterValue,
"cannot set reorder_joins_limit to a negative value: %d", b)
}
m.SetReorderJoinsLimit(int(b))
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return strconv.FormatInt(evalCtx.SessionData().ReorderJoinsLimit, 10), nil
},
GlobalDefault: func(sv *settings.Values) string {
return strconv.FormatInt(ReorderJoinsLimitClusterValue.Get(sv), 10)
},
},
// CockroachDB extension.
`require_explicit_primary_keys`: {
GetStringVal: makePostgresBoolGetStringValFn(`require_explicit_primary_keys`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("require_explicit_primary_key", s)
if err != nil {
return err
}
m.SetRequireExplicitPrimaryKeys(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().RequireExplicitPrimaryKeys), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(requireExplicitPrimaryKeysClusterMode.Get(sv))
},
},
// CockroachDB extension.
`vectorize`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
mode, ok := sessiondatapb.VectorizeExecModeFromString(s)
if !ok {
return newVarValueError(`vectorize`, s,
"off", "on", "experimental_always")
}
m.SetVectorize(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.SessionData().VectorizeMode.String(), nil
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondatapb.VectorizeExecMode(
VectorizeClusterMode.Get(sv)).String()
},
},
// CockroachDB extension.
`testing_vectorize_inject_panics`: {
GetStringVal: makePostgresBoolGetStringValFn(`testing_vectorize_inject_panics`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("testing_vectorize_inject_panics", s)
if err != nil {
return err
}
m.SetTestingVectorizeInjectPanics(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().TestingVectorizeInjectPanics), nil
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
// This is deprecated; the only allowable setting is "on".
`optimizer`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
if strings.ToUpper(s) != "ON" {
return newVarValueError(`optimizer`, s, "on")
}
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return "on", nil
},
GlobalDefault: func(sv *settings.Values) string {
return "on"
},
},
// CockroachDB extension.
`foreign_key_cascades_limit`: {
GetStringVal: makeIntGetStringValFn(`foreign_key_cascades_limit`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
if b < 0 {
return pgerror.Newf(pgcode.InvalidParameterValue,
"cannot set foreign_key_cascades_limit to a negative value: %d", b)
}
m.SetOptimizerFKCascadesLimit(int(b))
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return strconv.FormatInt(evalCtx.SessionData().OptimizerFKCascadesLimit, 10), nil
},
GlobalDefault: func(sv *settings.Values) string {
return strconv.FormatInt(optDrivenFKCascadesClusterLimit.Get(sv), 10)
},
},
// CockroachDB extension.
`optimizer_use_histograms`: {
GetStringVal: makePostgresBoolGetStringValFn(`optimizer_use_histograms`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("optimizer_use_histograms", s)
if err != nil {
return err
}
m.SetOptimizerUseHistograms(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().OptimizerUseHistograms), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(optUseHistogramsClusterMode.Get(sv))
},
},
// CockroachDB extension.
`optimizer_use_multicol_stats`: {
GetStringVal: makePostgresBoolGetStringValFn(`optimizer_use_multicol_stats`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("optimizer_use_multicol_stats", s)
if err != nil {
return err
}
m.SetOptimizerUseMultiColStats(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().OptimizerUseMultiColStats), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(optUseMultiColStatsClusterMode.Get(sv))
},
},
// CockroachDB extension.
`locality_optimized_partitioned_index_scan`: {
GetStringVal: makePostgresBoolGetStringValFn(`locality_optimized_partitioned_index_scan`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar(`locality_optimized_partitioned_index_scan`, s)
if err != nil {
return err
}
m.SetLocalityOptimizedSearch(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().LocalityOptimizedSearch), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(localityOptimizedSearchMode.Get(sv))
},
},
// CockroachDB extension.
`enable_implicit_select_for_update`: {
GetStringVal: makePostgresBoolGetStringValFn(`enable_implicit_select_for_update`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("enabled_implicit_select_for_update", s)
if err != nil {
return err
}
m.SetImplicitSelectForUpdate(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().ImplicitSelectForUpdate), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(implicitSelectForUpdateClusterMode.Get(sv))
},
},
// CockroachDB extension.
`enable_insert_fast_path`: {
GetStringVal: makePostgresBoolGetStringValFn(`enable_insert_fast_path`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("enable_insert_fast_path", s)
if err != nil {
return err
}
m.SetInsertFastPath(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().InsertFastPath), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(insertFastPathClusterMode.Get(sv))
},
},
// CockroachDB extension.
`serial_normalization`: {
Set: func(_ context.Context, m sessionDataMutator, s string) error {
mode, ok := sessiondatapb.SerialNormalizationModeFromString(s)
if !ok {
return newVarValueError(`serial_normalization`, s,
"rowid", "virtual_sequence", "sql_sequence", "sql_sequence_cached")
}
m.SetSerialNormalizationMode(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.SessionData().SerialNormalizationMode.String(), nil
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondatapb.SerialNormalizationMode(
SerialNormalizationMode.Get(sv)).String()
},
},
// CockroachDB extension.
`stub_catalog_tables`: {
GetStringVal: makePostgresBoolGetStringValFn(`stub_catalog_tables`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
b, err := paramparse.ParseBoolVar("stub_catalog_tables", s)
if err != nil {
return err
}
m.SetStubCatalogTablesEnabled(b)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().StubCatalogTablesEnabled), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatBoolAsPostgresSetting(stubCatalogTablesEnabledClusterValue.Get(sv))
},
},
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html
`extra_float_digits`: {
GetStringVal: makeIntGetStringValFn(`extra_float_digits`),
Set: func(
_ context.Context, m sessionDataMutator, s string,
) error {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return wrapSetVarError(err, "extra_float_digits", s)
}
// Note: this is the range allowed by PostgreSQL.
// See also the documentation around (DataConversionConfig).GetFloatPrec()
// in session_data.go.
if i < -15 || i > 3 {
return pgerror.Newf(pgcode.InvalidParameterValue,
`%d is outside the valid range for parameter "extra_float_digits" (-15 .. 3)`, i)
}
m.SetExtraFloatDigits(int32(i))
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return fmt.Sprintf("%d", evalCtx.SessionData().DataConversionConfig.ExtraFloatDigits), nil
},
GlobalDefault: func(sv *settings.Values) string { return "1" },
},
// CockroachDB extension. See docs on SessionData.ForceSavepointRestart.
// https://github.com/cockroachdb/cockroach/issues/30588
`force_savepoint_restart`: {
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().ForceSavepointRestart), nil
},
GetStringVal: makePostgresBoolGetStringValFn("force_savepoint_restart"),
Set: func(_ context.Context, m sessionDataMutator, val string) error {
b, err := paramparse.ParseBoolVar("force_savepoint_restart", val)
if err != nil {
return err
}
if b {
telemetry.Inc(sqltelemetry.ForceSavepointRestartCounter)
}
m.SetForceSavepointRestart(b)
return nil
},
GlobalDefault: globalFalse,
},
// See https://www.postgresql.org/docs/10/static/runtime-config-preset.html
`integer_datetimes`: makeReadOnlyVar("on"),
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-INTERVALSTYLE
`intervalstyle`: {
Set: func(ctx context.Context, m sessionDataMutator, s string) error {
styleVal, ok := duration.IntervalStyle_value[strings.ToUpper(s)]
if !ok {
validIntervalStyles := make([]string, 0, len(duration.IntervalStyle_value))
for k := range duration.IntervalStyle_value {
validIntervalStyles = append(validIntervalStyles, strings.ToLower(k))
}
return newVarValueError(`IntervalStyle`, s, validIntervalStyles...)
}
style := duration.IntervalStyle(styleVal)
m.SetIntervalStyle(style)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return strings.ToLower(evalCtx.SessionData().GetIntervalStyle().String()), nil
},
GetFromSessionData: func(sd *sessiondata.SessionData) string {
return strings.ToLower(sd.GetIntervalStyle().String())
},
GlobalDefault: func(sv *settings.Values) string {
return strings.ToLower(duration.IntervalStyle_name[int32(intervalStyle.Get(sv))])
},
Equal: func(a, b *sessiondata.SessionData) bool {
return a.GetIntervalStyle() == b.GetIntervalStyle()
},
},
// This is only kept for backwards compatibility and no longer has any effect.
`intervalstyle_enabled`: makeBackwardsCompatBoolVar(
"intervalstyle_enabled", true,
),
`is_superuser`: {
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatBoolAsPostgresSetting(evalCtx.SessionData().IsSuperuser), nil
},
GetFromSessionData: func(sd *sessiondata.SessionData) string {
return formatBoolAsPostgresSetting(sd.IsSuperuser)
},
GetStringVal: makePostgresBoolGetStringValFn("is_superuser"),
GlobalDefault: globalFalse,
Equal: func(a, b *sessiondata.SessionData) bool {
return a.IsSuperuser == b.IsSuperuser
},
},
// CockroachDB extension.
`large_full_scan_rows`: {
GetStringVal: makeFloatGetStringValFn(`large_full_scan_rows`),
Set: func(_ context.Context, m sessionDataMutator, s string) error {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return err
}
m.SetLargeFullScanRows(f)
return nil
},
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return formatFloatAsPostgresSetting(evalCtx.SessionData().LargeFullScanRows), nil
},
GlobalDefault: func(sv *settings.Values) string {
return formatFloatAsPostgresSetting(largeFullScanRows.Get(sv))
},
},
// CockroachDB extension.
`locality`: {
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
return evalCtx.Locality.String(), nil
},
},
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-LOC-TIMEOUT
`lock_timeout`: {
GetStringVal: makeTimeoutVarGetter(`lock_timeout`),
Set: lockTimeoutVarSet,
Get: func(evalCtx *extendedEvalContext, _ *kv.Txn) (string, error) {
ms := evalCtx.SessionData().LockTimeout.Nanoseconds() / int64(time.Millisecond)
return strconv.FormatInt(ms, 10), nil
},
GlobalDefault: func(sv *settings.Values) string {
return clusterLockTimeout.String(sv)
},
},
// See https://www.postgresql.org/docs/12/runtime-config-client.html.
`default_table_access_method`: makeCompatStringVar(`default_table_access_method`, `heap`),
// See https://www.postgresql.org/docs/13/runtime-config-compatible.html
// CockroachDB only supports safe_encoding for now. If `client_encoding` is updated to
// allow encodings other than UTF8, then the default value of `backslash_quote` should
// be changed to `on`.
`backslash_quote`: makeCompatStringVar(`backslash_quote`, `safe_encoding`),
// See https://www.postgresql.org/docs/9.5/runtime-config-compatible.html
`default_with_oids`: makeCompatBoolVar(`default_with_oids`, false, false),
// See https://www.postgresql.org/docs/current/datatype-xml.html.
`xmloption`: makeCompatStringVar(`xmloption`, `content`),
// Supported for PG compatibility only.
// See https://www.postgresql.org/docs/10/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
`max_identifier_length`: {