-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
exec_util.go
3483 lines (3074 loc) · 124 KB
/
exec_util.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 2015 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"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"net"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"github.com/cockroachdb/apd/v3"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/cloud/externalconn"
"github.com/cockroachdb/cockroach/pkg/clusterversion"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/featureflag"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/jobs"
"github.com/cockroachdb/cockroach/pkg/jobs/jobspb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvcoord"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/kvtenant"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangecache"
"github.com/cockroachdb/cockroach/pkg/kv/kvclient/rangefeed"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverbase"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/protectedts"
"github.com/cockroachdb/cockroach/pkg/multitenant"
"github.com/cockroachdb/cockroach/pkg/obs"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/server/pgurl"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/server/status/statuspb"
"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/spanconfig"
"github.com/cockroachdb/cockroach/pkg/sql/cacheutil"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/lease"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/schemaexpr"
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
"github.com/cockroachdb/cockroach/pkg/sql/contention"
"github.com/cockroachdb/cockroach/pkg/sql/distsql"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra"
"github.com/cockroachdb/cockroach/pkg/sql/execinfra/execopnode"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/gcjob/gcjobnotifier"
"github.com/cockroachdb/cockroach/pkg/sql/idxusage"
"github.com/cockroachdb/cockroach/pkg/sql/lex"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/optionalnodeliveness"
"github.com/cockroachdb/cockroach/pkg/sql/parser"
"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/pgwire/pgwirecancel"
"github.com/cockroachdb/cockroach/pkg/sql/physicalplan"
"github.com/cockroachdb/cockroach/pkg/sql/querycache"
"github.com/cockroachdb/cockroach/pkg/sql/rowenc"
"github.com/cockroachdb/cockroach/pkg/sql/rowinfra"
"github.com/cockroachdb/cockroach/pkg/sql/scheduledlogging"
"github.com/cockroachdb/cockroach/pkg/sql/schemachanger/scexec"
"github.com/cockroachdb/cockroach/pkg/sql/sem/asof"
"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/sessioninit"
"github.com/cockroachdb/cockroach/pkg/sql/sessionphase"
"github.com/cockroachdb/cockroach/pkg/sql/sqlliveness"
"github.com/cockroachdb/cockroach/pkg/sql/sqlstats"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/sql/stats"
"github.com/cockroachdb/cockroach/pkg/sql/stmtdiagnostics"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/upgrade"
"github.com/cockroachdb/cockroach/pkg/util/bitarray"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/metric"
"github.com/cockroachdb/cockroach/pkg/util/mon"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil/pgdate"
"github.com/cockroachdb/cockroach/pkg/util/tracing"
"github.com/cockroachdb/cockroach/pkg/util/tracing/collector"
"github.com/cockroachdb/cockroach/pkg/util/tracing/tracingpb"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/redact"
)
// ClusterOrganization is the organization name.
var ClusterOrganization = settings.RegisterStringSetting(
settings.TenantWritable,
"cluster.organization",
"organization name",
"",
).WithPublic()
// ClusterIsInternal returns true if the cluster organization contains
// "Cockroach Labs", indicating an internal cluster.
func ClusterIsInternal(sv *settings.Values) bool {
return strings.Contains(ClusterOrganization.Get(sv), "Cockroach Labs")
}
// ClusterSecret is a cluster specific secret. This setting is
// non-reportable.
var ClusterSecret = func() *settings.StringSetting {
s := settings.RegisterStringSetting(
settings.TenantWritable,
"cluster.secret",
"cluster specific secret",
"",
)
// Even though string settings are non-reportable by default, we
// still mark them explicitly in case a future code change flips the
// default.
s.SetReportable(false)
return s
}()
// defaultIntSize controls how a "naked" INT type will be parsed.
// TODO(bob): Change this to 4 in v2.3; https://github.com/cockroachdb/cockroach/issues/32534
// TODO(bob): Remove or n-op this in v2.4: https://github.com/cockroachdb/cockroach/issues/32844
var defaultIntSize = func() *settings.IntSetting {
s := settings.RegisterIntSetting(
settings.TenantWritable,
"sql.defaults.default_int_size",
"the size, in bytes, of an INT type", 8, func(i int64) error {
if i != 4 && i != 8 {
return errors.New("only 4 or 8 are valid values")
}
return nil
}).WithPublic()
s.SetVisibility(settings.Public)
return s
}()
const allowCrossDatabaseFKsSetting = "sql.cross_db_fks.enabled"
var allowCrossDatabaseFKs = settings.RegisterBoolSetting(
settings.TenantWritable,
allowCrossDatabaseFKsSetting,
"if true, creating foreign key references across databases is allowed",
false,
).WithPublic()
const allowCrossDatabaseViewsSetting = "sql.cross_db_views.enabled"
var allowCrossDatabaseViews = settings.RegisterBoolSetting(
settings.TenantWritable,
allowCrossDatabaseViewsSetting,
"if true, creating views that refer to other databases is allowed",
false,
).WithPublic()
const allowCrossDatabaseSeqOwnerSetting = "sql.cross_db_sequence_owners.enabled"
var allowCrossDatabaseSeqOwner = settings.RegisterBoolSetting(
settings.TenantWritable,
allowCrossDatabaseSeqOwnerSetting,
"if true, creating sequences owned by tables from other databases is allowed",
false,
).WithPublic()
const allowCrossDatabaseSeqReferencesSetting = "sql.cross_db_sequence_references.enabled"
var allowCrossDatabaseSeqReferences = settings.RegisterBoolSetting(
settings.TenantWritable,
allowCrossDatabaseSeqReferencesSetting,
"if true, sequences referenced by tables from other databases are allowed",
false,
).WithPublic()
// SecondaryTenantsZoneConfigsEnabledSettingName controls if secondary tenants
// are allowed to set zone configurations. It has no effect for the system
// tenant.
const SecondaryTenantsZoneConfigsEnabledSettingName = "sql.zone_configs.allow_for_secondary_tenant.enabled"
// secondaryTenantZoneConfigsEnabled controls if secondary tenants are allowed
// to set zone configurations. It has no effect for the system tenant.
//
// This setting has no effect on zone configurations that have already been set.
var secondaryTenantZoneConfigsEnabled = settings.RegisterBoolSetting(
settings.TenantReadOnly,
SecondaryTenantsZoneConfigsEnabledSettingName,
"allow secondary tenants to set zone configurations; does not affect the system tenant",
false,
)
// traceTxnThreshold can be used to log SQL transactions that take
// longer than duration to complete. For example, traceTxnThreshold=1s
// will log the trace for any transaction that takes 1s or longer. To
// log traces for all transactions use traceTxnThreshold=1ns. Note
// that any positive duration will enable tracing and will slow down
// all execution because traces are gathered for all transactions even
// if they are not output.
var traceTxnThreshold = settings.RegisterDurationSetting(
settings.TenantWritable,
"sql.trace.txn.enable_threshold",
"enables tracing on all transactions; transactions open for longer than "+
"this duration will have their trace logged (set to 0 to disable); "+
"note that enabling this may have a negative performance impact; "+
"this setting is coarser-grained than sql.trace.stmt.enable_threshold "+
"because it applies to all statements within a transaction as well as "+
"client communication (e.g. retries)", 0,
).WithPublic()
// TraceStmtThreshold is identical to traceTxnThreshold except it applies to
// individual statements in a transaction. The motivation for this setting is
// to be able to reduce the noise associated with a larger transaction (e.g.
// round trips to client).
var TraceStmtThreshold = settings.RegisterDurationSetting(
settings.TenantWritable,
"sql.trace.stmt.enable_threshold",
"enables tracing on all statements; statements executing for longer than "+
"this duration will have their trace logged (set to 0 to disable); "+
"note that enabling this may have a negative performance impact; "+
"this setting applies to individual statements within a transaction and "+
"is therefore finer-grained than sql.trace.txn.enable_threshold",
0,
).WithPublic()
// traceSessionEventLogEnabled can be used to enable the event log
// that is normally kept for every SQL connection. The event log has a
// non-trivial performance impact and also reveals SQL statements
// which may be a privacy concern.
var traceSessionEventLogEnabled = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.trace.session_eventlog.enabled",
"set to true to enable session tracing; "+
"note that enabling this may have a negative performance impact",
false,
).WithPublic()
// ReorderJoinsLimitClusterSettingName is the name of the cluster setting for
// the maximum number of joins to reorder.
const ReorderJoinsLimitClusterSettingName = "sql.defaults.reorder_joins_limit"
// ReorderJoinsLimitClusterValue controls the cluster default for the maximum
// number of joins reordered.
var ReorderJoinsLimitClusterValue = settings.RegisterIntSetting(
settings.TenantWritable,
ReorderJoinsLimitClusterSettingName,
"default number of joins to reorder",
opt.DefaultJoinOrderLimit,
func(limit int64) error {
if limit < 0 || limit > opt.MaxReorderJoinsLimit {
return pgerror.Newf(pgcode.InvalidParameterValue,
"cannot set %s to a value less than 0 or greater than %v",
ReorderJoinsLimitClusterSettingName,
opt.MaxReorderJoinsLimit,
)
}
return nil
},
).WithPublic()
var requireExplicitPrimaryKeysClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.require_explicit_primary_keys.enabled",
"default value for requiring explicit primary keys in CREATE TABLE statements",
false,
).WithPublic()
var placementEnabledClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.multiregion_placement_policy.enabled",
"default value for enable_multiregion_placement_policy;"+
" allows for use of PLACEMENT RESTRICTED",
false,
)
var autoRehomingEnabledClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.experimental_auto_rehoming.enabled",
"default value for experimental_enable_auto_rehoming;"+
" allows for rows in REGIONAL BY ROW tables to be auto-rehomed on UPDATE",
false,
).WithPublic()
var onUpdateRehomeRowEnabledClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.on_update_rehome_row.enabled",
"default value for on_update_rehome_row;"+
" enables ON UPDATE rehome_row() expressions to trigger on updates",
true,
).WithPublic()
var temporaryTablesEnabledClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.experimental_temporary_tables.enabled",
"default value for experimental_enable_temp_tables; allows for use of temporary tables by default",
false,
).WithPublic()
var implicitColumnPartitioningEnabledClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.experimental_implicit_column_partitioning.enabled",
"default value for experimental_enable_temp_tables; allows for the use of implicit column partitioning",
false,
).WithPublic()
var overrideMultiRegionZoneConfigClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.override_multi_region_zone_config.enabled",
"default value for override_multi_region_zone_config; "+
"allows for overriding the zone configs of a multi-region table or database",
false,
).WithPublic()
var maxHashShardedIndexRangePreSplit = settings.RegisterIntSetting(
settings.SystemOnly,
"sql.hash_sharded_range_pre_split.max",
"max pre-split ranges to have when adding hash sharded index to an existing table",
16,
settings.PositiveInt,
).WithPublic()
var zigzagJoinClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.zigzag_join.enabled",
"default value for enable_zigzag_join session setting; allows use of zig-zag join by default",
true,
).WithPublic()
var optDrivenFKCascadesClusterLimit = settings.RegisterIntSetting(
settings.TenantWritable,
"sql.defaults.foreign_key_cascades_limit",
"default value for foreign_key_cascades_limit session setting; limits the number of cascading operations that run as part of a single query",
10000,
settings.NonNegativeInt,
).WithPublic()
var preferLookupJoinsForFKs = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.prefer_lookup_joins_for_fks.enabled",
"default value for prefer_lookup_joins_for_fks session setting; causes foreign key operations to use lookup joins when possible",
false,
).WithPublic()
// optUseHistogramsClusterMode controls the cluster default for whether
// histograms are used by the optimizer for cardinality estimation.
// Note that it does not control histogram collection; regardless of the
// value of this setting, the optimizer cannot use histograms if they
// haven't been collected. Collection of histograms is controlled by the
// cluster setting sql.stats.histogram_collection.enabled.
var optUseHistogramsClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.optimizer_use_histograms.enabled",
"default value for optimizer_use_histograms session setting; enables usage of histograms in the optimizer by default",
true,
).WithPublic()
// optUseMultiColStatsClusterMode controls the cluster default for whether
// multi-column stats are used by the optimizer for cardinality estimation.
// Note that it does not control collection of multi-column stats; regardless
// of the value of this setting, the optimizer cannot use multi-column stats
// if they haven't been collected. Collection of multi-column stats is
// controlled by the cluster setting sql.stats.multi_column_collection.enabled.
var optUseMultiColStatsClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.optimizer_use_multicol_stats.enabled",
"default value for optimizer_use_multicol_stats session setting; enables usage of multi-column stats in the optimizer by default",
true,
).WithPublic()
// optUseNotVisibleIndexesClusterMode controls the cluster default for whether
// not visible indexes can still be chosen by the optimizer for query plans. If
// enabled, the optimizer will treat not visible indexes as they are visible.
// Note that not visible indexes remain not visible, but the optimizer will
// disable not visible index feature. If disabled, optimizer will ignore not
// visible indexes unless it is explicitly selected with force index or for
// constraint check.
var optUseNotVisibleIndexesClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.optimizer_use_not_visible_indexes.enabled",
"default value for optimizer_use_not_visible_indexes session setting; "+
"disable usage of not visible indexes in the optimizer by default",
false,
).WithPublic()
// localityOptimizedSearchMode controls the cluster default for the use of
// locality optimized search. If enabled, the optimizer will try to plan scans
// and lookup joins in which local nodes (i.e., nodes in the gateway region) are
// searched for matching rows before remote nodes, in the hope that the
// execution engine can avoid visiting remote nodes.
var localityOptimizedSearchMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.locality_optimized_partitioned_index_scan.enabled",
"default value for locality_optimized_partitioned_index_scan session setting; "+
"enables searching for rows in the current region before searching remote regions",
true,
).WithPublic()
var implicitSelectForUpdateClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.implicit_select_for_update.enabled",
"default value for enable_implicit_select_for_update session setting; enables FOR UPDATE locking during the row-fetch phase of mutation statements",
true,
).WithPublic()
var insertFastPathClusterMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.insert_fast_path.enabled",
"default value for enable_insert_fast_path session setting; enables a specialized insert path",
true,
).WithPublic()
var experimentalAlterColumnTypeGeneralMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.experimental_alter_column_type.enabled",
"default value for experimental_alter_column_type session setting; "+
"enables the use of ALTER COLUMN TYPE for general conversions",
false,
).WithPublic()
var clusterStatementTimeout = settings.RegisterDurationSetting(
settings.TenantWritable,
"sql.defaults.statement_timeout",
"default value for the statement_timeout; "+
"default value for the statement_timeout session setting; controls the "+
"duration a query is permitted to run before it is canceled; if set to 0, "+
"there is no timeout",
0,
settings.NonNegativeDuration,
).WithPublic()
var clusterLockTimeout = settings.RegisterDurationSetting(
settings.TenantWritable,
"sql.defaults.lock_timeout",
"default value for the lock_timeout; "+
"default value for the lock_timeout session setting; controls the "+
"duration a query is permitted to wait while attempting to acquire "+
"a lock on a key or while blocking on an existing lock in order to "+
"perform a non-locking read on a key; if set to 0, there is no timeout",
0,
settings.NonNegativeDuration,
).WithPublic()
var clusterIdleInSessionTimeout = settings.RegisterDurationSetting(
settings.TenantWritable,
"sql.defaults.idle_in_session_timeout",
"default value for the idle_in_session_timeout; "+
"default value for the idle_in_session_timeout session setting; controls the "+
"duration a session is permitted to idle before the session is terminated; "+
"if set to 0, there is no timeout",
0,
settings.NonNegativeDuration,
).WithPublic()
var clusterIdleInTransactionSessionTimeout = settings.RegisterDurationSetting(
settings.TenantWritable,
"sql.defaults.idle_in_transaction_session_timeout",
"default value for the idle_in_transaction_session_timeout; controls the "+
"duration a session is permitted to idle in a transaction before the "+
"session is terminated; if set to 0, there is no timeout",
0,
settings.NonNegativeDuration,
).WithPublic()
// TODO(rytaft): remove this once unique without index constraints are fully
// supported.
var experimentalUniqueWithoutIndexConstraintsMode = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.experimental_enable_unique_without_index_constraints.enabled",
"default value for experimental_enable_unique_without_index_constraints session setting;"+
"disables unique without index constraints by default",
false,
).WithPublic()
var experimentalUseNewSchemaChanger = settings.RegisterEnumSetting(
settings.TenantWritable,
"sql.defaults.use_declarative_schema_changer",
"default value for use_declarative_schema_changer session setting;"+
"disables new schema changer by default",
"on",
map[int64]string{
int64(sessiondatapb.UseNewSchemaChangerOff): "off",
int64(sessiondatapb.UseNewSchemaChangerOn): "on",
int64(sessiondatapb.UseNewSchemaChangerUnsafe): "unsafe",
int64(sessiondatapb.UseNewSchemaChangerUnsafeAlways): "unsafe_always",
},
).WithPublic()
var experimentalStreamReplicationEnabled = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.experimental_stream_replication.enabled",
"default value for experimental_stream_replication session setting;"+
"enables the ability to setup a replication stream",
false,
).WithPublic()
var stubCatalogTablesEnabledClusterValue = settings.RegisterBoolSetting(
settings.TenantWritable,
`sql.defaults.stub_catalog_tables.enabled`,
`default value for stub_catalog_tables session setting`,
true,
).WithPublic()
var experimentalComputedColumnRewrites = settings.RegisterValidatedStringSetting(
settings.TenantWritable,
"sql.defaults.experimental_computed_column_rewrites",
"allows rewriting computed column expressions in CREATE TABLE and ALTER TABLE statements; "+
"the format is: '(before expression) -> (after expression) [, (before expression) -> (after expression) ...]'",
"", /* defaultValue */
func(_ *settings.Values, val string) error {
_, err := schemaexpr.ParseComputedColumnRewrites(val)
return err
},
)
var propagateInputOrdering = settings.RegisterBoolSetting(
settings.TenantWritable,
`sql.defaults.propagate_input_ordering.enabled`,
`default value for the experimental propagate_input_ordering session variable`,
false,
)
// settingWorkMemBytes is a cluster setting that determines the maximum amount
// of RAM that a processor can use.
var settingWorkMemBytes = settings.RegisterByteSizeSetting(
settings.TenantWritable,
"sql.distsql.temp_storage.workmem",
"maximum amount of memory in bytes a processor can use before falling back to temp storage",
execinfra.DefaultMemoryLimit, /* 64MiB */
func(v int64) error {
if v <= 1 {
return errors.Errorf("can only be set to a value greater than 1: %d", v)
}
return nil
},
).WithPublic()
// ExperimentalDistSQLPlanningClusterSettingName is the name for the cluster
// setting that controls experimentalDistSQLPlanningClusterMode below.
const ExperimentalDistSQLPlanningClusterSettingName = "sql.defaults.experimental_distsql_planning"
// experimentalDistSQLPlanningClusterMode can be used to enable
// optimizer-driven DistSQL planning that sidesteps intermediate planNode
// transition when going from opt.Expr to DistSQL processor specs.
var experimentalDistSQLPlanningClusterMode = settings.RegisterEnumSetting(
settings.TenantWritable,
ExperimentalDistSQLPlanningClusterSettingName,
"default experimental_distsql_planning mode; enables experimental opt-driven DistSQL planning",
"off",
map[int64]string{
int64(sessiondatapb.ExperimentalDistSQLPlanningOff): "off",
int64(sessiondatapb.ExperimentalDistSQLPlanningOn): "on",
},
).WithPublic()
// VectorizeClusterSettingName is the name for the cluster setting that controls
// the VectorizeClusterMode below.
const VectorizeClusterSettingName = "sql.defaults.vectorize"
// VectorizeClusterMode controls the cluster default for when automatic
// vectorization is enabled.
var VectorizeClusterMode = settings.RegisterEnumSetting(
settings.TenantWritable,
VectorizeClusterSettingName,
"default vectorize mode",
"on",
map[int64]string{
int64(sessiondatapb.VectorizeUnset): "on",
int64(sessiondatapb.VectorizeOn): "on",
int64(sessiondatapb.VectorizeExperimentalAlways): "experimental_always",
int64(sessiondatapb.VectorizeOff): "off",
},
).WithPublic()
// DistSQLClusterExecMode controls the cluster default for when DistSQL is used.
var DistSQLClusterExecMode = settings.RegisterEnumSetting(
settings.TenantWritable,
"sql.defaults.distsql",
"default distributed SQL execution mode",
"auto",
map[int64]string{
int64(sessiondatapb.DistSQLOff): "off",
int64(sessiondatapb.DistSQLAuto): "auto",
int64(sessiondatapb.DistSQLOn): "on",
int64(sessiondatapb.DistSQLAlways): "always",
},
).WithPublic()
// SerialNormalizationMode controls how the SERIAL type is interpreted in table
// definitions.
var SerialNormalizationMode = settings.RegisterEnumSetting(
settings.TenantWritable,
"sql.defaults.serial_normalization",
"default handling of SERIAL in table definitions",
"rowid",
map[int64]string{
int64(sessiondatapb.SerialUsesRowID): "rowid",
int64(sessiondatapb.SerialUsesUnorderedRowID): "unordered_rowid",
int64(sessiondatapb.SerialUsesVirtualSequences): "virtual_sequence",
int64(sessiondatapb.SerialUsesSQLSequences): "sql_sequence",
int64(sessiondatapb.SerialUsesCachedSQLSequences): "sql_sequence_cached",
},
).WithPublic()
var disallowFullTableScans = settings.RegisterBoolSetting(
settings.TenantWritable,
`sql.defaults.disallow_full_table_scans.enabled`,
"setting to true rejects queries that have planned a full table scan",
false,
).WithPublic()
// intervalStyle controls intervals representation.
var intervalStyle = settings.RegisterEnumSetting(
settings.TenantWritable,
"sql.defaults.intervalstyle",
"default value for IntervalStyle session setting",
strings.ToLower(duration.IntervalStyle_POSTGRES.String()),
func() map[int64]string {
ret := make(map[int64]string, len(duration.IntervalStyle_name))
for k, v := range duration.IntervalStyle_name {
ret[int64(k)] = strings.ToLower(v)
}
return ret
}(),
).WithPublic()
var dateStyleEnumMap = map[int64]string{
0: "ISO, MDY",
1: "ISO, DMY",
2: "ISO, YMD",
}
// dateStyle controls dates representation.
var dateStyle = settings.RegisterEnumSetting(
settings.TenantWritable,
"sql.defaults.datestyle",
"default value for DateStyle session setting",
pgdate.DefaultDateStyle().SQLString(),
dateStyleEnumMap,
).WithPublic()
var txnRowsWrittenLog = settings.RegisterIntSetting(
settings.TenantWritable,
"sql.defaults.transaction_rows_written_log",
"the threshold for the number of rows written by a SQL transaction "+
"which - once exceeded - will trigger a logging event to SQL_PERF (or "+
"SQL_INTERNAL_PERF for internal transactions); use 0 to disable",
0,
settings.NonNegativeInt,
).WithPublic()
var txnRowsWrittenErr = settings.RegisterIntSetting(
settings.TenantWritable,
"sql.defaults.transaction_rows_written_err",
"the limit for the number of rows written by a SQL transaction which - "+
"once exceeded - will fail the transaction (or will trigger a logging "+
"event to SQL_INTERNAL_PERF for internal transactions); use 0 to disable",
0,
settings.NonNegativeInt,
).WithPublic()
var txnRowsReadLog = settings.RegisterIntSetting(
settings.TenantWritable,
"sql.defaults.transaction_rows_read_log",
"the threshold for the number of rows read by a SQL transaction "+
"which - once exceeded - will trigger a logging event to SQL_PERF (or "+
"SQL_INTERNAL_PERF for internal transactions); use 0 to disable",
0,
settings.NonNegativeInt,
).WithPublic()
var txnRowsReadErr = settings.RegisterIntSetting(
settings.TenantWritable,
"sql.defaults.transaction_rows_read_err",
"the limit for the number of rows read by a SQL transaction which - "+
"once exceeded - will fail the transaction (or will trigger a logging "+
"event to SQL_INTERNAL_PERF for internal transactions); use 0 to disable",
0,
settings.NonNegativeInt,
).WithPublic()
// This is a float setting (rather than an int setting) because the optimizer
// uses floating point for calculating row estimates.
var largeFullScanRows = settings.RegisterFloatSetting(
settings.TenantWritable,
"sql.defaults.large_full_scan_rows",
"default value for large_full_scan_rows session setting which determines "+
"the maximum table size allowed for a full scan when disallow_full_table_scans "+
"is set to true",
1000.0,
).WithPublic()
var costScansWithDefaultColSize = settings.RegisterBoolSetting(
settings.TenantWritable,
`sql.defaults.cost_scans_with_default_col_size.enabled`,
"setting to true uses the same size for all columns to compute scan cost",
false,
).WithPublic()
var enableSuperRegions = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.super_regions.enabled",
"default value for enable_super_regions; "+
"allows for the usage of super regions",
false,
).WithPublic()
var overrideAlterPrimaryRegionInSuperRegion = settings.RegisterBoolSetting(
settings.TenantWritable,
"sql.defaults.override_alter_primary_region_in_super_region.enabled",
"default value for override_alter_primary_region_in_super_region; "+
"allows for altering the primary region even if the primary region is a "+
"member of a super region",
false,
).WithPublic()
var errNoTransactionInProgress = errors.New("there is no transaction in progress")
var errTransactionInProgress = errors.New("there is already a transaction in progress")
const sqlTxnName string = "sql txn"
const metricsSampleInterval = 10 * time.Second
// Fully-qualified names for metrics.
var (
MetaSQLExecLatency = metric.Metadata{
Name: "sql.exec.latency",
Help: "Latency of SQL statement execution",
Measurement: "Latency",
Unit: metric.Unit_NANOSECONDS,
}
MetaSQLServiceLatency = metric.Metadata{
Name: "sql.service.latency",
Help: "Latency of SQL request execution",
Measurement: "Latency",
Unit: metric.Unit_NANOSECONDS,
}
MetaSQLOptFallback = metric.Metadata{
Name: "sql.optimizer.fallback.count",
Help: "Number of statements which the cost-based optimizer was unable to plan",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSQLOptPlanCacheHits = metric.Metadata{
Name: "sql.optimizer.plan_cache.hits",
Help: "Number of non-prepared statements for which a cached plan was used",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSQLOptPlanCacheMisses = metric.Metadata{
Name: "sql.optimizer.plan_cache.misses",
Help: "Number of non-prepared statements for which a cached plan was not used",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaDistSQLSelect = metric.Metadata{
Name: "sql.distsql.select.count",
Help: "Number of DistSQL SELECT statements",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaDistSQLExecLatency = metric.Metadata{
Name: "sql.distsql.exec.latency",
Help: "Latency of DistSQL statement execution",
Measurement: "Latency",
Unit: metric.Unit_NANOSECONDS,
}
MetaDistSQLServiceLatency = metric.Metadata{
Name: "sql.distsql.service.latency",
Help: "Latency of DistSQL request execution",
Measurement: "Latency",
Unit: metric.Unit_NANOSECONDS,
}
MetaTxnAbort = metric.Metadata{
Name: "sql.txn.abort.count",
Help: "Number of SQL transaction abort errors",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaFailure = metric.Metadata{
Name: "sql.failure.count",
Help: "Number of statements resulting in a planning or runtime error",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSQLTxnLatency = metric.Metadata{
Name: "sql.txn.latency",
Help: "Latency of SQL transactions",
Measurement: "Latency",
Unit: metric.Unit_NANOSECONDS,
}
MetaSQLTxnsOpen = metric.Metadata{
Name: "sql.txns.open",
Help: "Number of currently open user SQL transactions",
Measurement: "Open SQL Transactions",
Unit: metric.Unit_COUNT,
}
MetaSQLActiveQueries = metric.Metadata{
Name: "sql.statements.active",
Help: "Number of currently active user SQL statements",
Measurement: "Active Statements",
Unit: metric.Unit_COUNT,
}
MetaFullTableOrIndexScan = metric.Metadata{
Name: "sql.full.scan.count",
Help: "Number of full table or index scans",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
// Below are the metadata for the statement started counters.
MetaQueryStarted = metric.Metadata{
Name: "sql.query.started.count",
Help: "Number of SQL queries started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaTxnBeginStarted = metric.Metadata{
Name: "sql.txn.begin.started.count",
Help: "Number of SQL transaction BEGIN statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaTxnCommitStarted = metric.Metadata{
Name: "sql.txn.commit.started.count",
Help: "Number of SQL transaction COMMIT statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaTxnRollbackStarted = metric.Metadata{
Name: "sql.txn.rollback.started.count",
Help: "Number of SQL transaction ROLLBACK statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSQLTxnContended = metric.Metadata{
Name: "sql.txn.contended.count",
Help: "Number of SQL transactions experienced contention",
Measurement: "Contention",
Unit: metric.Unit_COUNT,
}
MetaSelectStarted = metric.Metadata{
Name: "sql.select.started.count",
Help: "Number of SQL SELECT statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaUpdateStarted = metric.Metadata{
Name: "sql.update.started.count",
Help: "Number of SQL UPDATE statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaInsertStarted = metric.Metadata{
Name: "sql.insert.started.count",
Help: "Number of SQL INSERT statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaDeleteStarted = metric.Metadata{
Name: "sql.delete.started.count",
Help: "Number of SQL DELETE statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSavepointStarted = metric.Metadata{
Name: "sql.savepoint.started.count",
Help: "Number of SQL SAVEPOINT statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaReleaseSavepointStarted = metric.Metadata{
Name: "sql.savepoint.release.started.count",
Help: "Number of `RELEASE SAVEPOINT` statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaRollbackToSavepointStarted = metric.Metadata{
Name: "sql.savepoint.rollback.started.count",
Help: "Number of `ROLLBACK TO SAVEPOINT` statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaRestartSavepointStarted = metric.Metadata{
Name: "sql.restart_savepoint.started.count",
Help: "Number of `SAVEPOINT cockroach_restart` statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaReleaseRestartSavepointStarted = metric.Metadata{
Name: "sql.restart_savepoint.release.started.count",
Help: "Number of `RELEASE SAVEPOINT cockroach_restart` statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaRollbackToRestartSavepointStarted = metric.Metadata{
Name: "sql.restart_savepoint.rollback.started.count",
Help: "Number of `ROLLBACK TO SAVEPOINT cockroach_restart` statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaDdlStarted = metric.Metadata{
Name: "sql.ddl.started.count",
Help: "Number of SQL DDL statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaCopyStarted = metric.Metadata{
Name: "sql.copy.started.count",
Help: "Number of COPY SQL statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaMiscStarted = metric.Metadata{
Name: "sql.misc.started.count",
Help: "Number of other SQL statements started",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
// Below are the metadata for the statement executed counters.
MetaQueryExecuted = metric.Metadata{
Name: "sql.query.count",
Help: "Number of SQL queries executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaTxnBeginExecuted = metric.Metadata{
Name: "sql.txn.begin.count",
Help: "Number of SQL transaction BEGIN statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaTxnCommitExecuted = metric.Metadata{
Name: "sql.txn.commit.count",
Help: "Number of SQL transaction COMMIT statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaTxnRollbackExecuted = metric.Metadata{
Name: "sql.txn.rollback.count",
Help: "Number of SQL transaction ROLLBACK statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSelectExecuted = metric.Metadata{
Name: "sql.select.count",
Help: "Number of SQL SELECT statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaUpdateExecuted = metric.Metadata{
Name: "sql.update.count",
Help: "Number of SQL UPDATE statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaInsertExecuted = metric.Metadata{
Name: "sql.insert.count",
Help: "Number of SQL INSERT statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaDeleteExecuted = metric.Metadata{
Name: "sql.delete.count",
Help: "Number of SQL DELETE statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaSavepointExecuted = metric.Metadata{
Name: "sql.savepoint.count",
Help: "Number of SQL SAVEPOINT statements successfully executed",
Measurement: "SQL Statements",
Unit: metric.Unit_COUNT,
}
MetaReleaseSavepointExecuted = metric.Metadata{
Name: "sql.savepoint.release.count",
Help: "Number of `RELEASE SAVEPOINT` statements successfully executed",
Measurement: "SQL Statements",