Skip to content

Commit 47e7cde

Browse files
committed
sql: use new increment when restarting sequence
Previously, if a sequence were restarted and its increment updated in the same transaction (command), the old increment would be used to set the value in KV. This change corrects the `ALTER` DDL to use the new increment value. Informs: #142914 Informs: #21564 Epic: CRDB-31283 Release note (sql change): Restarting a sequence with an updated increment has the expected initial value.
1 parent eb2dbf6 commit 47e7cde

File tree

3 files changed

+11
-10
lines changed

3 files changed

+11
-10
lines changed

pkg/sql/alter_sequence.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func alterSequenceImpl(
148148
//
149149
// The code below handles the second case.
150150

151-
if opts.Increment < 0 && (oldIncrement != seqDesc.SequenceOpts.Increment || oldMinValue != seqDesc.SequenceOpts.MinValue) {
151+
if opts.Increment < 0 && (oldIncrement != seqDesc.SequenceOpts.Increment || seqDesc.SequenceOpts.MinValue < oldMinValue) {
152152
// Only get the sequence value from KV if it's needed.
153153
sequenceVal, err := getSequenceValue()
154154
if err != nil {
@@ -209,13 +209,13 @@ func alterSequenceImpl(
209209
}
210210
}
211211
if restartVal != nil {
212-
// Using RESTART on a sequence should always cause the operation to run
213-
// in the current transaction. This is achieved by treating the sequence
214-
// as if it were just created.
215-
if err := params.p.createdSequences.addCreatedSequence(seqDesc.ID); err != nil {
216-
return err
212+
var err error
213+
if opts.Increment != 0 {
214+
err = setSequenceVal(*restartVal - opts.Increment)
215+
} else {
216+
err = setSequenceVal(*restartVal - oldIncrement)
217217
}
218-
if err := params.p.SetSequenceValueByID(params.ctx, uint32(seqDesc.ID), *restartVal, false); err != nil {
218+
if err != nil {
219219
return err
220220
}
221221
}

pkg/sql/alter_table.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
gojson "encoding/json"
1212
"fmt"
1313
"sort"
14+
"strings"
1415
"time"
1516

1617
"github.com/cockroachdb/cockroach/pkg/build"
@@ -1267,7 +1268,7 @@ func applyColumnMutation(
12671268
return err
12681269
}
12691270

1270-
// Alter referenced sequence for identity with sepcified option.
1271+
// Alter referenced sequence for identity with specified option.
12711272
// Does not override existing values if not specified.
12721273
if err := alterSequenceImpl(params, seqDesc, t.SeqOptions, t); err != nil {
12731274
return err
@@ -1288,7 +1289,7 @@ func applyColumnMutation(
12881289
if opts.Virtual {
12891290
optsNode = append(optsNode, tree.SequenceOption{Name: tree.SeqOptVirtual})
12901291
}
1291-
s := tree.Serialize(&optsNode)
1292+
s := strings.TrimSpace(tree.Serialize(&optsNode))
12921293
col.ColumnDesc().GeneratedAsIdentitySequenceOption = &s
12931294

12941295
case *tree.AlterTableDropIdentity:

pkg/sql/logictest/testdata/logic_test/alter_table

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4415,8 +4415,8 @@ SELECT b from t_alter_identity ORDER BY b;
44154415
1
44164416
5
44174417
9
4418-
18
44194418
20
4419+
22
44204420

44214421
statement ok
44224422
CREATE TABLE t_identity_drop (a int GENERATED ALWAYS AS IDENTITY (START WITH 10), b int GENERATED BY DEFAULT AS IDENTITY);

0 commit comments

Comments
 (0)