/
temporary_schema.go
90 lines (79 loc) · 2.85 KB
/
temporary_schema.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
// Copyright 2019 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"
"fmt"
"github.com/cockroachdb/cockroach/pkg/internal/client"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
func createTempSchema(params runParams, sKey sqlbase.DescriptorKey) (sqlbase.ID, error) {
id, err := GenerateUniqueDescID(params.ctx, params.extendedEvalCtx.ExecCfg.DB)
if err != nil {
return sqlbase.InvalidID, err
}
if err := params.p.createSchemaWithID(params.ctx, sKey.Key(), id); err != nil {
return sqlbase.InvalidID, err
}
params.p.SetTemporarySchemaName(sKey.Name())
return id, nil
}
func (p *planner) createSchemaWithID(
ctx context.Context, schemaNameKey roachpb.Key, schemaID sqlbase.ID,
) error {
if p.ExtendedEvalContext().Tracing.KVTracingEnabled() {
log.VEventf(ctx, 2, "CPut %s -> %d", schemaNameKey, schemaID)
}
b := &client.Batch{}
b.CPut(schemaNameKey, schemaID, nil)
return p.txn.Run(ctx, b)
}
func temporarySchemaName(sessionID ClusterWideID) string {
return fmt.Sprintf("pg_temp_%v%v", sessionID.Hi, sessionID.Lo)
}
func cleanupSessionTempObjects(ctx context.Context, p *planner, sessionID ClusterWideID) error {
tempSchemaName := temporarySchemaName(sessionID)
if p.sessionDataMutator != nil && p.sessionDataMutator.data.SearchPath.HasCreatedTemporarySchema(tempSchemaName) {
return nil
}
dbIDs, err := GetAllDatabaseDescriptorIDs(ctx, p.txn)
if err != nil {
return err
}
for _, id := range dbIDs {
dbDesc, err := p.Tables().databaseCache.getDatabaseDescByID(ctx, p.txn, id)
if err != nil {
return err
}
// TODO(arul): This should probably go through a dropSchemaImpl function
// once we have user defined schemas/support for dropping schemas implemented.
tbNames, err := GetObjectNames(ctx, p.txn, p, dbDesc, tempSchemaName, true /*explicitPrefix*/)
for i := range tbNames {
tbDesc, err := p.ResolveMutableTableDescriptor(ctx, &tbNames[i], true /* true */, ResolveAnyDescType)
if err != nil {
return err
}
if _, err := p.dropAppropriateDesc(ctx, tbDesc, tree.DropCascade); err != nil {
return err
}
}
// TODO(arul): When there is a schema cache, this should probably go through
// there, so that the cache entry is removed as well.
//
// Finally, also remove the temporary schema from the namespace table
if err := sqlbase.RemoveSchemaNamespaceEntry(ctx, p.txn, id, tempSchemaName); err != nil {
return err
}
}
return nil
}