-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
paramobserver.go
927 lines (883 loc) · 29.3 KB
/
paramobserver.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
// Copyright 2020 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 paramparse
import (
"context"
"strings"
"github.com/cockroachdb/cockroach/pkg/geo/geoindex"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"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/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/sql/stats"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/duration"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/errors"
)
// SetStorageParameters sets the given storage parameters using the
// given observer.
func SetStorageParameters(
ctx context.Context,
semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext,
params tree.StorageParams,
paramObserver StorageParamObserver,
) error {
for _, sp := range params {
key := string(sp.Key)
if sp.Value == nil {
return pgerror.Newf(pgcode.InvalidParameterValue, "storage parameter %q requires a value", key)
}
telemetry.Inc(sqltelemetry.SetTableStorageParameter(key))
// Expressions may be an unresolved name.
// Cast these as strings.
expr := UnresolvedNameToStrVal(sp.Value)
// Convert the expressions to a datum.
typedExpr, err := tree.TypeCheck(ctx, expr, semaCtx, types.Any)
if err != nil {
return err
}
if typedExpr, err = evalCtx.NormalizeExpr(typedExpr); err != nil {
return err
}
datum, err := typedExpr.Eval(evalCtx)
if err != nil {
return err
}
if err := paramObserver.onSet(ctx, semaCtx, evalCtx, key, datum); err != nil {
return err
}
}
return paramObserver.runPostChecks()
}
// ResetStorageParameters sets the given storage parameters using the
// given observer.
func ResetStorageParameters(
ctx context.Context,
evalCtx *tree.EvalContext,
params tree.NameList,
paramObserver StorageParamObserver,
) error {
for _, p := range params {
telemetry.Inc(sqltelemetry.ResetTableStorageParameter(string(p)))
if err := paramObserver.onReset(evalCtx, string(p)); err != nil {
return err
}
}
return paramObserver.runPostChecks()
}
// StorageParamObserver applies a storage parameter to an underlying item.
type StorageParamObserver interface {
// onSet is called during CREATE [TABLE | INDEX] ... WITH (...) or
// ALTER [TABLE | INDEX] ... WITH (...).
onSet(ctx context.Context, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error
// onReset is called during ALTER [TABLE | INDEX] ... RESET (...)
onReset(evalCtx *tree.EvalContext, key string) error
// runPostChecks is called after all storage parameters have been set.
// This allows checking whether multiple storage parameters together
// form a valid configuration.
runPostChecks() error
}
// TableStorageParamObserver observes storage parameters for tables.
type TableStorageParamObserver struct {
tableDesc *tabledesc.Mutable
setAutomaticColumn bool
}
var _ StorageParamObserver = (*TableStorageParamObserver)(nil)
// NewTableStorageParamObserver returns a new TableStorageParamObserver.
func NewTableStorageParamObserver(tableDesc *tabledesc.Mutable) *TableStorageParamObserver {
return &TableStorageParamObserver{tableDesc: tableDesc}
}
// runPostChecks implements the StorageParamObserver interface.
func (po *TableStorageParamObserver) runPostChecks() error {
ttl := po.tableDesc.GetRowLevelTTL()
if po.setAutomaticColumn && (ttl == nil || ttl.DurationExpr == "") {
return pgerror.Newf(
pgcode.InvalidParameterValue,
`"ttl_expire_after" must be set if "ttl_automatic_column" is set`,
)
}
if err := tabledesc.ValidateRowLevelTTL(ttl); err != nil {
return err
}
return nil
}
func boolFromDatum(evalCtx *tree.EvalContext, key string, datum tree.Datum) (bool, error) {
if stringVal, err := DatumAsString(evalCtx, key, datum); err == nil {
return ParseBoolVar(key, stringVal)
}
s, err := GetSingleBool(key, datum)
if err != nil {
return false, err
}
return bool(*s), nil
}
func intFromDatum(evalCtx *tree.EvalContext, key string, datum tree.Datum) (int64, error) {
intDatum := datum
if stringVal, err := DatumAsString(evalCtx, key, datum); err == nil {
if intDatum, err = tree.ParseDInt(stringVal); err != nil {
return 0, errors.Wrapf(err, "invalid value for %s", key)
}
}
s, err := DatumAsInt(evalCtx, key, intDatum)
if err != nil {
return 0, err
}
return s, nil
}
func floatFromDatum(evalCtx *tree.EvalContext, key string, datum tree.Datum) (float64, error) {
floatDatum := datum
if stringVal, err := DatumAsString(evalCtx, key, datum); err == nil {
if floatDatum, err = tree.ParseDFloat(stringVal); err != nil {
return 0, errors.Wrapf(err, "invalid value for %s", key)
}
}
s, err := DatumAsFloat(evalCtx, key, floatDatum)
if err != nil {
return 0, err
}
return s, nil
}
type tableParam struct {
onSet func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error
onReset func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error
}
var tableParams = map[string]tableParam{
`fillfactor`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
return setFillFactorStorageParam(evalCtx, key, datum)
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
// Operation is a no-op so do nothing.
return nil
},
},
`autovacuum_enabled`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
var boolVal bool
if stringVal, err := DatumAsString(evalCtx, key, datum); err == nil {
boolVal, err = ParseBoolVar(key, stringVal)
if err != nil {
return err
}
} else {
s, err := GetSingleBool(key, datum)
if err != nil {
return err
}
boolVal = bool(*s)
}
if !boolVal && evalCtx != nil {
evalCtx.ClientNoticeSender.BufferClientNotice(
evalCtx.Context,
pgnotice.Newf(`storage parameter "%s = %s" is ignored`, key, datum.String()),
)
}
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
// Operation is a no-op so do nothing.
return nil
},
},
`ttl`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
setTrue, err := boolFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
if setTrue && po.tableDesc.RowLevelTTL == nil {
// Set the base struct, but do not populate it.
// An error from runPostChecks will appear if the requisite fields are not set.
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
if !setTrue && po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL = nil
}
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
po.tableDesc.RowLevelTTL = nil
return nil
},
},
`ttl_automatic_column`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
setTrue, err := boolFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
if setTrue {
po.setAutomaticColumn = true
}
if !setTrue && po.tableDesc.RowLevelTTL != nil {
return unimplemented.NewWithIssue(76916, "unsetting TTL automatic column not yet implemented")
}
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
return unimplemented.NewWithIssue(76916, "unsetting TTL automatic column not yet implemented")
},
},
`ttl_expire_after`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
var d *tree.DInterval
if stringVal, err := DatumAsString(evalCtx, key, datum); err == nil {
d, err = tree.ParseDInterval(evalCtx.SessionData().GetIntervalStyle(), stringVal)
if err != nil || d == nil {
return pgerror.Newf(
pgcode.InvalidParameterValue,
`value of "ttl_expire_after" must be an interval`,
)
}
} else {
var ok bool
d, ok = datum.(*tree.DInterval)
if !ok || d == nil {
return pgerror.Newf(
pgcode.InvalidParameterValue,
`value of "%s" must be an interval`,
key,
)
}
}
if d.Duration.Compare(duration.MakeDuration(0, 0, 0)) < 0 {
return pgerror.Newf(
pgcode.InvalidParameterValue,
`value of "%s" must be at least zero`,
key,
)
}
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
po.tableDesc.RowLevelTTL.DurationExpr = catpb.Expression(tree.Serialize(d))
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
return errors.WithHintf(
pgerror.Newf(
pgcode.InvalidParameterValue,
`resetting "ttl_expire_after" is not permitted`,
),
"use `RESET (ttl)` to remove TTL from the table",
)
},
},
`ttl_select_batch_size`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLBatchSize(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.SelectBatchSize = val
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.SelectBatchSize = 0
}
return nil
},
},
`ttl_delete_batch_size`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLBatchSize(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.DeleteBatchSize = val
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.DeleteBatchSize = 0
}
return nil
},
},
`ttl_range_concurrency`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLRangeConcurrency(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.RangeConcurrency = val
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.RangeConcurrency = 0
}
return nil
},
},
`ttl_delete_rate_limit`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
val, err := DatumAsInt(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLRateLimit(key, val); err != nil {
return err
}
po.tableDesc.RowLevelTTL.DeleteRateLimit = val
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.DeleteRateLimit = 0
}
return nil
},
},
`ttl_job_cron`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
str, err := DatumAsString(evalCtx, key, datum)
if err != nil {
return err
}
if err := tabledesc.ValidateTTLCronExpr(key, str); err != nil {
return err
}
po.tableDesc.RowLevelTTL.DeletionCron = str
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
if po.tableDesc.RowLevelTTL != nil {
po.tableDesc.RowLevelTTL.DeletionCron = ""
}
return nil
},
},
`ttl_pause`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
b, err := boolFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
po.tableDesc.RowLevelTTL.Pause = b
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
po.tableDesc.RowLevelTTL.Pause = false
return nil
},
},
`ttl_row_stats_poll_interval`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
d, err := DatumAsDuration(evalCtx, key, datum)
if err != nil {
return err
}
if po.tableDesc.RowLevelTTL == nil {
po.tableDesc.RowLevelTTL = &catpb.RowLevelTTL{}
}
if err := tabledesc.ValidateTTLRowStatsPollInterval(key, d); err != nil {
return err
}
po.tableDesc.RowLevelTTL.RowStatsPollInterval = d
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
po.tableDesc.RowLevelTTL.RowStatsPollInterval = 0
return nil
},
},
`exclude_data_from_backup`: {
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
if po.tableDesc.Temporary {
return pgerror.Newf(pgcode.FeatureNotSupported,
"cannot set data in a temporary table to be excluded from backup")
}
// Check that the table does not have any incoming FK references. During a
// backup, the rows of a table with ephemeral data will not be backed up, and
// could result in a violation of FK constraints on restore. To prevent this,
// we only allow a table with no incoming FK references to be marked as
// ephemeral.
if len(po.tableDesc.InboundFKs) != 0 {
return errors.New("cannot set data in a table with inbound foreign key constraints to be excluded from backup")
}
excludeDataFromBackup, err := boolFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
// If the table descriptor being changed has the same value for the
// `ExcludeDataFromBackup` flag, no-op.
if po.tableDesc.ExcludeDataFromBackup == excludeDataFromBackup {
return nil
}
po.tableDesc.ExcludeDataFromBackup = excludeDataFromBackup
return nil
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
po.tableDesc.ExcludeDataFromBackup = false
return nil
},
},
stats.AutoStatsClusterSettingName: {
onSet: boolTableSettingFunc,
onReset: tableSettingResetFunc(boolSetting),
},
`sql.stats.automatic_collection.min_stale_rows`: {
onSet: intTableSettingFunc(settings.NonNegativeInt),
onReset: tableSettingResetFunc(intSetting),
},
`sql.stats.automatic_collection.fraction_stale_rows`: {
onSet: floatTableSettingFunc(settings.NonNegativeFloat),
onReset: tableSettingResetFunc(floatSetting),
},
}
func init() {
for _, param := range []string{
`toast_tuple_target`,
`parallel_workers`,
`toast.autovacuum_enabled`,
`autovacuum_vacuum_threshold`,
`toast.autovacuum_vacuum_threshold`,
`autovacuum_vacuum_scale_factor`,
`toast.autovacuum_vacuum_scale_factor`,
`autovacuum_analyze_threshold`,
`autovacuum_analyze_scale_factor`,
`autovacuum_vacuum_cost_delay`,
`toast.autovacuum_vacuum_cost_delay`,
`autovacuum_vacuum_cost_limit`,
`autovacuum_freeze_min_age`,
`toast.autovacuum_freeze_min_age`,
`autovacuum_freeze_max_age`,
`toast.autovacuum_freeze_max_age`,
`autovacuum_freeze_table_age`,
`toast.autovacuum_freeze_table_age`,
`autovacuum_multixact_freeze_min_age`,
`toast.autovacuum_multixact_freeze_min_age`,
`autovacuum_multixact_freeze_max_age`,
`toast.autovacuum_multixact_freeze_max_age`,
`autovacuum_multixact_freeze_table_age`,
`toast.autovacuum_multixact_freeze_table_age`,
`log_autovacuum_min_duration`,
`toast.log_autovacuum_min_duration`,
`user_catalog_table`,
} {
tableParams[param] = tableParam{
onSet: func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext, evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
return unimplemented.NewWithIssuef(43299, "storage parameter %q", key)
},
onReset: func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
return nil
},
}
}
}
type settingDataType int
const (
boolSetting = iota
intSetting
floatSetting
)
func boolTableSettingFunc(
ctx context.Context,
po *TableStorageParamObserver,
semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext,
key string,
datum tree.Datum,
) error {
boolVal, err := boolFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
if po.tableDesc.TableLevelSettings == nil {
po.tableDesc.TableLevelSettings = &catpb.TableLevelSettings{}
}
var settingPtr **catpb.Bool
var ok bool
if settingPtr, ok = settingValuePointer(key, po.tableDesc.TableLevelSettings).(**catpb.Bool); !ok {
return errors.Newf("table setting %s has unexpected type", key)
}
if settingPtr == nil {
return errors.Newf("unable to set table setting %s", key)
}
setting := *settingPtr
if setting == nil {
*settingPtr = &catpb.Bool{Value: boolVal}
return nil
} else if setting.Value == boolVal {
return nil
}
setting.Value = boolVal
return nil
}
func intTableSettingFunc(
validateFunc func(v int64) error,
) func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
return func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
intVal, err := intFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
if po.tableDesc.TableLevelSettings == nil {
po.tableDesc.TableLevelSettings = &catpb.TableLevelSettings{}
}
var settingPtr **catpb.Int
var ok bool
if settingPtr, ok = settingValuePointer(key, po.tableDesc.TableLevelSettings).(**catpb.Int); !ok {
return errors.Newf("table setting %s has unexpected type", key)
}
if settingPtr == nil {
return errors.Newf("unable to set table setting %s", key)
}
if err = validateFunc(intVal); err != nil {
return errors.Wrapf(err, "invalid value for %s", key)
}
setting := *settingPtr
if setting == nil {
*settingPtr = &catpb.Int{Value: intVal}
return nil
} else if setting.Value == intVal {
return nil
}
setting.Value = intVal
return nil
}
}
func floatTableSettingFunc(
validateFunc func(v float64) error,
) func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
return func(ctx context.Context, po *TableStorageParamObserver, semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
floatVal, err := floatFromDatum(evalCtx, key, datum)
if err != nil {
return err
}
if po.tableDesc.TableLevelSettings == nil {
po.tableDesc.TableLevelSettings = &catpb.TableLevelSettings{}
}
var settingPtr **catpb.Float
var ok bool
if settingPtr, ok = settingValuePointer(key, po.tableDesc.TableLevelSettings).(**catpb.Float); !ok {
return errors.Newf("table setting %s has unexpected type", key)
}
if settingPtr == nil {
return errors.Newf("unable to set table setting %s", key)
}
if err = validateFunc(floatVal); err != nil {
return errors.Wrapf(err, "invalid value for %s", key)
}
setting := *settingPtr
if setting == nil {
*settingPtr = &catpb.Float{Value: floatVal}
return nil
} else if setting.Value == floatVal {
return nil
}
setting.Value = floatVal
return nil
}
}
func tableSettingResetFunc(
settingDataType settingDataType,
) func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
return func(po *TableStorageParamObserver, evalCtx *tree.EvalContext, key string) error {
if po.tableDesc.TableLevelSettings == nil {
return nil
}
settingPtr := settingValuePointer(key, po.tableDesc.TableLevelSettings)
if settingPtr == nil {
return errors.Newf("unable to reset table setting %s", key)
}
var ok bool
switch settingDataType {
case boolSetting:
var setting **catpb.Bool
if setting, ok = settingPtr.(**catpb.Bool); !ok {
return errors.Newf("unable to reset table setting %s", key)
}
if *setting == nil {
// This setting is unset or has already been reset.
return nil
}
*setting = nil
case intSetting:
var setting **catpb.Int
if setting, ok = settingPtr.(**catpb.Int); !ok {
return errors.Newf("unable to reset table setting %s", key)
}
if *setting == nil {
// This setting is unset or has already been reset.
return nil
}
*setting = nil
case floatSetting:
var setting **catpb.Float
if setting, ok = settingPtr.(**catpb.Float); !ok {
return errors.Newf("unable to reset table setting %s", key)
}
if *setting == nil {
// This setting is unset or has already been reset.
return nil
}
*setting = nil
default:
return errors.Newf("unable to reset table setting %s", key)
}
return nil
}
}
// tableSettingsDict provides the switch case to use in settingValuePointer for
// finding the tableSettings element to mutate.
var tableSettingsDict = map[string]int{
`sql.stats.automatic_collection.enabled`: 1,
`sql.stats.automatic_collection.min_stale_rows`: 2,
`sql.stats.automatic_collection.fraction_stale_rows`: 3,
}
func settingValuePointer(settingName string, tableSettings *catpb.TableLevelSettings) interface{} {
if idx, ok := tableSettingsDict[settingName]; ok {
switch idx {
case 1:
return &tableSettings.SqlStatsAutomaticCollectionEnabled
case 2:
return &tableSettings.SqlStatsAutomaticCollectionMinStaleRows
case 3:
return &tableSettings.SqlStatsAutomaticCollectionFractionStaleRows
default:
return nil
}
}
return nil
}
// onSet implements the StorageParamObserver interface.
func (po *TableStorageParamObserver) onSet(
ctx context.Context,
semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext,
key string,
datum tree.Datum,
) error {
if strings.HasPrefix(key, "ttl_") && len(po.tableDesc.AllMutations()) > 0 {
return pgerror.Newf(
pgcode.FeatureNotSupported,
"cannot modify TTL settings while another schema change on the table is being processed",
)
}
if p, ok := tableParams[key]; ok {
return p.onSet(ctx, po, semaCtx, evalCtx, key, datum)
}
return pgerror.Newf(pgcode.InvalidParameterValue, "invalid storage parameter %q", key)
}
// onReset implements the StorageParamObserver interface.
func (po *TableStorageParamObserver) onReset(evalCtx *tree.EvalContext, key string) error {
if strings.HasPrefix(key, "ttl_") && len(po.tableDesc.AllMutations()) > 0 {
return pgerror.Newf(
pgcode.FeatureNotSupported,
"cannot modify TTL settings while another schema change on the table is being processed",
)
}
if p, ok := tableParams[key]; ok {
return p.onReset(po, evalCtx, key)
}
return pgerror.Newf(pgcode.InvalidParameterValue, "invalid storage parameter %q", key)
}
func setFillFactorStorageParam(evalCtx *tree.EvalContext, key string, datum tree.Datum) error {
val, err := DatumAsFloat(evalCtx, key, datum)
if err != nil {
return err
}
if val < 0 || val > 100 {
return pgerror.Newf(pgcode.InvalidParameterValue, "%q must be between 0 and 100", key)
}
if evalCtx != nil {
evalCtx.ClientNoticeSender.BufferClientNotice(
evalCtx.Context,
pgnotice.Newf("storage parameter %q is ignored", key),
)
}
return nil
}
// IndexStorageParamObserver observes storage parameters for indexes.
type IndexStorageParamObserver struct {
IndexDesc *descpb.IndexDescriptor
}
var _ StorageParamObserver = (*IndexStorageParamObserver)(nil)
func getS2ConfigFromIndex(indexDesc *descpb.IndexDescriptor) *geoindex.S2Config {
var s2Config *geoindex.S2Config
if indexDesc.GeoConfig.S2Geometry != nil {
s2Config = indexDesc.GeoConfig.S2Geometry.S2Config
}
if indexDesc.GeoConfig.S2Geography != nil {
s2Config = indexDesc.GeoConfig.S2Geography.S2Config
}
return s2Config
}
func (po *IndexStorageParamObserver) applyS2ConfigSetting(
evalCtx *tree.EvalContext, key string, expr tree.Datum, min int64, max int64,
) error {
s2Config := getS2ConfigFromIndex(po.IndexDesc)
if s2Config == nil {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"index setting %q can only be set on GEOMETRY or GEOGRAPHY spatial indexes",
key,
)
}
val, err := DatumAsInt(evalCtx, key, expr)
if err != nil {
return errors.Wrapf(err, "error decoding %q", key)
}
if val < min || val > max {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"%q value must be between %d and %d inclusive",
key,
min,
max,
)
}
switch key {
case `s2_max_level`:
s2Config.MaxLevel = int32(val)
case `s2_level_mod`:
s2Config.LevelMod = int32(val)
case `s2_max_cells`:
s2Config.MaxCells = int32(val)
}
return nil
}
func (po *IndexStorageParamObserver) applyGeometryIndexSetting(
evalCtx *tree.EvalContext, key string, expr tree.Datum,
) error {
if po.IndexDesc.GeoConfig.S2Geometry == nil {
return pgerror.Newf(pgcode.InvalidParameterValue, "%q can only be applied to GEOMETRY spatial indexes", key)
}
val, err := DatumAsFloat(evalCtx, key, expr)
if err != nil {
return errors.Wrapf(err, "error decoding %q", key)
}
switch key {
case `geometry_min_x`:
po.IndexDesc.GeoConfig.S2Geometry.MinX = val
case `geometry_max_x`:
po.IndexDesc.GeoConfig.S2Geometry.MaxX = val
case `geometry_min_y`:
po.IndexDesc.GeoConfig.S2Geometry.MinY = val
case `geometry_max_y`:
po.IndexDesc.GeoConfig.S2Geometry.MaxY = val
default:
return pgerror.Newf(pgcode.InvalidParameterValue, "unknown key: %q", key)
}
return nil
}
// onSet implements the StorageParamObserver interface.
func (po *IndexStorageParamObserver) onSet(
ctx context.Context,
semaCtx *tree.SemaContext,
evalCtx *tree.EvalContext,
key string,
expr tree.Datum,
) error {
switch key {
case `fillfactor`:
return setFillFactorStorageParam(evalCtx, key, expr)
case `s2_max_level`:
return po.applyS2ConfigSetting(evalCtx, key, expr, 0, 30)
case `s2_level_mod`:
return po.applyS2ConfigSetting(evalCtx, key, expr, 1, 3)
case `s2_max_cells`:
return po.applyS2ConfigSetting(evalCtx, key, expr, 1, 32)
case `geometry_min_x`, `geometry_max_x`, `geometry_min_y`, `geometry_max_y`:
return po.applyGeometryIndexSetting(evalCtx, key, expr)
// `bucket_count` is handled in schema changer when creating hash sharded
// indexes.
case `bucket_count`:
return nil
case `vacuum_cleanup_index_scale_factor`,
`buffering`,
`fastupdate`,
`gin_pending_list_limit`,
`pages_per_range`,
`autosummarize`:
return unimplemented.NewWithIssuef(43299, "storage parameter %q", key)
}
return pgerror.Newf(pgcode.InvalidParameterValue, "invalid storage parameter %q", key)
}
// onReset implements the StorageParameterObserver interface.
func (po *IndexStorageParamObserver) onReset(evalCtx *tree.EvalContext, key string) error {
return errors.AssertionFailedf("non-implemented codepath")
}
// runPostChecks implements the StorageParamObserver interface.
func (po *IndexStorageParamObserver) runPostChecks() error {
s2Config := getS2ConfigFromIndex(po.IndexDesc)
if s2Config != nil {
if (s2Config.MaxLevel)%s2Config.LevelMod != 0 {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"s2_max_level (%d) must be divisible by s2_level_mod (%d)",
s2Config.MaxLevel,
s2Config.LevelMod,
)
}
}
if cfg := po.IndexDesc.GeoConfig.S2Geometry; cfg != nil {
if cfg.MaxX <= cfg.MinX {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"geometry_max_x (%f) must be greater than geometry_min_x (%f)",
cfg.MaxX,
cfg.MinX,
)
}
if cfg.MaxY <= cfg.MinY {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"geometry_max_y (%f) must be greater than geometry_min_y (%f)",
cfg.MaxY,
cfg.MinY,
)
}
}
return nil
}