-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostgres.go
More file actions
207 lines (181 loc) · 5.41 KB
/
postgres.go
File metadata and controls
207 lines (181 loc) · 5.41 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
/*-------------------------------------------------------------------------
*
* radar
*
* Portions copyright (c) 2026, pgEdge, Inc.
* This software is released under The PostgreSQL License
*
*-------------------------------------------------------------------------
*/
package main
import (
"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.
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
func generateDatabaseTasks(db *sql.DB) ([]CollectionTask, error) {
if db == nil {
return 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, 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, 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, fmt.Errorf("iterating databases: %w", err)
}
// Generate tasks for each database
var tasks []CollectionTask
allDBTasks := append(perDatabaseQueryTasks, pgStatvizQueryTasks...)
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 execPGQueryOnDB(dbName, cfg, td.Query, w)
},
})
}
}
return tasks, nil
}
// execPGQueryOnDB executes a query on a specific database
func execPGQueryOnDB(dbname string, cfg *Config, query string, w io.Writer) error {
db, err := sql.Open("pgx", cfg.ConnectionString(dbname))
if err != nil {
return fmt.Errorf("connecting to %s: %w", dbname, err)
}
defer closeErrCheck(db, "database connection")
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)
}
}