forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alter_sequence.go
141 lines (121 loc) · 4.5 KB
/
alter_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
// Copyright 2015 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"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
)
type alterSequenceNode struct {
n *tree.AlterSequence
seqDesc *tabledesc.Mutable
}
// AlterSequence transforms a tree.AlterSequence into a plan node.
func (p *planner) AlterSequence(ctx context.Context, n *tree.AlterSequence) (planNode, error) {
if err := checkSchemaChangeEnabled(
ctx,
p.ExecCfg(),
"ALTER SEQUENCE",
); err != nil {
return nil, err
}
seqDesc, err := p.ResolveMutableTableDescriptorEx(
ctx, n.Name, !n.IfExists, tree.ResolveRequireSequenceDesc,
)
if err != nil {
return nil, err
}
if seqDesc == nil {
return newZeroNode(nil /* columns */), nil
}
if err := p.CheckPrivilege(ctx, seqDesc, privilege.CREATE); err != nil {
return nil, err
}
return &alterSequenceNode{n: n, seqDesc: seqDesc}, nil
}
// ReadingOwnWrites implements the planNodeReadingOwnWrites interface.
// This is because ALTER SEQUENCE performs multiple KV operations on descriptors
// and expects to see its own writes.
func (n *alterSequenceNode) ReadingOwnWrites() {}
func (n *alterSequenceNode) startExec(params runParams) error {
telemetry.Inc(sqltelemetry.SchemaChangeAlterCounter("sequence"))
desc := n.seqDesc
oldMinValue := desc.SequenceOpts.MinValue
oldMaxValue := desc.SequenceOpts.MaxValue
err := assignSequenceOptions(
desc.SequenceOpts, n.n.Options, false /* setDefaults */, ¶ms, desc.GetID(), desc.ParentID,
)
if err != nil {
return err
}
opts := desc.SequenceOpts
seqValueKey := params.p.ExecCfg().Codec.SequenceKey(uint32(desc.ID))
getSequenceValue := func() (int64, error) {
kv, err := params.p.txn.Get(params.ctx, seqValueKey)
if err != nil {
return 0, err
}
return kv.ValueInt(), nil
}
// Due to the semantics of sequence caching (see sql.planner.incrementSequenceUsingCache()),
// it is possible for a sequence to have a value that exceeds its MinValue or MaxValue. Users
// do no see values extending the sequence's bounds, and instead see "bounds exceeded" errors.
// To make a usable again after exceeding its bounds, there are two options:
// 1. The user changes the sequence's value by calling setval(...)
// 2. The user performs a schema change to alter the sequences MinValue or MaxValue. In this case, the
// value of the sequence must be restored to the original MinValue or MaxValue transactionally.
// The code below handles the second case.
// The sequence is decreasing and the minvalue is being decreased.
if opts.Increment < 0 && desc.SequenceOpts.MinValue < oldMinValue {
sequenceVal, err := getSequenceValue()
if err != nil {
return err
}
// If the sequence exceeded the old MinValue, it must be changed to start at the old MinValue.
if sequenceVal < oldMinValue {
err := params.p.txn.Put(params.ctx, seqValueKey, oldMinValue)
if err != nil {
return err
}
}
} else if opts.Increment > 0 && desc.SequenceOpts.MaxValue > oldMaxValue {
sequenceVal, err := getSequenceValue()
if err != nil {
return err
}
if sequenceVal > oldMaxValue {
err := params.p.txn.Put(params.ctx, seqValueKey, oldMaxValue)
if err != nil {
return err
}
}
}
if err := params.p.writeSchemaChange(
params.ctx, n.seqDesc, descpb.InvalidMutationID, tree.AsStringWithFQNames(n.n, params.Ann()),
); err != nil {
return err
}
// Record this sequence alteration in the event log. This is an auditable log
// event and is recorded in the same transaction as the table descriptor
// update.
return params.p.logEvent(params.ctx,
n.seqDesc.ID,
&eventpb.AlterSequence{
SequenceName: params.p.ResolvedName(n.n.Name).FQString(),
})
}
func (n *alterSequenceNode) Next(runParams) (bool, error) { return false, nil }
func (n *alterSequenceNode) Values() tree.Datums { return tree.Datums{} }
func (n *alterSequenceNode) Close(context.Context) {}