@@ -77,7 +77,7 @@ func (n *alterSequenceNode) startExec(params runParams) error {
7777}
7878
7979// 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.
8181func alterSequenceImpl (
8282 params runParams ,
8383 seqDesc * tabledesc.Mutable ,
@@ -86,6 +86,8 @@ func alterSequenceImpl(
8686) error {
8787 oldMinValue := seqDesc .SequenceOpts .MinValue
8888 oldMaxValue := seqDesc .SequenceOpts .MaxValue
89+ oldIncrement := seqDesc .SequenceOpts .Increment
90+ oldStart := seqDesc .SequenceOpts .Start
8991
9092 existingType := types .Int
9193 if seqDesc .GetSequenceOpts ().AsIntegerType != "" {
@@ -120,45 +122,80 @@ func alterSequenceImpl(
120122 if err != nil {
121123 return 0 , err
122124 }
125+
123126 return kv .ValueInt (), nil
124127 }
125128
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+ //
130143 // 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+ //
133149 // The code below handles the second case.
134150
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.
137153 sequenceVal , err := getSequenceValue ()
138154 if err != nil {
139155 return err
140156 }
141157
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.
143167 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 )
145173 if err != nil {
146174 return err
147175 }
148176 }
149- } else if opts .Increment > 0 && seqDesc .SequenceOpts .MaxValue > oldMaxValue {
177+ } else if opts .Increment > 0 && ( oldIncrement != seqDesc .SequenceOpts .Increment || seqDesc . SequenceOpts . MaxValue > oldMaxValue ) {
150178 sequenceVal , err := getSequenceValue ()
151179 if err != nil {
152180 return err
153181 }
154182
183+ if sequenceVal < oldStart {
184+ err := setSequenceVal (oldStart - seqDesc .SequenceOpts .Increment )
185+ if err != nil {
186+ return err
187+ }
188+ }
189+
155190 if sequenceVal > oldMaxValue {
156- err := params .p .txn .Put (params .ctx , seqValueKey , oldMaxValue )
191+ calls := (oldMaxValue - oldStart ) / oldIncrement
192+ err := setSequenceVal (oldStart + calls * oldIncrement )
157193 if err != nil {
158194 return err
159195 }
160196 }
161197 }
198+
162199 var restartVal * int64
163200 for _ , option := range seqOptions {
164201 if option .Name == tree .SeqOptRestart {
0 commit comments