-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
vars.go
979 lines (900 loc) · 32.4 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
// 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"
"sort"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/delegate"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sessiondata"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/errors"
)
const (
// PgServerVersion is the latest version of postgres that we claim to support.
PgServerVersion = "9.5.0"
// PgServerVersionNum is the latest version of postgres that we claim to support in the numeric format of "server_version_num".
PgServerVersionNum = "90500"
)
type getStringValFn = func(
ctx context.Context, evalCtx *extendedEvalContext, values []tree.TypedExpr,
) (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) 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, 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
}
func formatBoolAsPostgresSetting(b bool) string {
if b {
return "on"
}
return "off"
}
func parsePostgresBool(s string) (bool, error) {
s = strings.ToLower(s)
switch s {
case "on":
s = "true"
case "off":
s = "false"
}
return strconv.ParseBool(s)
}
// 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) string {
return evalCtx.SessionData.ApplicationName
},
GlobalDefault: func(_ *settings.Values) string { return "" },
},
// 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 := sessiondata.BytesEncodeFormatFromString(s)
if !ok {
return newVarValueError(`bytea_output`, s, "hex", "escape", "base64")
}
m.SetBytesEncodeFormat(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.SessionData.DataConversion.BytesEncodeFormat.String()
},
GlobalDefault: func(sv *settings.Values) string { return sessiondata.BytesEncodeHex.String() },
},
// Supported for PG compatibility only.
// Controls returned message verbosity. We don't support this.
// See https://www.postgresql.org/docs/9.6/static/runtime-config-compatible.html
`client_min_messages`: makeCompatStringVar(`client_min_messages`, `notice`, `debug5`, `debug4`, `debug3`, `debug2`, `debug1`, `debug`, `log`, `warning`, `error`, `fatal`, `panic`),
// 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.Newf("client_encoding "+encoding,
"unimplemented client encoding: %q", encoding)
}
},
Get: func(evalCtx *extendedEvalContext) string { return "UTF8" },
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,
) (string, error) {
dbName, err := getStringVal(&evalCtx.EvalContext, `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.schemaAccessors.logical.GetDatabaseDesc(ctx, evalCtx.Txn, dbName,
DatabaseLookupFlags{required: true}); err != nil {
return "", err
}
}
return dbName, nil
},
Set: func(
ctx context.Context, m *sessionDataMutator, dbName string,
) error {
m.SetDatabase(dbName)
return nil
},
Get: func(evalCtx *extendedEvalContext) string { return evalCtx.SessionData.Database },
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 ""
},
},
// Supported for PG compatibility only.
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-DATESTYLE
`datestyle`: {
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
s = strings.ToLower(s)
parts := strings.Split(s, ",")
if strings.TrimSpace(parts[0]) != "iso" ||
(len(parts) == 2 && strings.TrimSpace(parts[1]) != "mdy") ||
len(parts) > 2 {
err := newVarValueError("DateStyle", s, "ISO", "ISO, MDY")
err = errors.WithDetail(err, compatErrMsg)
return err
}
return nil
},
Get: func(evalCtx *extendedEvalContext) string { return "ISO, MDY" },
GlobalDefault: func(_ *settings.Values) string { return "ISO, MDY" },
},
// 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) string {
return strconv.FormatInt(int64(evalCtx.SessionData.DefaultIntSize), 10)
},
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("default_int_size", val, "%v", err)
}
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(int(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) string {
return ""
},
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) string {
return "serializable"
},
GlobalDefault: func(sv *settings.Values) string { return "default" },
},
// See https://www.postgresql.org/docs/9.3/static/runtime-config-client.html#GUC-DEFAULT-TRANSACTION-READ-ONLY
`default_transaction_read_only`: {
GetStringVal: makeBoolGetStringValFn("default_transaction_read_only"),
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return err
}
m.SetDefaultReadOnly(b)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.SessionData.DefaultReadOnly)
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`distsql`: {
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
mode, ok := sessiondata.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) string {
return evalCtx.SessionData.DistSQLMode.String()
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondata.DistSQLExecMode(DistSQLClusterExecMode.Get(sv)).String()
},
},
// CockroachDB extension.
`experimental_force_split_at`: {
GetStringVal: makeBoolGetStringValFn(`experimental_force_split_at`),
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return err
}
m.SetForceSplitAt(b)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.SessionData.ForceSplitAt)
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`experimental_enable_zigzag_join`: {
GetStringVal: makeBoolGetStringValFn(`experimental_enable_zigzag_join`),
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return err
}
m.SetZigzagJoinEnabled(b)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.SessionData.ZigzagJoinEnabled)
},
GlobalDefault: globalTrue,
},
// 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) string {
return strconv.FormatInt(int64(evalCtx.SessionData.ReorderJoinsLimit), 10)
},
GlobalDefault: func(sv *settings.Values) string {
return strconv.FormatInt(ReorderJoinsLimitClusterValue.Get(sv), 10)
},
},
// CockroachDB extension.
`experimental_vectorize`: {
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
mode, ok := sessiondata.VectorizeExecModeFromString(s)
if !ok {
return newVarValueError(`experimental_vectorize`, s, "off", "on", "always")
}
m.SetVectorize(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.SessionData.Vectorize.String()
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondata.VectorizeExecMode(
VectorizeClusterMode.Get(sv)).String()
},
},
// CockroachDB extension.
`optimizer`: {
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
mode, ok := sessiondata.OptimizerModeFromString(s)
if !ok {
return newVarValueError(`optimizer`, s, "on", "off", "local", "always")
}
m.SetOptimizerMode(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.SessionData.OptimizerMode.String()
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondata.OptimizerMode(
OptimizerClusterMode.Get(sv)).String()
},
},
// CockroachDB extension.
`experimental_optimizer_foreign_keys`: {
GetStringVal: makeBoolGetStringValFn(`experimental_optimizer_foreign_keys`),
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return err
}
m.SetOptimizerFKs(b)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.SessionData.OptimizerFKs)
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`experimental_serial_normalization`: {
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
mode, ok := sessiondata.SerialNormalizationModeFromString(s)
if !ok {
return newVarValueError(`experimental_serial_normalization`, s,
"rowid", "virtual_sequence", "sql_sequence")
}
m.SetSerialNormalizationMode(mode)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.SessionData.SerialNormalizationMode.String()
},
GlobalDefault: func(sv *settings.Values) string {
return sessiondata.SerialNormalizationMode(
SerialNormalizationMode.Get(sv)).String()
},
},
// 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("extra_float_digits", s, "%v", err)
}
// 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(int(i))
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return fmt.Sprintf("%d", evalCtx.SessionData.DataConversion.ExtraFloatDigits)
},
GlobalDefault: func(sv *settings.Values) string { return "0" },
},
// CockroachDB extension. See docs on SessionData.ForceSavepointRestart.
// https://github.com/cockroachdb/cockroach/issues/30588
`force_savepoint_restart`: {
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.SessionData.ForceSavepointRestart)
},
GetStringVal: makeBoolGetStringValFn("force_savepoint_restart"),
Set: func(_ context.Context, m *sessionDataMutator, val string) error {
b, err := parsePostgresBool(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`: makeCompatStringVar(`IntervalStyle`, "postgres"),
// CockroachDB extension.
`locality`: {
Get: func(evalCtx *extendedEvalContext) string {
if len(evalCtx.Locality.Tiers) != 0 {
return evalCtx.Locality.String()
}
return "No locality set."
},
},
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-LOC-TIMEOUT
`lock_timeout`: makeCompatIntVar(`lock_timeout`, 0),
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-IDLE-IN-TRANSACTION-SESSION-TIMEOUT
// See also issue #5924.
`idle_in_transaction_session_timeout`: makeCompatIntVar(`idle_in_transaction_session_timeout`, 0),
// See https://www.postgresql.org/docs/10/static/runtime-config-preset.html#GUC-MAX-INDEX-KEYS
`max_index_keys`: makeReadOnlyVar("32"),
// CockroachDB extension.
`node_id`: {
Get: func(evalCtx *extendedEvalContext) string {
return fmt.Sprintf("%d", evalCtx.NodeID)
},
},
// CockroachDB extension.
// TODO(dan): This should also work with SET.
`results_buffer_size`: {
Get: func(evalCtx *extendedEvalContext) string {
return strconv.FormatInt(evalCtx.SessionData.ResultsBufferSize, 10)
},
},
// CockroachDB extension (inspired by MySQL).
// See https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_sql_safe_updates
`sql_safe_updates`: {
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.SessionData.SafeUpdates)
},
GetStringVal: makeBoolGetStringValFn("sql_safe_updates"),
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return err
}
m.SetSafeUpdates(b)
return nil
},
GlobalDefault: globalFalse,
},
// See https://www.postgresql.org/docs/10/static/ddl-schemas.html#DDL-SCHEMAS-PATH
// https://www.postgresql.org/docs/9.6/static/runtime-config-client.html
`search_path`: {
GetStringVal: func(
_ context.Context, evalCtx *extendedEvalContext, values []tree.TypedExpr,
) (string, error) {
comma := ""
var buf bytes.Buffer
for _, v := range values {
s, err := datumAsString(&evalCtx.EvalContext, "search_path", v)
if err != nil {
return "", err
}
if strings.Contains(s, ",") {
// TODO(knz): if/when we want to support this, we'll need to change
// the interface between GetStringVal() and Set() to take string
// arrays instead of a single string.
return "", unimplemented.Newf("schema names containing commas in search_path",
"schema name %q not supported in search_path", s)
}
buf.WriteString(comma)
buf.WriteString(s)
comma = ","
}
return buf.String(), nil
},
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
paths := strings.Split(s, ",")
m.SetSearchPath(sessiondata.MakeSearchPath(paths))
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.SessionData.SearchPath.String()
},
GlobalDefault: func(sv *settings.Values) string {
return sqlbase.DefaultSearchPath.String()
},
},
// See https://www.postgresql.org/docs/10/static/runtime-config-preset.html#GUC-SERVER-VERSION
`server_version`: makeReadOnlyVar(PgServerVersion),
// See https://www.postgresql.org/docs/10/static/runtime-config-preset.html#GUC-SERVER-VERSION-NUM
`server_version_num`: makeReadOnlyVar(PgServerVersionNum),
// See https://www.postgresql.org/docs/9.4/runtime-config-connection.html
`ssl_renegotiation_limit`: {
Hidden: true,
GetStringVal: makeIntGetStringValFn(`ssl_renegotiation_limit`),
Get: func(_ *extendedEvalContext) string { return "0" },
GlobalDefault: func(_ *settings.Values) string { return "0" },
Set: func(_ context.Context, _ *sessionDataMutator, s string) error {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return wrapSetVarError("ssl_renegotiation_limit", s, "%v", err)
}
if i != 0 {
// See pg src/backend/utils/misc/guc.c: non-zero values are not to be supported.
return newVarValueError("ssl_renegotiation_limit", s, "0")
}
return nil
},
},
// CockroachDB extension.
`crdb_version`: makeReadOnlyVar(build.GetInfo().Short()),
// CockroachDB extension.
// In PG this is a pseudo-function used with SELECT, not SHOW.
// See https://www.postgresql.org/docs/10/static/functions-info.html
`session_user`: {
Get: func(evalCtx *extendedEvalContext) string { return evalCtx.SessionData.User },
},
// See pg sources src/backend/utils/misc/guc.c. The variable is defined
// but is hidden from SHOW ALL.
`session_authorization`: {
Hidden: true,
Get: func(evalCtx *extendedEvalContext) string { return evalCtx.SessionData.User },
},
// Supported for PG compatibility only.
// See https://www.postgresql.org/docs/10/static/runtime-config-compatible.html#GUC-STANDARD-CONFORMING-STRINGS
`standard_conforming_strings`: makeCompatBoolVar(`standard_conforming_strings`, true, false /* anyAllowed */),
// See https://www.postgresql.org/docs/10/static/runtime-config-compatible.html#GUC-SYNCHRONIZE-SEQSCANS
// The default in pg is "on" but the behavior in CockroachDB is "off". As this does not affect
// results received by clients, we accept both values.
`synchronize_seqscans`: makeCompatBoolVar(`synchronize_seqscans`, true, true /* anyAllowed */),
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-ROW-SECURITY
// The default in pg is "on" but row security is not supported in CockroachDB.
// We blindly accept both values because as long as there are now row security policies defined,
// either value produces the same query results in PostgreSQL. That is, as long as CockroachDB
// does not support row security, accepting either "on" and "off" but ignoring the result
// is postgres-compatible.
// If/when CockroachDB is extended to support row security, the default and allowed values
// should be modified accordingly.
`row_security`: makeCompatBoolVar(`row_security`, false, true /* anyAllowed */),
`statement_timeout`: {
GetStringVal: stmtTimeoutVarGetStringVal,
Set: stmtTimeoutVarSet,
Get: func(evalCtx *extendedEvalContext) string {
ms := evalCtx.SessionData.StmtTimeout.Nanoseconds() / int64(time.Millisecond)
return strconv.FormatInt(ms, 10)
},
GlobalDefault: func(sv *settings.Values) string { return "0" },
},
// See https://www.postgresql.org/docs/10/static/runtime-config-client.html#GUC-TIMEZONE
`timezone`: {
Get: func(evalCtx *extendedEvalContext) string {
// If the time zone is a "fixed offset" one, initialized from an offset
// and not a standard name, then we use a magic format in the Location's
// name. We attempt to parse that here and retrieve the original offset
// specified by the user.
locStr := evalCtx.SessionData.DataConversion.Location.String()
_, origRepr, parsed := timeutil.ParseFixedOffsetTimeZone(locStr)
if parsed {
return origRepr
}
return locStr
},
GetStringVal: timeZoneVarGetStringVal,
Set: timeZoneVarSet,
GlobalDefault: func(_ *settings.Values) string { return "UTC" },
},
// This is not directly documented in PG's docs but does indeed behave this way.
// See https://github.com/postgres/postgres/blob/REL_10_STABLE/src/backend/utils/misc/guc.c#L3401-L3409
`transaction_isolation`: {
Get: func(evalCtx *extendedEvalContext) string {
return "serializable"
},
RuntimeSet: func(_ context.Context, evalCtx *extendedEvalContext, s string) error {
_, ok := tree.IsolationLevelMap[s]
if !ok {
return newVarValueError(`transaction_isolation`, s, "serializable")
}
return nil
},
GlobalDefault: func(_ *settings.Values) string { return "serializable" },
},
// CockroachDB extension.
`transaction_priority`: {
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.Txn.UserPriority().String()
},
},
// CockroachDB extension.
`transaction_status`: {
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.TxnState
},
},
// See https://www.postgresql.org/docs/10/static/hot-standby.html#HOT-STANDBY-USERS
`transaction_read_only`: {
GetStringVal: makeBoolGetStringValFn("transaction_read_only"),
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return err
}
m.SetReadOnly(b)
return nil
},
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.TxnReadOnly)
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`tracing`: {
Get: func(evalCtx *extendedEvalContext) string {
sessTracing := evalCtx.Tracing
if sessTracing.Enabled() {
val := "on"
if sessTracing.RecordingType() == tracing.SingleNodeRecording {
val += ", local"
}
if sessTracing.KVTracingEnabled() {
val += ", kv"
}
return val
}
return "off"
},
// Setting is done by the SetTracing statement.
},
// CockroachDB extension.
`allow_prepare_as_opt_plan`: {
Hidden: true,
Get: func(evalCtx *extendedEvalContext) string {
return formatBoolAsPostgresSetting(evalCtx.SessionData.AllowPrepareAsOptPlan)
},
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return err
}
m.SetAllowPrepareAsOptPlan(b)
return nil
},
GlobalDefault: globalFalse,
},
// CockroachDB extension.
`save_tables_prefix`: {
Hidden: true,
Get: func(evalCtx *extendedEvalContext) string {
return evalCtx.SessionData.SaveTablesPrefix
},
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
m.SetSaveTablesPrefix(s)
return nil
},
GlobalDefault: func(_ *settings.Values) string { return "" },
},
}
const compatErrMsg = "this parameter is currently recognized only for compatibility and has no effect in CockroachDB."
func init() {
// Initialize delegate.ValidVars.
for v := range varGen {
delegate.ValidVars[v] = struct{}{}
}
}
func makeBoolGetStringValFn(varName string) getStringValFn {
return func(
ctx context.Context, evalCtx *extendedEvalContext, values []tree.TypedExpr,
) (string, error) {
s, err := getSingleBool(varName, evalCtx, values)
if err != nil {
return "", err
}
return strconv.FormatBool(bool(*s)), nil
}
}
func makeReadOnlyVar(value string) sessionVar {
return sessionVar{
Get: func(_ *extendedEvalContext) string { return value },
GlobalDefault: func(_ *settings.Values) string { return value },
}
}
func displayPgBool(val bool) func(_ *settings.Values) string {
strVal := formatBoolAsPostgresSetting(val)
return func(_ *settings.Values) string { return strVal }
}
var globalTrue = displayPgBool(true)
var globalFalse = displayPgBool(false)
func makeCompatBoolVar(varName string, displayValue, anyValAllowed bool) sessionVar {
displayValStr := formatBoolAsPostgresSetting(displayValue)
return sessionVar{
Get: func(_ *extendedEvalContext) string { return displayValStr },
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
b, err := parsePostgresBool(s)
if err != nil {
return pgerror.WithCandidateCode(err, pgcode.InvalidParameterValue)
}
if anyValAllowed || b == displayValue {
return nil
}
telemetry.Inc(sqltelemetry.UnimplementedSessionVarValueCounter(varName, s))
allowedVals := []string{displayValStr}
if anyValAllowed {
allowedVals = append(allowedVals, formatBoolAsPostgresSetting(!displayValue))
}
err = newVarValueError(varName, s, allowedVals...)
err = errors.WithDetail(err, compatErrMsg)
return err
},
GlobalDefault: func(sv *settings.Values) string { return displayValStr },
}
}
func makeCompatIntVar(varName string, displayValue int, extraAllowed ...int) sessionVar {
displayValueStr := strconv.Itoa(displayValue)
extraAllowedStr := make([]string, len(extraAllowed))
for i, v := range extraAllowed {
extraAllowedStr[i] = strconv.Itoa(v)
}
varObj := makeCompatStringVar(varName, displayValueStr, extraAllowedStr...)
varObj.GetStringVal = makeIntGetStringValFn(varName)
return varObj
}
func makeCompatStringVar(varName, displayValue string, extraAllowed ...string) sessionVar {
allowedVals := append(extraAllowed, strings.ToLower(displayValue))
return sessionVar{
Get: func(_ *extendedEvalContext) string {
return displayValue
},
Set: func(_ context.Context, m *sessionDataMutator, s string) error {
enc := strings.ToLower(s)
for _, a := range allowedVals {
if enc == a {
return nil
}
}
telemetry.Inc(sqltelemetry.UnimplementedSessionVarValueCounter(varName, s))
err := newVarValueError(varName, s, allowedVals...)
err = errors.WithDetail(err, compatErrMsg)
return err
},
GlobalDefault: func(sv *settings.Values) string { return displayValue },
}
}
// makeIntGetStringValFn returns a getStringValFn which allows
// the user to provide plain integer values to a SET variable.
func makeIntGetStringValFn(name string) getStringValFn {
return func(ctx context.Context, evalCtx *extendedEvalContext, values []tree.TypedExpr) (string, error) {
s, err := getIntVal(&evalCtx.EvalContext, name, values)
if err != nil {
return "", err
}
return strconv.FormatInt(s, 10), nil
}
}
// IsSessionVariableConfigurable returns true iff there is a session
// variable with the given name and it is settable by a client
// (e.g. in pgwire).
func IsSessionVariableConfigurable(varName string) (exists, configurable bool) {
v, exists := varGen[varName]
return exists, v.Set != nil
}
var varNames = func() []string {
res := make([]string, 0, len(varGen))
for vName := range varGen {
res = append(res, vName)
}
sort.Strings(res)
return res
}()
func getSingleBool(
name string, evalCtx *extendedEvalContext, values []tree.TypedExpr,
) (*tree.DBool, error) {
if len(values) != 1 {
return nil, newSingleArgVarError(name)
}
val, err := values[0].Eval(&evalCtx.EvalContext)
if err != nil {
return nil, err
}
b, ok := val.(*tree.DBool)
if !ok {
err = pgerror.Newf(pgcode.InvalidParameterValue,
"parameter %q requires a Boolean value", name)
err = errors.WithDetailf(err,
"%s is a %s", values[0], errors.Safe(val.ResolvedType()))
return nil, err
}
return b, nil
}
func getSessionVar(name string, missingOk bool) (bool, sessionVar, error) {
if _, ok := UnsupportedVars[name]; ok {
return false, sessionVar{}, unimplemented.Newf("set."+name,
"the configuration setting %q is not supported", name)
}
v, ok := varGen[name]
if !ok {
if missingOk {
return false, sessionVar{}, nil
}
return false, sessionVar{}, pgerror.Newf(pgcode.UndefinedObject,
"unrecognized configuration parameter %q", name)
}
return true, v, nil
}
// GetSessionVar implements the EvalSessionAccessor interface.
func (p *planner) GetSessionVar(
_ context.Context, varName string, missingOk bool,
) (bool, string, error) {
name := strings.ToLower(varName)
ok, v, err := getSessionVar(name, missingOk)
if err != nil || !ok {
return ok, "", err
}
return true, v.Get(&p.extendedEvalCtx), nil
}
// SetSessionVar implements the EvalSessionAccessor interface.
func (p *planner) SetSessionVar(ctx context.Context, varName, newVal string) error {
name := strings.ToLower(varName)
_, v, err := getSessionVar(name, false /* missingOk */)
if err != nil {
return err
}
if v.Set == nil && v.RuntimeSet == nil {
return newCannotChangeParameterError(name)
}
if v.RuntimeSet != nil {
return v.RuntimeSet(ctx, &p.extendedEvalCtx, newVal)
}
return v.Set(ctx, p.sessionDataMutator, newVal)
}