-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathdrop_sequence.go
217 lines (196 loc) · 6.54 KB
/
drop_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
// 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"
"github.com/cockroachdb/cockroach/pkg/server/telemetry"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/tabledesc"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode"
"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltelemetry"
"github.com/cockroachdb/cockroach/pkg/util/errorutil/unimplemented"
"github.com/cockroachdb/cockroach/pkg/util/iterutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/eventpb"
"github.com/cockroachdb/errors"
)
type dropSequenceNode struct {
n *tree.DropSequence
td []toDelete
}
func (p *planner) DropSequence(ctx context.Context, n *tree.DropSequence) (planNode, error) {
if err := checkSchemaChangeEnabled(
ctx,
p.ExecCfg(),
"DROP SEQUENCE",
); err != nil {
return nil, err
}
td := make([]toDelete, 0, len(n.Names))
for i := range n.Names {
tn := &n.Names[i]
droppedDesc, err := p.prepareDrop(ctx, tn, !n.IfExists, tree.ResolveRequireSequenceDesc)
if err != nil {
return nil, err
}
if droppedDesc == nil {
// IfExists specified and descriptor does not exist.
continue
}
if depErr := p.sequenceDependencyError(ctx, droppedDesc); depErr != nil {
return nil, depErr
}
td = append(td, toDelete{tn, droppedDesc})
}
if len(td) == 0 {
return newZeroNode(nil /* columns */), nil
}
return &dropSequenceNode{
n: n,
td: td,
}, nil
}
// ReadingOwnWrites implements the planNodeReadingOwnWrites interface.
// This is because DROP SEQUENCE performs multiple KV operations on descriptors
// and expects to see its own writes.
func (n *dropSequenceNode) ReadingOwnWrites() {}
func (n *dropSequenceNode) startExec(params runParams) error {
telemetry.Inc(sqltelemetry.SchemaChangeDropCounter("sequence"))
ctx := params.ctx
for _, toDel := range n.td {
droppedDesc := toDel.desc
err := params.p.dropSequenceImpl(
ctx, droppedDesc, true /* queueJob */, tree.AsStringWithFQNames(n.n, params.Ann()), n.n.DropBehavior,
)
if err != nil {
return err
}
// Log a Drop Sequence event for this table. This is an auditable log event
// and is recorded in the same transaction as the table descriptor
// update.
if err := params.p.logEvent(params.ctx,
droppedDesc.ID,
&eventpb.DropSequence{
SequenceName: toDel.tn.FQString(),
}); err != nil {
return err
}
}
return nil
}
func (*dropSequenceNode) Next(runParams) (bool, error) { return false, nil }
func (*dropSequenceNode) Values() tree.Datums { return tree.Datums{} }
func (*dropSequenceNode) Close(context.Context) {}
func (p *planner) dropSequenceImpl(
ctx context.Context,
seqDesc *tabledesc.Mutable,
queueJob bool,
jobDesc string,
behavior tree.DropBehavior,
) error {
if err := removeSequenceOwnerIfExists(ctx, p, seqDesc.ID, seqDesc.GetSequenceOpts()); err != nil {
return err
}
return p.initiateDropTable(ctx, seqDesc, queueJob, jobDesc, true /* drainName */)
}
// sequenceDependency error returns an error if the given sequence cannot be dropped because
// a table uses it in a DEFAULT expression on one of its columns, or nil if there is no
// such dependency.
func (p *planner) sequenceDependencyError(
ctx context.Context, droppedDesc *tabledesc.Mutable,
) error {
if len(droppedDesc.DependedOnBy) > 0 {
return pgerror.Newf(
pgcode.DependentObjectsStillExist,
"cannot drop sequence %s because other objects depend on it",
droppedDesc.Name,
)
}
return nil
}
func (p *planner) canRemoveAllTableOwnedSequences(
ctx context.Context, desc *tabledesc.Mutable, behavior tree.DropBehavior,
) error {
for _, col := range desc.Columns {
err := p.canRemoveOwnedSequencesImpl(ctx, desc, &col, behavior, false /* isColumnDrop */)
if err != nil {
return err
}
}
return nil
}
func (p *planner) canRemoveAllColumnOwnedSequences(
ctx context.Context,
desc *tabledesc.Mutable,
col *descpb.ColumnDescriptor,
behavior tree.DropBehavior,
) error {
return p.canRemoveOwnedSequencesImpl(ctx, desc, col, behavior, true /* isColumnDrop */)
}
func (p *planner) canRemoveOwnedSequencesImpl(
ctx context.Context,
desc *tabledesc.Mutable,
col *descpb.ColumnDescriptor,
behavior tree.DropBehavior,
isColumnDrop bool,
) error {
for _, sequenceID := range col.OwnsSequenceIds {
seqDesc, err := p.LookupTableByID(ctx, sequenceID)
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 ensuring owned sequences can be removed: %s", err.Error())
continue
}
return err
}
affectsNoColumns := true
canBeSafelyRemoved := false
_ = seqDesc.ForeachDependedOnBy(func(dep *descpb.TableDescriptor_Reference) error {
if affectsNoColumns {
// First DependedOnBy.
affectsNoColumns = false
// It is okay if the sequence is depended on by columns that are being
// dropped in the same transaction
canBeSafelyRemoved = dep.ID == desc.ID
// If only the column is being dropped, no other columns of the table can
// depend on that sequence either
if isColumnDrop {
canBeSafelyRemoved = canBeSafelyRemoved && len(dep.ColumnIDs) == 1 &&
dep.ColumnIDs[0] == col.ID
}
return nil
}
// Second DependedOnBy.
canBeSafelyRemoved = false
return iterutil.StopIteration()
})
canRemove := affectsNoColumns || canBeSafelyRemoved
// Once Drop Sequence Cascade actually respects the drop behavior, this
// check should go away.
if behavior == tree.DropCascade && !canRemove {
return unimplemented.NewWithIssue(20965, "DROP SEQUENCE CASCADE is currently unimplemented")
}
// If Cascade is not enabled, and more than 1 columns depend on it, and the
if behavior != tree.DropCascade && !canRemove {
return pgerror.Newf(
pgcode.DependentObjectsStillExist,
"cannot drop table %s because other objects depend on it",
desc.Name,
)
}
}
return nil
}