@@ -77,7 +77,7 @@ func (n *alterSequenceNode) startExec(params runParams) error {
77
77
}
78
78
79
79
// alterSequenceImpl applies given tree.SequenceOptions to the specified sequence descriptor.
80
- // Exisiting sequence options are not overridden with default values.
80
+ // Existing sequence options are not overridden with default values.
81
81
func alterSequenceImpl (
82
82
params runParams ,
83
83
seqDesc * tabledesc.Mutable ,
@@ -86,6 +86,8 @@ func alterSequenceImpl(
86
86
) error {
87
87
oldMinValue := seqDesc .SequenceOpts .MinValue
88
88
oldMaxValue := seqDesc .SequenceOpts .MaxValue
89
+ oldIncrement := seqDesc .SequenceOpts .Increment
90
+ oldStart := seqDesc .SequenceOpts .Start
89
91
90
92
existingType := types .Int
91
93
if seqDesc .GetSequenceOpts ().AsIntegerType != "" {
@@ -120,45 +122,80 @@ func alterSequenceImpl(
120
122
if err != nil {
121
123
return 0 , err
122
124
}
125
+
123
126
return kv .ValueInt (), nil
124
127
}
125
128
126
- // Due to the semantics of sequence caching (see sql.planner.incrementSequenceUsingCache()),
127
- // it is possible for a sequence to have a value that exceeds its MinValue or MaxValue. Users
128
- // do no see values extending the sequence's bounds, and instead see "bounds exceeded" errors.
129
- // To make a usable again after exceeding its bounds, there are two options:
129
+ setSequenceVal := func (val int64 ) error {
130
+ err := params .p .txn .Put (params .ctx , seqValueKey , val )
131
+ if err != nil {
132
+ return err
133
+ }
134
+
135
+ return nil
136
+ }
137
+
138
+ // Due to the semantics of sequence caching (see sql.planner.incrementSequenceUsingCache()), it
139
+ // is possible for a sequence to have a value that exceeds its MinValue or MaxValue. Users do
140
+ // not see values beyond the sequence's bounds, and instead see "bounds exceeded" errors. To
141
+ // make a sequence usable again after exceeding its bounds, there are two options:
142
+ //
130
143
// 1. The user changes the sequence's value by calling setval(...)
131
- // 2. The user performs a schema change to alter the sequences MinValue or MaxValue. In this case, the
132
- // value of the sequence must be restored to the original MinValue or MaxValue transactionally.
144
+ //
145
+ // 2. The user performs a schema change to alter the sequence's MinValue, MaxValue, or
146
+ // Increment. In this case, the value of the sequence must be (transactionally) restored to a
147
+ // value within MinValue and MaxValue.
148
+ //
133
149
// The code below handles the second case.
134
150
135
- // The sequence is decreasing and the minvalue is being decreased.
136
- if opts . Increment < 0 && seqDesc . SequenceOpts . MinValue < oldMinValue {
151
+ if opts . Increment < 0 && ( oldIncrement != seqDesc . SequenceOpts . Increment || oldMinValue != seqDesc . SequenceOpts . MinValue ) {
152
+ // Only get the sequence value from KV if it's needed.
137
153
sequenceVal , err := getSequenceValue ()
138
154
if err != nil {
139
155
return err
140
156
}
141
157
142
- // If the sequence exceeded the old MinValue, it must be changed to start at the old MinValue.
158
+ // If the sequence were never advanced, its current value is offset by the increment.
159
+ if sequenceVal > oldStart {
160
+ err := setSequenceVal (oldStart - seqDesc .SequenceOpts .Increment )
161
+ if err != nil {
162
+ return err
163
+ }
164
+ }
165
+
166
+ // If the sequence were exhausted, it would be beyond its previous bounds.
143
167
if sequenceVal < oldMinValue {
144
- err := params .p .txn .Put (params .ctx , seqValueKey , oldMinValue )
168
+ // Every call to nextval increments the sequence even if the
169
+ // sequence is exhausted. Find the final valid value of the sequence
170
+ // by calculating the number of valid calls to it
171
+ calls := (oldMinValue - oldStart ) / oldIncrement
172
+ err := setSequenceVal (oldStart + calls * oldIncrement )
145
173
if err != nil {
146
174
return err
147
175
}
148
176
}
149
- } else if opts .Increment > 0 && seqDesc .SequenceOpts .MaxValue > oldMaxValue {
177
+ } else if opts .Increment > 0 && ( oldIncrement != seqDesc .SequenceOpts .Increment || seqDesc . SequenceOpts . MaxValue > oldMaxValue ) {
150
178
sequenceVal , err := getSequenceValue ()
151
179
if err != nil {
152
180
return err
153
181
}
154
182
183
+ if sequenceVal < oldStart {
184
+ err := setSequenceVal (oldStart - seqDesc .SequenceOpts .Increment )
185
+ if err != nil {
186
+ return err
187
+ }
188
+ }
189
+
155
190
if sequenceVal > oldMaxValue {
156
- err := params .p .txn .Put (params .ctx , seqValueKey , oldMaxValue )
191
+ calls := (oldMaxValue - oldStart ) / oldIncrement
192
+ err := setSequenceVal (oldStart + calls * oldIncrement )
157
193
if err != nil {
158
194
return err
159
195
}
160
196
}
161
197
}
198
+
162
199
var restartVal * int64
163
200
for _ , option := range seqOptions {
164
201
if option .Name == tree .SeqOptRestart {
0 commit comments