-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
sequence.go
997 lines (928 loc) · 31.7 KB
/
sequence.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
// 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 (
"context"
"fmt"
"math"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/colinfo"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/seqexpr"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"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/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/builtins"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlerrors"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// GetSerialSequenceNameFromColumn is part of the tree.SequenceOperators interface.
func (p *planner) GetSerialSequenceNameFromColumn(
ctx context.Context, tn *tree.TableName, columnName tree.Name,
) (*tree.TableName, error) {
flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireTableDesc)
_, tableDesc, err := resolver.ResolveExistingTableObject(ctx, p, tn, flags)
if err != nil {
return nil, err
}
for _, col := range tableDesc.PublicColumns() {
if col.ColName() == columnName {
// Seems like we have no way of detecting whether this was done using "SERIAL".
// Guess by assuming it is SERIAL it it uses only one sequence.
// NOTE: This could be alleviated by going through the process of saving SERIAL
// into the descriptor for the column, but has flow on effects for
// which have not been thought about (e.g. implication for backup and restore,
// as well as backward compatibility) so we're using this heuristic for now.
// TODO(#52487): fix this up.
if col.NumUsesSequences() == 1 {
seq, err := p.Descriptors().GetImmutableTableByID(
ctx,
p.txn,
col.GetUsesSequenceID(0),
tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc),
)
if err != nil {
return nil, err
}
return p.getQualifiedTableName(ctx, seq)
}
return nil, nil
}
}
return nil, colinfo.NewUndefinedColumnError(string(columnName))
}
// IncrementSequenceByID implements the tree.SequenceOperators interface.
func (p *planner) IncrementSequenceByID(ctx context.Context, seqID int64) (int64, error) {
if p.EvalContext().TxnReadOnly {
return 0, readOnlyError("nextval()")
}
flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc)
descriptor, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, descpb.ID(seqID), flags)
if err != nil {
return 0, err
}
if !descriptor.IsSequence() {
seqName, err := p.getQualifiedTableName(ctx, descriptor)
if err != nil {
return 0, err
}
return 0, sqlerrors.NewWrongObjectTypeError(seqName, "sequence")
}
return incrementSequenceHelper(ctx, p, descriptor)
}
// incrementSequenceHelper is shared by IncrementSequence and IncrementSequenceByID
// to increment the given sequence.
func incrementSequenceHelper(
ctx context.Context, p *planner, descriptor catalog.TableDescriptor,
) (int64, error) {
if err := p.CheckPrivilege(ctx, descriptor, privilege.UPDATE); err != nil {
return 0, err
}
seqOpts := descriptor.GetSequenceOpts()
var val int64
var err error
if seqOpts.Virtual {
rowid := builtins.GenerateUniqueInt(p.EvalContext().NodeID.SQLInstanceID())
val = int64(rowid)
} else {
val, err = p.incrementSequenceUsingCache(ctx, descriptor)
}
if err != nil {
return 0, err
}
p.sessionDataMutatorIterator.applyOnEachMutator(
func(m sessionDataMutator) {
m.RecordLatestSequenceVal(uint32(descriptor.GetID()), val)
},
)
return val, nil
}
// incrementSequenceUsingCache fetches the next value of the sequence
// represented by the passed catalog.TableDescriptor. If the sequence has a
// cache size of greater than 1, then this function will read cached values
// from the session data and repopulate these values when the cache is empty.
func (p *planner) incrementSequenceUsingCache(
ctx context.Context, descriptor catalog.TableDescriptor,
) (int64, error) {
seqOpts := descriptor.GetSequenceOpts()
cacheSize := seqOpts.EffectiveCacheSize()
fetchNextValues := func() (currentValue, incrementAmount, sizeOfCache int64, err error) {
seqValueKey := p.ExecCfg().Codec.SequenceKey(uint32(descriptor.GetID()))
// We *do not* use the planner txn here, since nextval does not respect
// transaction boundaries. This matches the specification at
// https://www.postgresql.org/docs/14/functions-sequence.html
endValue, err := kv.IncrementValRetryable(
ctx, p.ExecCfg().DB, seqValueKey, seqOpts.Increment*cacheSize)
if err != nil {
if errors.HasType(err, (*roachpb.IntegerOverflowError)(nil)) {
return 0, 0, 0, boundsExceededError(descriptor)
}
return 0, 0, 0, err
}
// This sequence has exceeded its bounds after performing this increment.
if endValue > seqOpts.MaxValue || endValue < seqOpts.MinValue {
// If the sequence exceeded its bounds prior to the increment, then return an error.
if (seqOpts.Increment > 0 && endValue-seqOpts.Increment*cacheSize >= seqOpts.MaxValue) ||
(seqOpts.Increment < 0 && endValue-seqOpts.Increment*cacheSize <= seqOpts.MinValue) {
return 0, 0, 0, boundsExceededError(descriptor)
}
// Otherwise, values between the limit and the value prior to incrementing can be cached.
limit := seqOpts.MaxValue
if seqOpts.Increment < 0 {
limit = seqOpts.MinValue
}
abs := func(i int64) int64 {
if i < 0 {
return -i
}
return i
}
currentValue = endValue - seqOpts.Increment*(cacheSize-1)
incrementAmount = seqOpts.Increment
sizeOfCache = abs(limit-(endValue-seqOpts.Increment*cacheSize)) / abs(seqOpts.Increment)
return currentValue, incrementAmount, sizeOfCache, nil
}
return endValue - seqOpts.Increment*(cacheSize-1), seqOpts.Increment, cacheSize, nil
}
var val int64
var err error
if cacheSize == 1 {
val, _, _, err = fetchNextValues()
if err != nil {
return 0, err
}
} else {
val, err = p.GetOrInitSequenceCache().NextValue(uint32(descriptor.GetID()), uint32(descriptor.GetVersion()), fetchNextValues)
if err != nil {
return 0, err
}
}
return val, nil
}
func boundsExceededError(descriptor catalog.TableDescriptor) error {
seqOpts := descriptor.GetSequenceOpts()
isAscending := seqOpts.Increment > 0
var word string
var value int64
if isAscending {
word = "maximum"
value = seqOpts.MaxValue
} else {
word = "minimum"
value = seqOpts.MinValue
}
name := descriptor.GetName()
return pgerror.Newf(
pgcode.SequenceGeneratorLimitExceeded,
`reached %s value of sequence %q (%d)`, word,
tree.ErrString((*tree.Name)(&name)), value)
}
// GetLatestValueInSessionForSequenceByID implements the tree.SequenceOperators interface.
func (p *planner) GetLatestValueInSessionForSequenceByID(
ctx context.Context, seqID int64,
) (int64, error) {
flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc)
descriptor, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, descpb.ID(seqID), flags)
if err != nil {
return 0, err
}
seqName, err := p.getQualifiedTableName(ctx, descriptor)
if err != nil {
return 0, err
}
if !descriptor.IsSequence() {
return 0, sqlerrors.NewWrongObjectTypeError(seqName, "sequence")
}
return getLatestValueInSessionForSequenceHelper(p, descriptor, seqName)
}
// getLatestValueInSessionForSequenceHelper is shared by
// GetLatestValueInSessionForSequence and GetLatestValueInSessionForSequenceByID
// to get the latest value for the given sequence.
func getLatestValueInSessionForSequenceHelper(
p *planner, descriptor catalog.TableDescriptor, seqName *tree.TableName,
) (int64, error) {
val, ok := p.SessionData().SequenceState.GetLastValueByID(uint32(descriptor.GetID()))
if !ok {
return 0, pgerror.Newf(
pgcode.ObjectNotInPrerequisiteState,
`currval of sequence %q is not yet defined in this session`, tree.ErrString(seqName))
}
return val, nil
}
// SetSequenceValueByID implements the tree.SequenceOperators interface.
func (p *planner) SetSequenceValueByID(
ctx context.Context, seqID uint32, newVal int64, isCalled bool,
) error {
if p.EvalContext().TxnReadOnly {
return readOnlyError("setval()")
}
flags := tree.ObjectLookupFlagsWithRequiredTableKind(tree.ResolveRequireSequenceDesc)
descriptor, err := p.Descriptors().GetImmutableTableByID(ctx, p.txn, descpb.ID(seqID), flags)
if err != nil {
return err
}
seqName, err := p.getQualifiedTableName(ctx, descriptor)
if err != nil {
return err
}
if !descriptor.IsSequence() {
return sqlerrors.NewWrongObjectTypeError(seqName, "sequence")
}
if err := setSequenceValueHelper(ctx, p, descriptor, newVal, isCalled, seqName); err != nil {
return err
}
// Clear out the cache and update the last value if needed.
p.sessionDataMutatorIterator.applyOnEachMutator(func(m sessionDataMutator) {
m.initSequenceCache()
if isCalled {
m.RecordLatestSequenceVal(seqID, newVal)
}
})
return nil
}
// setSequenceValueHelper is shared by SetSequenceValue and SetSequenceValueByID
// to set the given sequence to a new given value.
func setSequenceValueHelper(
ctx context.Context,
p *planner,
descriptor catalog.TableDescriptor,
newVal int64,
isCalled bool,
seqName *tree.TableName,
) error {
if err := p.CheckPrivilege(ctx, descriptor, privilege.UPDATE); err != nil {
return err
}
if descriptor.GetSequenceOpts().Virtual {
// TODO(knz): we currently return an error here, but if/when
// CockroachDB grows to automatically make sequences virtual when
// clients don't expect it, we may need to make this a no-op
// instead.
return pgerror.Newf(
pgcode.ObjectNotInPrerequisiteState,
`cannot set the value of virtual sequence %q`, tree.ErrString(seqName))
}
seqValueKey, newVal, err := MakeSequenceKeyVal(p.ExecCfg().Codec, descriptor, newVal, isCalled)
if err != nil {
return err
}
// We *do not* use the planner txn here, since setval does not respect
// transaction boundaries. This matches the specification at
// https://www.postgresql.org/docs/14/functions-sequence.html
// TODO(vilterp): not supposed to mix usage of Inc and Put on a key,
// according to comments on Inc operation. Switch to Inc if `desired-current`
// overflows correctly.
return p.ExecCfg().DB.Put(ctx, seqValueKey, newVal)
}
// MakeSequenceKeyVal returns the key and value of a sequence being set
// with newVal.
func MakeSequenceKeyVal(
codec keys.SQLCodec, sequence catalog.TableDescriptor, newVal int64, isCalled bool,
) ([]byte, int64, error) {
opts := sequence.GetSequenceOpts()
if newVal > opts.MaxValue || newVal < opts.MinValue {
return nil, 0, pgerror.Newf(
pgcode.NumericValueOutOfRange,
`value %d is out of bounds for sequence "%s" (%d..%d)`,
newVal, sequence.GetName(), opts.MinValue, opts.MaxValue,
)
}
if !isCalled {
newVal = newVal - opts.Increment
}
seqValueKey := codec.SequenceKey(uint32(sequence.GetID()))
return seqValueKey, newVal, nil
}
// GetSequenceValue returns the current value of the sequence.
func (p *planner) GetSequenceValue(
ctx context.Context, codec keys.SQLCodec, desc catalog.TableDescriptor,
) (int64, error) {
if desc.GetSequenceOpts() == nil {
return 0, errors.New("descriptor is not a sequence")
}
keyValue, err := p.txn.Get(ctx, codec.SequenceKey(uint32(desc.GetID())))
if err != nil {
return 0, err
}
return keyValue.ValueInt(), nil
}
func readOnlyError(s string) error {
return pgerror.Newf(pgcode.ReadOnlySQLTransaction,
"cannot execute %s in a read-only transaction", s)
}
func getSequenceIntegerBounds(
integerType *types.T,
) (lowerIntBound int64, upperIntBound int64, err error) {
switch integerType {
case types.Int2:
return math.MinInt16, math.MaxInt16, nil
case types.Int4:
return math.MinInt32, math.MaxInt32, nil
case types.Int:
return math.MinInt64, math.MaxInt64, nil
}
return 0, 0, pgerror.Newf(
pgcode.InvalidParameterValue,
"CREATE SEQUENCE option AS received type %s, must be integer",
integerType,
)
}
func setSequenceIntegerBounds(
opts *descpb.TableDescriptor_SequenceOpts,
integerType *types.T,
isAscending bool,
setMinValue bool,
setMaxValue bool,
) error {
var minValue int64 = math.MinInt64
var maxValue int64 = math.MaxInt64
if isAscending {
minValue = 1
switch integerType {
case types.Int2:
maxValue = math.MaxInt16
case types.Int4:
maxValue = math.MaxInt32
case types.Int:
// Do nothing, it's the default.
default:
return pgerror.Newf(
pgcode.InvalidParameterValue,
"CREATE SEQUENCE option AS received type %s, must be integer",
integerType,
)
}
} else {
maxValue = -1
switch integerType {
case types.Int2:
minValue = math.MinInt16
case types.Int4:
minValue = math.MinInt32
case types.Int:
// Do nothing, it's the default.
default:
return pgerror.Newf(
pgcode.InvalidParameterValue,
"CREATE SEQUENCE option AS received type %s, must be integer",
integerType,
)
}
}
if setMinValue {
opts.MinValue = minValue
}
if setMaxValue {
opts.MaxValue = maxValue
}
return nil
}
// assignSequenceOptions moves options from the AST node to the sequence options descriptor,
// starting with defaults and overriding them with user-provided options.
func assignSequenceOptions(
opts *descpb.TableDescriptor_SequenceOpts,
optsNode tree.SequenceOptions,
setDefaults bool,
params *runParams,
sequenceID descpb.ID,
sequenceParentID descpb.ID,
existingType *types.T,
) error {
wasAscending := opts.Increment > 0
// Set the default integer type of a sequence.
var integerType = types.Int
// All other defaults are dependent on the value of increment
// and the AS integerType. (i.e. whether the sequence is ascending
// or descending, bigint vs. smallint)
for _, option := range optsNode {
if option.Name == tree.SeqOptIncrement {
opts.Increment = *option.IntVal
} else if option.Name == tree.SeqOptAs {
integerType = option.AsIntegerType
opts.AsIntegerType = integerType.SQLString()
}
}
if opts.Increment == 0 {
return pgerror.New(
pgcode.InvalidParameterValue, "INCREMENT must not be zero")
}
isAscending := opts.Increment > 0
// Set increment-dependent defaults.
if setDefaults {
if isAscending {
opts.MinValue = 1
opts.MaxValue = math.MaxInt64
opts.Start = opts.MinValue
} else {
opts.MinValue = math.MinInt64
opts.MaxValue = -1
opts.Start = opts.MaxValue
}
// No Caching
opts.CacheSize = 1
}
lowerIntBound, upperIntBound, err := getSequenceIntegerBounds(integerType)
if err != nil {
return err
}
// Set default MINVALUE and MAXVALUE if AS option value for integer type is specified.
if opts.AsIntegerType != "" {
// We change MINVALUE and MAXVALUE if it is the originally set to the default during ALTER.
setMinValue := setDefaults
setMaxValue := setDefaults
if !setDefaults && existingType != nil {
existingLowerIntBound, existingUpperIntBound, err := getSequenceIntegerBounds(existingType)
if err != nil {
return err
}
if (wasAscending && opts.MinValue == 1) || (!wasAscending && opts.MinValue == existingLowerIntBound) {
setMinValue = true
}
if (wasAscending && opts.MaxValue == existingUpperIntBound) || (!wasAscending && opts.MaxValue == -1) {
setMaxValue = true
}
}
if err := setSequenceIntegerBounds(
opts,
integerType,
isAscending,
setMinValue,
setMaxValue,
); err != nil {
return err
}
}
// Fill in all other options.
optionsSeen := map[string]bool{}
for _, option := range optsNode {
// Error on duplicate options.
_, seenBefore := optionsSeen[option.Name]
if seenBefore {
return pgerror.New(pgcode.Syntax, "conflicting or redundant options")
}
optionsSeen[option.Name] = true
switch option.Name {
case tree.SeqOptCycle:
return unimplemented.NewWithIssue(20961,
"CYCLE option is not supported")
case tree.SeqOptNoCycle:
// Do nothing; this is the default.
case tree.SeqOptCache:
if v := *option.IntVal; v >= 1 {
opts.CacheSize = v
} else {
return pgerror.Newf(pgcode.InvalidParameterValue,
"CACHE (%d) must be greater than zero", v)
}
case tree.SeqOptIncrement:
// Do nothing; this has already been set.
case tree.SeqOptMinValue:
// A value of nil represents the user explicitly saying `NO MINVALUE`.
if option.IntVal != nil {
opts.MinValue = *option.IntVal
}
case tree.SeqOptMaxValue:
// A value of nil represents the user explicitly saying `NO MAXVALUE`.
if option.IntVal != nil {
opts.MaxValue = *option.IntVal
}
case tree.SeqOptStart:
opts.Start = *option.IntVal
case tree.SeqOptVirtual:
opts.Virtual = true
case tree.SeqOptOwnedBy:
if params == nil {
return pgerror.Newf(pgcode.Internal,
"Trying to add/remove Sequence Owner without access to context")
}
// The owner is being removed
if option.ColumnItemVal == nil {
if err := removeSequenceOwnerIfExists(params.ctx, params.p, sequenceID, opts); err != nil {
return err
}
} else {
// The owner is being added/modified
tableDesc, col, err := resolveColumnItemToDescriptors(
params.ctx, params.p, option.ColumnItemVal,
)
if err != nil {
return err
}
if tableDesc.ParentID != sequenceParentID &&
!allowCrossDatabaseSeqOwner.Get(¶ms.p.execCfg.Settings.SV) {
return errors.WithHintf(
pgerror.Newf(pgcode.FeatureNotSupported,
"OWNED BY cannot refer to other databases; (see the '%s' cluster setting)",
allowCrossDatabaseSeqOwnerSetting),
crossDBReferenceDeprecationHint(),
)
}
// We only want to trigger schema changes if the owner is not what we
// want it to be.
if opts.SequenceOwner.OwnerTableID != tableDesc.ID ||
opts.SequenceOwner.OwnerColumnID != col.GetID() {
if err := removeSequenceOwnerIfExists(params.ctx, params.p, sequenceID, opts); err != nil {
return err
}
err := addSequenceOwner(params.ctx, params.p, option.ColumnItemVal, sequenceID, opts)
if err != nil {
return err
}
}
}
}
}
if setDefaults || (wasAscending && opts.Start == 1) || (!wasAscending && opts.Start == -1) {
// If start option not specified, set it to MinValue (for ascending sequences)
// or MaxValue (for descending sequences).
// We only do this if we're setting it for the first time, or the sequence was
// ALTERed with the default original values.
if _, startSeen := optionsSeen[tree.SeqOptStart]; !startSeen {
if opts.Increment > 0 {
opts.Start = opts.MinValue
} else {
opts.Start = opts.MaxValue
}
}
}
if opts.MinValue < lowerIntBound {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"MINVALUE (%d) must be greater than (%d) for type %s",
opts.MinValue,
lowerIntBound,
integerType.SQLString(),
)
}
if opts.MaxValue < lowerIntBound {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"MAXVALUE (%d) must be greater than (%d) for type %s",
opts.MaxValue,
lowerIntBound,
integerType.SQLString(),
)
}
if opts.MinValue > upperIntBound {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"MINVALUE (%d) must be less than (%d) for type %s",
opts.MinValue,
upperIntBound,
integerType.SQLString(),
)
}
if opts.MaxValue > upperIntBound {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"MAXVALUE (%d) must be less than (%d) for type %s",
opts.MaxValue,
upperIntBound,
integerType.SQLString(),
)
}
if opts.Start > opts.MaxValue {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"START value (%d) cannot be greater than MAXVALUE (%d)",
opts.Start,
opts.MaxValue,
)
}
if opts.Start < opts.MinValue {
return pgerror.Newf(
pgcode.InvalidParameterValue,
"START value (%d) cannot be less than MINVALUE (%d)",
opts.Start,
opts.MinValue,
)
}
return nil
}
func removeSequenceOwnerIfExists(
ctx context.Context, p *planner, sequenceID descpb.ID, opts *descpb.TableDescriptor_SequenceOpts,
) error {
if !opts.HasOwner() {
return nil
}
tableDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, opts.SequenceOwner.OwnerTableID, p.txn)
if err != nil {
// Special case error swallowing for #50711 and #50781, which can cause a
// column to own sequences that have been dropped/do not exist.
if errors.Is(err, catalog.ErrDescriptorDropped) ||
pgerror.GetPGCode(err) == pgcode.UndefinedTable {
log.Eventf(ctx, "swallowing error during sequence ownership unlinking: %s", err.Error())
return nil
}
return err
}
// If the table descriptor has already been dropped, there is no need to
// remove the reference.
if tableDesc.Dropped() {
return nil
}
col, err := tableDesc.FindColumnWithID(opts.SequenceOwner.OwnerColumnID)
if err != nil {
return err
}
// Find an item in colDesc.OwnsSequenceIds which references SequenceID.
newOwnsSequenceIDs := make([]descpb.ID, 0, col.NumOwnsSequences())
for i := 0; i < col.NumOwnsSequences(); i++ {
id := col.GetOwnsSequenceID(i)
if id != sequenceID {
newOwnsSequenceIDs = append(newOwnsSequenceIDs, id)
}
}
if len(newOwnsSequenceIDs) == col.NumOwnsSequences() {
return errors.AssertionFailedf("couldn't find reference from column to this sequence")
}
col.ColumnDesc().OwnsSequenceIds = newOwnsSequenceIDs
if err := p.writeSchemaChange(
ctx, tableDesc, descpb.InvalidMutationID,
fmt.Sprintf("removing sequence owner %s(%d) for sequence %d",
tableDesc.Name, tableDesc.ID, sequenceID,
),
); err != nil {
return err
}
// Reset the SequenceOwner to empty
opts.SequenceOwner.Reset()
return nil
}
func resolveColumnItemToDescriptors(
ctx context.Context, p *planner, columnItem *tree.ColumnItem,
) (*tabledesc.Mutable, catalog.Column, error) {
if columnItem.TableName == nil {
err := pgerror.New(pgcode.Syntax, "invalid OWNED BY option")
return nil, nil, errors.WithHint(err, "Specify OWNED BY table.column or OWNED BY NONE.")
}
tableName := columnItem.TableName.ToTableName()
_, tableDesc, err := p.ResolveMutableTableDescriptor(ctx, &tableName, true /* required */, tree.ResolveRequireTableDesc)
if err != nil {
return nil, nil, err
}
col, err := tableDesc.FindColumnWithName(columnItem.ColumnName)
if err != nil {
return nil, nil, err
}
return tableDesc, col, nil
}
func addSequenceOwner(
ctx context.Context,
p *planner,
columnItemVal *tree.ColumnItem,
sequenceID descpb.ID,
opts *descpb.TableDescriptor_SequenceOpts,
) error {
tableDesc, col, err := resolveColumnItemToDescriptors(ctx, p, columnItemVal)
if err != nil {
return err
}
col.ColumnDesc().OwnsSequenceIds = append(col.ColumnDesc().OwnsSequenceIds, sequenceID)
opts.SequenceOwner.OwnerColumnID = col.GetID()
opts.SequenceOwner.OwnerTableID = tableDesc.GetID()
return p.writeSchemaChange(
ctx, tableDesc, descpb.InvalidMutationID, fmt.Sprintf(
"adding sequence owner %s(%d) for sequence %d",
tableDesc.Name, tableDesc.ID, sequenceID),
)
}
// maybeAddSequenceDependencies adds references between the column and sequence descriptors,
// if the column has a DEFAULT expression that uses one or more sequences. (Usually just one,
// e.g. `DEFAULT nextval('my_sequence')`.
// The passed-in column descriptor is mutated, and the modified sequence descriptors are returned.
func maybeAddSequenceDependencies(
ctx context.Context,
st *cluster.Settings,
sc resolver.SchemaResolver,
tableDesc *tabledesc.Mutable,
col *descpb.ColumnDescriptor,
expr tree.TypedExpr,
backrefs map[descpb.ID]*tabledesc.Mutable,
) ([]*tabledesc.Mutable, error) {
seqIdentifiers, err := seqexpr.GetUsedSequences(expr)
if err != nil {
return nil, err
}
var seqDescs []*tabledesc.Mutable
seqNameToID := make(map[string]int64)
for _, seqIdentifier := range seqIdentifiers {
seqDesc, err := GetSequenceDescFromIdentifier(ctx, sc, seqIdentifier)
if err != nil {
return nil, err
}
// Check if this reference is cross DB.
if seqDesc.GetParentID() != tableDesc.GetParentID() &&
!allowCrossDatabaseSeqReferences.Get(&st.SV) {
return nil, errors.WithHintf(
pgerror.Newf(pgcode.FeatureNotSupported,
"sequence references cannot come from other databases; (see the '%s' cluster setting)",
allowCrossDatabaseSeqReferencesSetting),
crossDBReferenceDeprecationHint(),
)
}
seqNameToID[seqIdentifier.SeqName] = int64(seqDesc.ID)
// If we had already modified this Sequence as part of this transaction,
// we only want to modify a single instance of it instead of overwriting it.
// So replace seqDesc with the descriptor that was previously modified.
if prev, ok := backrefs[seqDesc.ID]; ok {
seqDesc = prev
}
col.UsesSequenceIds = append(col.UsesSequenceIds, seqDesc.ID)
// Add reference from sequence descriptor to column.
refIdx := -1
for i, reference := range seqDesc.DependedOnBy {
if reference.ID == tableDesc.ID {
refIdx = i
}
}
if refIdx == -1 {
seqDesc.DependedOnBy = append(seqDesc.DependedOnBy, descpb.TableDescriptor_Reference{
ID: tableDesc.ID,
ColumnIDs: []descpb.ColumnID{col.ID},
ByID: true,
})
} else {
seqDesc.DependedOnBy[refIdx].ColumnIDs = append(seqDesc.DependedOnBy[refIdx].ColumnIDs, col.ID)
}
seqDescs = append(seqDescs, seqDesc)
}
// If sequences are present in the expr (and the cluster is the right version),
// walk the expr tree and replace any sequences names with their IDs.
if len(seqIdentifiers) > 0 {
newExpr, err := seqexpr.ReplaceSequenceNamesWithIDs(expr, seqNameToID)
if err != nil {
return nil, err
}
s := tree.Serialize(newExpr)
col.DefaultExpr = &s
}
return seqDescs, nil
}
// GetSequenceDescFromIdentifier resolves the sequence descriptor for the given
// sequence identifier.
func GetSequenceDescFromIdentifier(
ctx context.Context, sc resolver.SchemaResolver, seqIdentifier seqexpr.SeqIdentifier,
) (*tabledesc.Mutable, error) {
var tn tree.TableName
if seqIdentifier.IsByID() {
name, err := sc.GetQualifiedTableNameByID(ctx, seqIdentifier.SeqID, tree.ResolveRequireSequenceDesc)
if err != nil {
return nil, err
}
tn = *name
} else {
parsedSeqName, err := parser.ParseTableName(seqIdentifier.SeqName)
if err != nil {
return nil, err
}
tn = parsedSeqName.ToTableName()
}
var seqDesc *tabledesc.Mutable
var err error
p, ok := sc.(*planner)
if ok {
_, seqDesc, err = p.ResolveMutableTableDescriptor(ctx, &tn, true /*required*/, tree.ResolveRequireSequenceDesc)
if err != nil {
return nil, err
}
} else {
// This is only executed via IMPORT which uses its own resolver.
_, seqDesc, err = resolver.ResolveMutableExistingTableObject(ctx, sc, &tn, true /*required*/, tree.ResolveRequireSequenceDesc)
if err != nil {
return nil, err
}
}
return seqDesc, nil
}
// dropSequencesOwnedByCol drops all the sequences from col.OwnsSequenceIDs.
// Called when the respective column (or the whole table) is being dropped.
func (p *planner) dropSequencesOwnedByCol(
ctx context.Context, col catalog.Column, queueJob bool, behavior tree.DropBehavior,
) error {
// Copy out the sequence IDs as the code to drop the sequence will reach
// back around and update the descriptor from underneath us.
colOwnsSequenceIDs := make([]descpb.ID, col.NumOwnsSequences())
for i := 0; i < col.NumOwnsSequences(); i++ {
colOwnsSequenceIDs[i] = col.GetOwnsSequenceID(i)
}
for _, sequenceID := range colOwnsSequenceIDs {
seqDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, sequenceID, p.txn)
// Special case error swallowing for #50781, which can cause a
// column to own sequences that do not exist.
if err != nil {
if errors.Is(err, catalog.ErrDescriptorDropped) ||
pgerror.GetPGCode(err) == pgcode.UndefinedTable {
log.Eventf(ctx, "swallowing error dropping owned sequences: %s", err.Error())
continue
}
return err
}
// This sequence is already getting dropped. Don't do it twice.
if seqDesc.Dropped() {
continue
}
jobDesc := fmt.Sprintf("removing sequence %q dependent on column %q which is being dropped",
seqDesc.Name, col.ColName())
// Note that this call will end up resolving and modifying the table
// descriptor.
if err := p.dropSequenceImpl(
ctx, seqDesc, queueJob, jobDesc, behavior,
); err != nil {
return err
}
}
return nil
}
// removeSequenceDependencies:
// - removes the reference from the column descriptor to the sequence descriptor.
// - removes the reference from the sequence descriptor to the column descriptor.
// - writes the sequence descriptor and notifies a schema change.
// The column descriptor is mutated but not saved to persistent storage; the caller must save it.
func (p *planner) removeSequenceDependencies(
ctx context.Context, tableDesc *tabledesc.Mutable, col catalog.Column,
) error {
for i := 0; i < col.NumUsesSequences(); i++ {
sequenceID := col.GetUsesSequenceID(i)
// Get the sequence descriptor so we can remove the reference from it.
seqDesc, err := p.Descriptors().GetMutableTableVersionByID(ctx, sequenceID, p.txn)
if err != nil {
return err
}
// If the sequence descriptor has been dropped, we do not need to unlink the
// dependency. This can happen during a `DROP DATABASE CASCADE` when both
// the table and sequence are objects in the database being dropped. If
// `dropImpl` is called on the sequence before the table, because CRDB
// doesn't implement CASCADE for sequences, the dependency to the
// table descriptor is not unlinked. This check prevents us from failing
// when trying to unlink a dependency that really shouldn't have existed
// at this point in the code to begin with.
if seqDesc.Dropped() {
continue
}
// Find the item in seqDesc.DependedOnBy which references tableDesc and col.
refTableIdx := -1
refColIdx := -1
found:
for i, reference := range seqDesc.DependedOnBy {
if reference.ID == tableDesc.ID {
refTableIdx = i
for j, colRefID := range seqDesc.DependedOnBy[i].ColumnIDs {
if colRefID == col.GetID() {
refColIdx = j
break found
}
// Before #40852, columnIDs stored in the SeqDesc were 0 as they hadn't
// been allocated then. The 0 check prevents older descs from breaking.
// Do not break though, as we still want to search in case the actual ID
// exists.
if colRefID == 0 {
refColIdx = j
}
}
}
}
if refColIdx == -1 {
return errors.AssertionFailedf("couldn't find reference from sequence to this column")
}
// Remove the column ID from the sequence descriptors list of things that
// depend on it. If the column was the only column that depended on the
// sequence, remove the table reference from the sequence as well.
seqDesc.DependedOnBy[refTableIdx].ColumnIDs = append(
seqDesc.DependedOnBy[refTableIdx].ColumnIDs[:refColIdx],
seqDesc.DependedOnBy[refTableIdx].ColumnIDs[refColIdx+1:]...)
if len(seqDesc.DependedOnBy[refTableIdx].ColumnIDs) == 0 {
seqDesc.DependedOnBy = append(
seqDesc.DependedOnBy[:refTableIdx],
seqDesc.DependedOnBy[refTableIdx+1:]...)
}
jobDesc := fmt.Sprintf("removing sequence %q dependent on column %q which is being dropped",
seqDesc.Name, col.ColName())
if err := p.writeSchemaChange(
ctx, seqDesc, descpb.InvalidMutationID, jobDesc,
); err != nil {
return err
}
}
// Remove the reference from the column descriptor to the sequence descriptor.
col.ColumnDesc().UsesSequenceIds = []descpb.ID{}
return nil
}