-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
discard.go
234 lines (208 loc) · 6.39 KB
/
discard.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// 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"
"strings"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/catalog"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descs"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/resolver"
"github.com/cockroachdb/cockroach/pkg/sql/clusterunique"
"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/sessiondata"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// Discard implements the DISCARD statement.
// See https://www.postgresql.org/docs/9.6/static/sql-discard.html for details.
func (p *planner) Discard(ctx context.Context, s *tree.Discard) (planNode, error) {
switch s.Mode {
case tree.DiscardModeAll:
if !p.autoCommit {
return nil, pgerror.New(pgcode.ActiveSQLTransaction,
"DISCARD ALL cannot run inside a transaction block")
}
// RESET ALL
if err := p.sessionDataMutatorIterator.applyOnEachMutatorError(
func(m sessionDataMutator) error {
return resetSessionVars(ctx, m)
},
); err != nil {
return nil, err
}
// DEALLOCATE ALL
p.preparedStatements.DeleteAll(ctx)
// DISCARD TEMP
err := prepareDeleteTempTables(ctx, p)
if err != nil {
return nil, err
}
case tree.DiscardModeTemp:
err := prepareDeleteTempTables(ctx, p)
if err != nil {
return nil, err
}
default:
return nil, errors.AssertionFailedf("unknown mode for DISCARD: %d", s.Mode)
}
return newZeroNode(nil /* columns */), nil
}
func resetSessionVars(ctx context.Context, m sessionDataMutator) error {
for _, varName := range varNames {
if err := resetSessionVar(ctx, m, varName); err != nil {
return err
}
}
return nil
}
func resetSessionVar(ctx context.Context, m sessionDataMutator, varName string) error {
v := varGen[varName]
if v.Set != nil {
hasDefault, defVal := getSessionVarDefaultString(varName, v, m.sessionDataMutatorBase)
if hasDefault {
if err := v.Set(ctx, m, defVal); err != nil {
return err
}
}
}
return nil
}
func getTempSchema(
ctx context.Context, p *planner, codec keys.SQLCodec, allDbDescs []catalog.DatabaseDescriptor,
) (map[clusterunique.ID]struct{}, error) {
sessionIDs := make(map[clusterunique.ID]struct{})
for _, dbDesc := range allDbDescs {
var schemaEntries map[descpb.ID]resolver.SchemaEntryForDB
schemaEntries, err := resolver.GetForDatabase(ctx, p.txn, codec, dbDesc)
if err != nil {
return nil, err
}
for _, scEntry := range schemaEntries {
isTempSchema, sessionID, err := temporarySchemaSessionID(scEntry.Name)
if err != nil {
// This should not cause an error.
log.Warningf(ctx, "could not parse %q as temporary schema name", scEntry)
continue
}
if isTempSchema {
sessionIDs[sessionID] = struct{}{}
}
}
}
log.Infof(ctx, "found %d temporary schemas", len(sessionIDs))
return sessionIDs, nil
}
func prepareDeleteTempTables(ctx context.Context, p *planner) error {
codec := p.execCfg.Codec
descCol := p.Descriptors()
allDbDescs, err := descCol.GetAllDatabaseDescriptors(ctx, p.Txn())
if err != nil {
return err
}
ie := p.execCfg.InternalExecutor
sessionIDs, err := getTempSchema(ctx, p, codec, allDbDescs)
if err != nil {
return err
}
for _, dbDesc := range allDbDescs {
err = removeTempTable(ctx, p, ie, descCol, dbDesc, sessionIDs)
if err != nil {
return err
}
}
return nil
}
func removeTempTable(
ctx context.Context,
p *planner,
ie *InternalExecutor,
descCol *descs.Collection,
dbDesc catalog.DatabaseDescriptor,
sessionIDs map[clusterunique.ID]struct{},
) error {
for sessionID := range sessionIDs {
schemaName := temporarySchemaName(sessionID)
tbNames, tbIDs, err := descCol.GetObjectNamesAndIDs(
ctx,
p.txn,
dbDesc,
schemaName,
tree.DatabaseListFlags{CommonLookupFlags: tree.CommonLookupFlags{Required: false}},
)
if err != nil {
return err
}
databaseIDToTempSchemaID := make(map[uint32]uint32)
var tables descpb.IDs
tblDescsByID := make(map[descpb.ID]catalog.TableDescriptor, len(tbNames))
tblNamesByID := make(map[descpb.ID]tree.TableName, len(tbNames))
for i, tbName := range tbNames {
desc, err := descCol.Direct().MustGetTableDescByID(ctx, p.txn, tbIDs[i])
if err != nil {
return err
}
tblDescsByID[desc.GetID()] = desc
tblNamesByID[desc.GetID()] = tbName
databaseIDToTempSchemaID[uint32(desc.GetParentID())] = uint32(desc.GetParentSchemaID())
if !desc.IsSequence() {
tables = append(tables, desc.GetID())
}
}
searchPath := sessiondata.DefaultSearchPathForUser(username.RootUserName()).WithTemporarySchemaName(schemaName)
override := sessiondata.InternalExecutorOverride{
SearchPath: &searchPath,
User: username.RootUserName(),
DatabaseIDToTempSchemaID: databaseIDToTempSchemaID,
}
for _, toDelete := range []struct {
// typeName is the type of table being deleted, e.g. view, table, sequence
typeName string
// ids represents which ids we wish to remove.
ids descpb.IDs
// preHook is used to perform any operations needed before calling
// delete on all the given ids.
preHook func(descpb.ID) error
}{
{"TABLE", tables, nil},
} {
if len(toDelete.ids) > 0 {
if toDelete.preHook != nil {
for _, id := range toDelete.ids {
if err := toDelete.preHook(id); err != nil {
return err
}
}
}
var query strings.Builder
query.WriteString("DROP ")
query.WriteString(toDelete.typeName)
for i, id := range toDelete.ids {
tbName := tblNamesByID[id]
if i != 0 {
query.WriteString(",")
}
query.WriteString(" ")
query.WriteString(tbName.FQString())
}
query.WriteString(" CASCADE")
_, err = ie.ExecEx(ctx, "delete-temp-"+toDelete.typeName, p.txn, override, query.String())
if err != nil {
return err
}
}
}
}
return nil
}