-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostgres.go
More file actions
278 lines (245 loc) · 7.77 KB
/
Copy pathpostgres.go
File metadata and controls
278 lines (245 loc) · 7.77 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*-------------------------------------------------------------------------
*
* radar
*
* Portions copyright (c) 2026, pgEdge, Inc.
* This software is released under The PostgreSQL License
*
*-------------------------------------------------------------------------
*/
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"github.com/jackc/pgx/v5/pgconn"
)
// isPGUnavailableError reports whether err indicates that the queried object
// is not installed/available (missing extension, table, function, or schema).
// These are treated as skips rather than failures. A permission denial is
// excluded deliberately: the object exists and holds data, so being refused is
// reported per collector rather than skipped.
func isPGUnavailableError(err error) bool {
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) {
return false
}
switch pgErr.Code {
case "42P01", // undefined_table
"42704", // undefined_object
"42883", // undefined_function
"3F000": // invalid_schema_name
return true
}
return false
}
// postgresConfigFileTasks defines tasks for collecting PostgreSQL configuration files (sorted alphabetically by name)
var postgresConfigFileTasks = []SimpleConfigFileTask{
{
Name: "pg_hba.conf",
ArchivePath: "postgresql/pg_hba.conf",
Filename: "pg_hba.conf",
},
{
Name: "pg_ident.conf",
ArchivePath: "postgresql/pg_ident.conf",
Filename: "pg_ident.conf",
},
{
Name: "postgresql.auto.conf",
ArchivePath: "postgresql/postgresql.auto.conf",
Filename: "postgresql.auto.conf",
},
{
Name: "postgresql.conf",
ArchivePath: "postgresql/postgresql.conf",
Filename: "postgresql.conf",
},
{
Name: "recovery.conf",
ArchivePath: "postgresql/recovery.conf",
Filename: "recovery.conf",
},
{
Name: "recovery.done",
ArchivePath: "postgresql/recovery.done",
Filename: "recovery.done",
},
}
// getPostgreSQLTasks returns PostgreSQL instance-level collection tasks
func getPostgreSQLTasks(db *sql.DB) []CollectionTask {
// Build simple query tasks from registry
tasks := buildQueryTasks("postgresql", postgresQueryTasks, db)
// Build config file tasks
tasks = append(tasks, buildConfigFileTasks("postgresql", postgresConfigFileTasks, db)...)
return tasks
}
// collectPGConfigFile reads a PostgreSQL config file
func collectPGConfigFile(db *sql.DB, cfg *Config, filename string, w io.Writer) error {
if db == nil {
return fmt.Errorf("PostgreSQL not initialized")
}
// Auto-detect data directory if not provided
if cfg.DataDir == "" {
var dataDir string
err := db.QueryRow("SHOW data_directory").Scan(&dataDir)
if err != nil {
return fmt.Errorf("detecting data directory: %w", err)
}
cfg.DataDir = dataDir
}
path := filepath.Join(cfg.DataDir, filename)
data, err := readFile(path)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
// generateDatabaseTasks creates per-database collection tasks, returning the
// closer for the connection they share.
func generateDatabaseTasks(db *sql.DB) ([]CollectionTask, io.Closer, error) {
if db == nil {
return nil, nil, fmt.Errorf("PostgreSQL not initialized")
}
// Get list of databases
rows, err := db.Query("SELECT datname FROM pg_database WHERE datallowconn ORDER BY datname")
if err != nil {
return nil, nil, fmt.Errorf("querying databases: %w", err)
}
defer closeErrCheck(rows, "database list query rows")
var databases []string
for rows.Next() {
var dbname string
if err := rows.Scan(&dbname); err != nil {
return nil, nil, fmt.Errorf("scanning database name: %w", err)
}
// Always skip template0 and template1
if dbname == "template0" || dbname == "template1" {
continue
}
databases = append(databases, dbname)
}
if err := rows.Err(); err != nil {
return nil, nil, fmt.Errorf("iterating databases: %w", err)
}
// Generate tasks for each database
var tasks []CollectionTask
conns := &dbConns{}
allDBTasks := make([]SimpleQueryTask, 0,
len(perDatabaseQueryTasks)+len(pgStatvizQueryTasks)+len(spockQueryTasks))
allDBTasks = append(allDBTasks, perDatabaseQueryTasks...)
allDBTasks = append(allDBTasks, pgStatvizQueryTasks...)
allDBTasks = append(allDBTasks, spockQueryTasks...)
for _, dbname := range databases {
// Capture loop variables for closure
dbName := dbname
for _, taskDef := range allDBTasks {
// Capture loop variable for closure
td := taskDef
tasks = append(tasks, CollectionTask{
Category: "database",
Name: fmt.Sprintf("%s/%s", dbName, td.Name),
ArchivePath: fmt.Sprintf(td.ArchivePath, dbName),
Collector: func(cfg *Config, w io.Writer) error {
return conns.exec(cfg, dbName, td.Query, w)
},
})
}
}
return tasks, conns, nil
}
// dbConns holds the connection the per-database tasks share. They are generated
// and run grouped by database, so holding one connection and closing it when
// collection moves on costs one connect and one authentication per database
// rather than one per task.
type dbConns struct {
db *sql.DB
name string
err error // why name could not be reached, if it could not
}
// conn returns the connection for dbname, establishing one on first use and
// closing the connection to the database collection has left behind. A database
// that cannot be reached is recorded, so its remaining tasks skip. The database
// radar was invoked against is served by the connection opened at startup.
func (c *dbConns) conn(cfg *Config, dbname string) (*sql.DB, error) {
if dbname == c.name {
if c.err != nil {
return nil, NewSkipError(c.err.Error())
}
return c.db, nil
}
// A different database, so the held connection is finished with.
closeErrCheck(c, "database connection")
if dbname == cfg.Database && cfg.DB != nil {
return cfg.DB, nil
}
db, err := sql.Open("pgx", cfg.ConnectionString(dbname))
if err != nil {
return nil, fmt.Errorf("connecting to %s: %w", dbname, err)
}
// Collectors run one at a time, so one connection is all the pool needs.
db.SetMaxOpenConns(1)
c.name = dbname
// Connect here rather than leaving it to the first query. A database that
// allows connections but refuses this user then costs one rejected login
// instead of one per task, since c.err makes the rest of them skip.
pooled, err := db.Conn(context.Background())
if err != nil {
closeErrCheck(db, "database connection")
c.err = fmt.Errorf("connecting to %s: %w", dbname, err)
return nil, c.err
}
// Back to the pool, which the tasks then draw it from.
closeErrCheck(pooled, "pooled connection")
c.db = db
return db, nil
}
// Close closes the connection the per-database tasks were sharing.
func (c *dbConns) Close() error {
c.name, c.err = "", nil
if c.db == nil {
return nil
}
db := c.db
c.db = nil
return db.Close()
}
// exec executes a query on a specific database
func (c *dbConns) exec(cfg *Config, dbname, query string, w io.Writer) error {
db, err := c.conn(cfg, dbname)
if err != nil {
return err
}
rows, err := db.Query(query)
if err != nil {
if isPGUnavailableError(err) {
return NewSkipError(err.Error())
}
return fmt.Errorf("query failed: %w", err)
}
defer closeErrCheck(rows, "query rows")
return rowsToTSV(rows, w)
}
// printSummary logs the archive filename, size, and collector count.
func printSummary(totalCollected int, outputFile string, cfg *Config) {
stat, err := os.Stat(outputFile)
if err != nil {
errorLog.Printf("Failed to stat archive: %v", err)
return
}
// Format file size nicely (KB)
sizeKB := stat.Size() / 1024
if cfg.Verbose {
// Verbose mode: show total collected
infoLog.Printf("\n✓ Archive created: %s (%d KB)", outputFile, sizeKB)
infoLog.Printf(" Total collectors: %d", totalCollected)
} else {
// Simple success message for default mode
infoLog.Printf("✓ Archive created: %s (%d KB)", outputFile, sizeKB)
}
}