-
Notifications
You must be signed in to change notification settings - Fork 260
/
postgres.go
144 lines (126 loc) · 4.13 KB
/
postgres.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
/*
Copyright The CloudNativePG Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package infrastructure
import (
"context"
"database/sql"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/cloudnative-pg/cloudnative-pg/pkg/management/log"
"github.com/cloudnative-pg/cloudnative-pg/pkg/postgres"
"github.com/cloudnative-pg/cloudnative-pg/pkg/specs"
)
// postgresTablespaceManager is a TablespaceManager for a database instance
type postgresTablespaceManager struct {
superUserDB *sql.DB
}
// NewPostgresTablespaceManager returns an implementation of TablespaceManager for postgres
func NewPostgresTablespaceManager(superDB *sql.DB) TablespaceManager {
return newPostgresTablespaceManager(superDB)
}
// NewPostgresTablespaceManager returns an implementation of TablespaceManager for postgres
func newPostgresTablespaceManager(superDB *sql.DB) postgresTablespaceManager {
return postgresTablespaceManager{
superUserDB: superDB,
}
}
// List the tablespaces in the database
// The content exclude pg_default and pg_global database
func (tbsMgr postgresTablespaceManager) List(ctx context.Context) ([]Tablespace, error) {
logger := log.FromContext(ctx).WithName("tbs_reconciler_list")
logger.Trace("Invoked list")
wrapErr := func(err error) error { return fmt.Errorf("while listing DB tablespaces: %w", err) }
rows, err := tbsMgr.superUserDB.QueryContext(
ctx,
`
SELECT
pg_tablespace.spcname spcname,
COALESCE(pg_roles.rolname, '') rolname
FROM pg_tablespace
LEFT JOIN pg_roles ON pg_tablespace.spcowner = pg_roles.oid
WHERE spcname NOT LIKE $1
`,
postgres.SystemTablespacesPrefix,
)
if err != nil {
return nil, wrapErr(err)
}
defer func() {
if closeErr := rows.Close(); closeErr != nil {
logger.Info("Ignorable error while closing pg_catalog.pg_tablespace", "err", closeErr)
}
}()
var tablespaces []Tablespace
for rows.Next() {
var tbs Tablespace
err := rows.Scan(
&tbs.Name,
&tbs.Owner,
)
if err != nil {
return nil, wrapErr(err)
}
tablespaces = append(tablespaces, tbs)
}
if rows.Err() != nil {
return nil, wrapErr(rows.Err())
}
return tablespaces, nil
}
// Create the tablespace in the database, if tablespace is temporary tablespace, need reload configure
func (tbsMgr postgresTablespaceManager) Create(ctx context.Context, tbs Tablespace) error {
contextLog := log.FromContext(ctx).WithName("tbs_reconciler_create")
tablespaceLocation := specs.LocationForTablespace(tbs.Name)
contextLog.Info("Creating tablespace",
"tablespace", tbs,
"tablespaceLocation", tablespaceLocation)
wrapErr := func(err error) error {
return fmt.Errorf("while creating tablespace %s: %w", tbs.Name, err)
}
var err error
if _, err = tbsMgr.superUserDB.ExecContext(
ctx,
fmt.Sprintf(
"CREATE TABLESPACE %s OWNER %s LOCATION '%s'",
pgx.Identifier{tbs.Name}.Sanitize(),
pgx.Identifier{tbs.Owner}.Sanitize(),
tablespaceLocation,
),
); err != nil {
return wrapErr(err)
}
return nil
}
// Update the tablespace in the database (change ownership)
func (tbsMgr postgresTablespaceManager) Update(ctx context.Context, tbs Tablespace) error {
contextLog := log.FromContext(ctx).WithName("tbs_reconciler_update")
tablespaceLocation := specs.LocationForTablespace(tbs.Name)
contextLog.Info("Updating tablespace",
"tablespace", tbs,
"tablespaceLocation", tablespaceLocation)
wrapErr := func(err error) error {
return fmt.Errorf("while updating tablespace %s: %w", tbs.Name, err)
}
var err error
if _, err = tbsMgr.superUserDB.ExecContext(
ctx,
fmt.Sprintf(
"ALTER TABLESPACE %s OWNER TO %s",
pgx.Identifier{tbs.Name}.Sanitize(),
pgx.Identifier{tbs.Owner}.Sanitize(),
),
); err != nil {
return wrapErr(err)
}
return nil
}