postgresai 0.14.0-dev.42 → 0.14.0-dev.44
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/postgres-ai.ts +649 -310
- package/bun.lock +258 -0
- package/dist/bin/postgres-ai.js +29491 -1910
- package/dist/sql/01.role.sql +16 -0
- package/dist/sql/02.permissions.sql +37 -0
- package/dist/sql/03.optional_rds.sql +6 -0
- package/dist/sql/04.optional_self_managed.sql +8 -0
- package/dist/sql/05.helpers.sql +415 -0
- package/lib/auth-server.ts +58 -97
- package/lib/checkup-api.ts +175 -0
- package/lib/checkup.ts +833 -0
- package/lib/config.ts +3 -0
- package/lib/init.ts +106 -74
- package/lib/issues.ts +121 -194
- package/lib/mcp-server.ts +6 -17
- package/lib/metrics-loader.ts +156 -0
- package/package.json +13 -9
- package/sql/02.permissions.sql +9 -5
- package/sql/05.helpers.sql +415 -0
- package/test/checkup.test.ts +953 -0
- package/test/init.integration.test.ts +396 -0
- package/test/init.test.ts +345 -0
- package/test/schema-validation.test.ts +188 -0
- package/tsconfig.json +12 -20
- package/dist/bin/postgres-ai.d.ts +0 -3
- package/dist/bin/postgres-ai.d.ts.map +0 -1
- package/dist/bin/postgres-ai.js.map +0 -1
- package/dist/lib/auth-server.d.ts +0 -31
- package/dist/lib/auth-server.d.ts.map +0 -1
- package/dist/lib/auth-server.js +0 -263
- package/dist/lib/auth-server.js.map +0 -1
- package/dist/lib/config.d.ts +0 -45
- package/dist/lib/config.d.ts.map +0 -1
- package/dist/lib/config.js +0 -181
- package/dist/lib/config.js.map +0 -1
- package/dist/lib/init.d.ts +0 -85
- package/dist/lib/init.d.ts.map +0 -1
- package/dist/lib/init.js +0 -644
- package/dist/lib/init.js.map +0 -1
- package/dist/lib/issues.d.ts +0 -75
- package/dist/lib/issues.d.ts.map +0 -1
- package/dist/lib/issues.js +0 -336
- package/dist/lib/issues.js.map +0 -1
- package/dist/lib/mcp-server.d.ts +0 -9
- package/dist/lib/mcp-server.d.ts.map +0 -1
- package/dist/lib/mcp-server.js +0 -168
- package/dist/lib/mcp-server.js.map +0 -1
- package/dist/lib/pkce.d.ts +0 -32
- package/dist/lib/pkce.d.ts.map +0 -1
- package/dist/lib/pkce.js +0 -101
- package/dist/lib/pkce.js.map +0 -1
- package/dist/lib/util.d.ts +0 -27
- package/dist/lib/util.d.ts.map +0 -1
- package/dist/lib/util.js +0 -46
- package/dist/lib/util.js.map +0 -1
- package/dist/package.json +0 -46
- package/test/init.integration.test.cjs +0 -382
- package/test/init.test.cjs +0 -392
package/lib/checkup.ts
ADDED
|
@@ -0,0 +1,833 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Express Checkup Module
|
|
3
|
+
* ======================
|
|
4
|
+
* Generates JSON health check reports directly from PostgreSQL without Prometheus.
|
|
5
|
+
*
|
|
6
|
+
* ARCHITECTURAL DECISIONS
|
|
7
|
+
* -----------------------
|
|
8
|
+
*
|
|
9
|
+
* 1. SINGLE SOURCE OF TRUTH FOR SQL QUERIES
|
|
10
|
+
* All SQL queries MUST be loaded from config/pgwatch-prometheus/metrics.yml
|
|
11
|
+
* via getMetricSql() from metrics-loader.ts.
|
|
12
|
+
*
|
|
13
|
+
* DO NOT copy-paste or inline SQL queries in this file!
|
|
14
|
+
*
|
|
15
|
+
* The metrics.yml file is the single source of truth for metric extraction
|
|
16
|
+
* logic, shared between:
|
|
17
|
+
* - Full-fledged monitoring (Prometheus/pgwatch)
|
|
18
|
+
* - Express checkup (this CLI tool)
|
|
19
|
+
*
|
|
20
|
+
* This ensures consistency and avoids maintenance burden of duplicate queries.
|
|
21
|
+
*
|
|
22
|
+
* 2. JSON SCHEMA COMPLIANCE
|
|
23
|
+
* All generated reports MUST comply with JSON schemas in reporter/schemas/.
|
|
24
|
+
* These schemas define the expected format for both:
|
|
25
|
+
* - Full-fledged monitoring reporter output
|
|
26
|
+
* - Express checkup output
|
|
27
|
+
*
|
|
28
|
+
* Before adding or modifying a report, verify the corresponding schema exists
|
|
29
|
+
* and ensure the output matches. Run schema validation tests to confirm.
|
|
30
|
+
*
|
|
31
|
+
* ADDING NEW REPORTS
|
|
32
|
+
* ------------------
|
|
33
|
+
* 1. Add/verify the metric exists in config/pgwatch-prometheus/metrics.yml
|
|
34
|
+
* 2. Add the metric name mapping to METRIC_NAMES in metrics-loader.ts
|
|
35
|
+
* 3. Verify JSON schema exists in reporter/schemas/{CHECK_ID}.schema.json
|
|
36
|
+
* 4. Implement the generator function using getMetricSql()
|
|
37
|
+
* 5. Add schema validation test in test/schema-validation.test.ts
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import { Client } from "pg";
|
|
41
|
+
import * as fs from "fs";
|
|
42
|
+
import * as path from "path";
|
|
43
|
+
import * as pkg from "../package.json";
|
|
44
|
+
import { getMetricSql, transformMetricRow, METRIC_NAMES } from "./metrics-loader";
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* PostgreSQL version information
|
|
48
|
+
*/
|
|
49
|
+
export interface PostgresVersion {
|
|
50
|
+
version: string;
|
|
51
|
+
server_version_num: string;
|
|
52
|
+
server_major_ver: string;
|
|
53
|
+
server_minor_ver: string;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Setting information from pg_settings
|
|
58
|
+
*/
|
|
59
|
+
export interface SettingInfo {
|
|
60
|
+
setting: string;
|
|
61
|
+
unit: string;
|
|
62
|
+
category: string;
|
|
63
|
+
context: string;
|
|
64
|
+
vartype: string;
|
|
65
|
+
pretty_value: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Altered setting (A007) - subset of SettingInfo
|
|
70
|
+
*/
|
|
71
|
+
export interface AlteredSetting {
|
|
72
|
+
value: string;
|
|
73
|
+
unit: string;
|
|
74
|
+
category: string;
|
|
75
|
+
pretty_value: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Cluster metric (A004)
|
|
80
|
+
*/
|
|
81
|
+
export interface ClusterMetric {
|
|
82
|
+
value: string;
|
|
83
|
+
unit: string;
|
|
84
|
+
description: string;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Invalid index entry (H001) - matches H001.schema.json invalidIndex
|
|
89
|
+
*/
|
|
90
|
+
export interface InvalidIndex {
|
|
91
|
+
schema_name: string;
|
|
92
|
+
table_name: string;
|
|
93
|
+
index_name: string;
|
|
94
|
+
relation_name: string;
|
|
95
|
+
index_size_bytes: number;
|
|
96
|
+
index_size_pretty: string;
|
|
97
|
+
supports_fk: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Unused index entry (H002) - matches H002.schema.json unusedIndex
|
|
102
|
+
*/
|
|
103
|
+
export interface UnusedIndex {
|
|
104
|
+
schema_name: string;
|
|
105
|
+
table_name: string;
|
|
106
|
+
index_name: string;
|
|
107
|
+
index_definition: string;
|
|
108
|
+
reason: string;
|
|
109
|
+
idx_scan: number;
|
|
110
|
+
index_size_bytes: number;
|
|
111
|
+
idx_is_btree: boolean;
|
|
112
|
+
supports_fk: boolean;
|
|
113
|
+
index_size_pretty: string;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Stats reset info for H002 - matches H002.schema.json statsReset
|
|
118
|
+
*/
|
|
119
|
+
export interface StatsReset {
|
|
120
|
+
stats_reset_epoch: number | null;
|
|
121
|
+
stats_reset_time: string | null;
|
|
122
|
+
days_since_reset: number | null;
|
|
123
|
+
postmaster_startup_epoch: number | null;
|
|
124
|
+
postmaster_startup_time: string | null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Redundant index entry (H004) - matches H004.schema.json redundantIndex
|
|
129
|
+
*/
|
|
130
|
+
export interface RedundantIndex {
|
|
131
|
+
schema_name: string;
|
|
132
|
+
table_name: string;
|
|
133
|
+
index_name: string;
|
|
134
|
+
relation_name: string;
|
|
135
|
+
access_method: string;
|
|
136
|
+
reason: string;
|
|
137
|
+
index_size_bytes: number;
|
|
138
|
+
table_size_bytes: number;
|
|
139
|
+
index_usage: number;
|
|
140
|
+
supports_fk: boolean;
|
|
141
|
+
index_definition: string;
|
|
142
|
+
index_size_pretty: string;
|
|
143
|
+
table_size_pretty: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Node result for reports
|
|
148
|
+
*/
|
|
149
|
+
export interface NodeResult {
|
|
150
|
+
data: Record<string, any>;
|
|
151
|
+
postgres_version?: PostgresVersion;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Report structure matching JSON schemas
|
|
156
|
+
*/
|
|
157
|
+
export interface Report {
|
|
158
|
+
version: string | null;
|
|
159
|
+
build_ts: string | null;
|
|
160
|
+
checkId: string;
|
|
161
|
+
checkTitle: string;
|
|
162
|
+
timestamptz: string;
|
|
163
|
+
nodes: {
|
|
164
|
+
primary: string;
|
|
165
|
+
standbys: string[];
|
|
166
|
+
};
|
|
167
|
+
results: Record<string, NodeResult>;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Parse PostgreSQL version number into major and minor components
|
|
172
|
+
*/
|
|
173
|
+
export function parseVersionNum(versionNum: string): { major: string; minor: string } {
|
|
174
|
+
if (!versionNum || versionNum.length < 6) {
|
|
175
|
+
return { major: "", minor: "" };
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
const num = parseInt(versionNum, 10);
|
|
179
|
+
return {
|
|
180
|
+
major: Math.floor(num / 10000).toString(),
|
|
181
|
+
minor: (num % 10000).toString(),
|
|
182
|
+
};
|
|
183
|
+
} catch {
|
|
184
|
+
return { major: "", minor: "" };
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Format bytes to human readable string
|
|
190
|
+
*/
|
|
191
|
+
export function formatBytes(bytes: number): string {
|
|
192
|
+
if (bytes === 0) return "0 B";
|
|
193
|
+
const units = ["B", "kB", "MB", "GB", "TB", "PB"];
|
|
194
|
+
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
|
195
|
+
return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Get PostgreSQL version information
|
|
200
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (express_version)
|
|
201
|
+
*/
|
|
202
|
+
export async function getPostgresVersion(client: Client): Promise<PostgresVersion> {
|
|
203
|
+
const sql = getMetricSql(METRIC_NAMES.version);
|
|
204
|
+
const result = await client.query(sql);
|
|
205
|
+
|
|
206
|
+
let version = "";
|
|
207
|
+
let serverVersionNum = "";
|
|
208
|
+
|
|
209
|
+
for (const row of result.rows) {
|
|
210
|
+
if (row.name === "server_version") {
|
|
211
|
+
version = row.setting;
|
|
212
|
+
} else if (row.name === "server_version_num") {
|
|
213
|
+
serverVersionNum = row.setting;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const { major, minor } = parseVersionNum(serverVersionNum);
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
version,
|
|
221
|
+
server_version_num: serverVersionNum,
|
|
222
|
+
server_major_ver: major,
|
|
223
|
+
server_minor_ver: minor,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Get all PostgreSQL settings
|
|
229
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (express_settings)
|
|
230
|
+
*/
|
|
231
|
+
export async function getSettings(client: Client): Promise<Record<string, SettingInfo>> {
|
|
232
|
+
const sql = getMetricSql(METRIC_NAMES.settings);
|
|
233
|
+
const result = await client.query(sql);
|
|
234
|
+
const settings: Record<string, SettingInfo> = {};
|
|
235
|
+
|
|
236
|
+
for (const row of result.rows) {
|
|
237
|
+
settings[row.name] = {
|
|
238
|
+
setting: row.setting,
|
|
239
|
+
unit: row.unit || "",
|
|
240
|
+
category: row.category,
|
|
241
|
+
context: row.context,
|
|
242
|
+
vartype: row.vartype,
|
|
243
|
+
pretty_value: row.pretty_value,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
return settings;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Get altered (non-default) PostgreSQL settings
|
|
252
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (express_altered_settings)
|
|
253
|
+
*/
|
|
254
|
+
export async function getAlteredSettings(client: Client): Promise<Record<string, AlteredSetting>> {
|
|
255
|
+
const sql = getMetricSql(METRIC_NAMES.alteredSettings);
|
|
256
|
+
const result = await client.query(sql);
|
|
257
|
+
const settings: Record<string, AlteredSetting> = {};
|
|
258
|
+
|
|
259
|
+
for (const row of result.rows) {
|
|
260
|
+
settings[row.name] = {
|
|
261
|
+
value: row.setting,
|
|
262
|
+
unit: row.unit || "",
|
|
263
|
+
category: row.category,
|
|
264
|
+
pretty_value: row.pretty_value,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return settings;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Get database sizes
|
|
273
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (express_database_sizes)
|
|
274
|
+
*/
|
|
275
|
+
export async function getDatabaseSizes(client: Client): Promise<Record<string, number>> {
|
|
276
|
+
const sql = getMetricSql(METRIC_NAMES.databaseSizes);
|
|
277
|
+
const result = await client.query(sql);
|
|
278
|
+
const sizes: Record<string, number> = {};
|
|
279
|
+
|
|
280
|
+
for (const row of result.rows) {
|
|
281
|
+
sizes[row.datname] = parseInt(row.size_bytes, 10);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
return sizes;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Get cluster general info metrics
|
|
289
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (express_cluster_stats, express_connection_states, express_uptime)
|
|
290
|
+
*/
|
|
291
|
+
export async function getClusterInfo(client: Client): Promise<Record<string, ClusterMetric>> {
|
|
292
|
+
const info: Record<string, ClusterMetric> = {};
|
|
293
|
+
|
|
294
|
+
// Get cluster statistics
|
|
295
|
+
const clusterStatsSql = getMetricSql(METRIC_NAMES.clusterStats);
|
|
296
|
+
const statsResult = await client.query(clusterStatsSql);
|
|
297
|
+
if (statsResult.rows.length > 0) {
|
|
298
|
+
const stats = statsResult.rows[0];
|
|
299
|
+
|
|
300
|
+
info.total_connections = {
|
|
301
|
+
value: String(stats.total_connections || 0),
|
|
302
|
+
unit: "connections",
|
|
303
|
+
description: "Total active database connections",
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
info.total_commits = {
|
|
307
|
+
value: String(stats.total_commits || 0),
|
|
308
|
+
unit: "transactions",
|
|
309
|
+
description: "Total committed transactions",
|
|
310
|
+
};
|
|
311
|
+
|
|
312
|
+
info.total_rollbacks = {
|
|
313
|
+
value: String(stats.total_rollbacks || 0),
|
|
314
|
+
unit: "transactions",
|
|
315
|
+
description: "Total rolled back transactions",
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const blocksHit = parseInt(stats.blocks_hit || "0", 10);
|
|
319
|
+
const blocksRead = parseInt(stats.blocks_read || "0", 10);
|
|
320
|
+
const totalBlocks = blocksHit + blocksRead;
|
|
321
|
+
const cacheHitRatio = totalBlocks > 0 ? ((blocksHit / totalBlocks) * 100).toFixed(2) : "0.00";
|
|
322
|
+
|
|
323
|
+
info.cache_hit_ratio = {
|
|
324
|
+
value: cacheHitRatio,
|
|
325
|
+
unit: "%",
|
|
326
|
+
description: "Buffer cache hit ratio",
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
info.blocks_read = {
|
|
330
|
+
value: String(blocksRead),
|
|
331
|
+
unit: "blocks",
|
|
332
|
+
description: "Total disk blocks read",
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
info.blocks_hit = {
|
|
336
|
+
value: String(blocksHit),
|
|
337
|
+
unit: "blocks",
|
|
338
|
+
description: "Total buffer cache hits",
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
info.tuples_returned = {
|
|
342
|
+
value: String(stats.tuples_returned || 0),
|
|
343
|
+
unit: "rows",
|
|
344
|
+
description: "Total rows returned by queries",
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
info.tuples_fetched = {
|
|
348
|
+
value: String(stats.tuples_fetched || 0),
|
|
349
|
+
unit: "rows",
|
|
350
|
+
description: "Total rows fetched by queries",
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
info.tuples_inserted = {
|
|
354
|
+
value: String(stats.tuples_inserted || 0),
|
|
355
|
+
unit: "rows",
|
|
356
|
+
description: "Total rows inserted",
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
info.tuples_updated = {
|
|
360
|
+
value: String(stats.tuples_updated || 0),
|
|
361
|
+
unit: "rows",
|
|
362
|
+
description: "Total rows updated",
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
info.tuples_deleted = {
|
|
366
|
+
value: String(stats.tuples_deleted || 0),
|
|
367
|
+
unit: "rows",
|
|
368
|
+
description: "Total rows deleted",
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
info.total_deadlocks = {
|
|
372
|
+
value: String(stats.total_deadlocks || 0),
|
|
373
|
+
unit: "deadlocks",
|
|
374
|
+
description: "Total deadlocks detected",
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
info.temp_files_created = {
|
|
378
|
+
value: String(stats.temp_files_created || 0),
|
|
379
|
+
unit: "files",
|
|
380
|
+
description: "Total temporary files created",
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const tempBytes = parseInt(stats.temp_bytes_written || "0", 10);
|
|
384
|
+
info.temp_bytes_written = {
|
|
385
|
+
value: formatBytes(tempBytes),
|
|
386
|
+
unit: "bytes",
|
|
387
|
+
description: "Total temporary file bytes written",
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
// Get connection states
|
|
392
|
+
const connStatesSql = getMetricSql(METRIC_NAMES.connectionStates);
|
|
393
|
+
const connResult = await client.query(connStatesSql);
|
|
394
|
+
for (const row of connResult.rows) {
|
|
395
|
+
const stateKey = `connections_${row.state.replace(/\s+/g, "_")}`;
|
|
396
|
+
info[stateKey] = {
|
|
397
|
+
value: String(row.count),
|
|
398
|
+
unit: "connections",
|
|
399
|
+
description: `Connections in '${row.state}' state`,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Get uptime info
|
|
404
|
+
const uptimeSql = getMetricSql(METRIC_NAMES.uptimeInfo);
|
|
405
|
+
const uptimeResult = await client.query(uptimeSql);
|
|
406
|
+
if (uptimeResult.rows.length > 0) {
|
|
407
|
+
const uptime = uptimeResult.rows[0];
|
|
408
|
+
info.start_time = {
|
|
409
|
+
value: uptime.start_time.toISOString(),
|
|
410
|
+
unit: "timestamp",
|
|
411
|
+
description: "PostgreSQL server start time",
|
|
412
|
+
};
|
|
413
|
+
info.uptime = {
|
|
414
|
+
value: uptime.uptime,
|
|
415
|
+
unit: "interval",
|
|
416
|
+
description: "Server uptime",
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return info;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Get invalid indexes (H001)
|
|
425
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (pg_invalid_indexes)
|
|
426
|
+
*/
|
|
427
|
+
export async function getInvalidIndexes(client: Client): Promise<InvalidIndex[]> {
|
|
428
|
+
const sql = getMetricSql(METRIC_NAMES.H001);
|
|
429
|
+
const result = await client.query(sql);
|
|
430
|
+
return result.rows.map((row) => {
|
|
431
|
+
const transformed = transformMetricRow(row);
|
|
432
|
+
const indexSizeBytes = parseInt(String(transformed.index_size_bytes || 0), 10);
|
|
433
|
+
return {
|
|
434
|
+
schema_name: String(transformed.schema_name || ""),
|
|
435
|
+
table_name: String(transformed.table_name || ""),
|
|
436
|
+
index_name: String(transformed.index_name || ""),
|
|
437
|
+
relation_name: String(transformed.relation_name || ""),
|
|
438
|
+
index_size_bytes: indexSizeBytes,
|
|
439
|
+
index_size_pretty: formatBytes(indexSizeBytes),
|
|
440
|
+
supports_fk: transformed.supports_fk === true || transformed.supports_fk === 1,
|
|
441
|
+
};
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Get unused indexes (H002)
|
|
447
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (unused_indexes)
|
|
448
|
+
*/
|
|
449
|
+
export async function getUnusedIndexes(client: Client): Promise<UnusedIndex[]> {
|
|
450
|
+
const sql = getMetricSql(METRIC_NAMES.H002);
|
|
451
|
+
const result = await client.query(sql);
|
|
452
|
+
return result.rows.map((row) => {
|
|
453
|
+
const transformed = transformMetricRow(row);
|
|
454
|
+
const indexSizeBytes = parseInt(String(transformed.index_size_bytes || 0), 10);
|
|
455
|
+
return {
|
|
456
|
+
schema_name: String(transformed.schema_name || ""),
|
|
457
|
+
table_name: String(transformed.table_name || ""),
|
|
458
|
+
index_name: String(transformed.index_name || ""),
|
|
459
|
+
index_definition: String(transformed.index_definition || ""),
|
|
460
|
+
reason: String(transformed.reason || ""),
|
|
461
|
+
idx_scan: parseInt(String(transformed.idx_scan || 0), 10),
|
|
462
|
+
index_size_bytes: indexSizeBytes,
|
|
463
|
+
idx_is_btree: transformed.idx_is_btree === true || transformed.idx_is_btree === "t",
|
|
464
|
+
supports_fk: transformed.supports_fk === true || transformed.supports_fk === 1,
|
|
465
|
+
index_size_pretty: formatBytes(indexSizeBytes),
|
|
466
|
+
};
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Get stats reset info (H002)
|
|
472
|
+
*/
|
|
473
|
+
/**
|
|
474
|
+
* Get stats reset info (H002)
|
|
475
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (express_stats_reset)
|
|
476
|
+
*/
|
|
477
|
+
export async function getStatsReset(client: Client): Promise<StatsReset> {
|
|
478
|
+
const sql = getMetricSql(METRIC_NAMES.statsReset);
|
|
479
|
+
const result = await client.query(sql);
|
|
480
|
+
const row = result.rows[0] || {};
|
|
481
|
+
return {
|
|
482
|
+
stats_reset_epoch: row.stats_reset_epoch ? parseFloat(row.stats_reset_epoch) : null,
|
|
483
|
+
stats_reset_time: row.stats_reset_time || null,
|
|
484
|
+
days_since_reset: row.days_since_reset ? parseInt(row.days_since_reset, 10) : null,
|
|
485
|
+
postmaster_startup_epoch: row.postmaster_startup_epoch ? parseFloat(row.postmaster_startup_epoch) : null,
|
|
486
|
+
postmaster_startup_time: row.postmaster_startup_time || null,
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* Get current database name and size
|
|
492
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (express_current_database)
|
|
493
|
+
*/
|
|
494
|
+
export async function getCurrentDatabaseInfo(client: Client): Promise<{ datname: string; size_bytes: number }> {
|
|
495
|
+
const sql = getMetricSql(METRIC_NAMES.currentDatabase);
|
|
496
|
+
const result = await client.query(sql);
|
|
497
|
+
const row = result.rows[0] || {};
|
|
498
|
+
return {
|
|
499
|
+
datname: row.datname || "postgres",
|
|
500
|
+
size_bytes: parseInt(row.size_bytes, 10) || 0,
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Get redundant indexes (H004)
|
|
506
|
+
* SQL loaded from config/pgwatch-prometheus/metrics.yml (redundant_indexes)
|
|
507
|
+
*/
|
|
508
|
+
export async function getRedundantIndexes(client: Client): Promise<RedundantIndex[]> {
|
|
509
|
+
const sql = getMetricSql(METRIC_NAMES.H004);
|
|
510
|
+
const result = await client.query(sql);
|
|
511
|
+
return result.rows.map((row) => {
|
|
512
|
+
const transformed = transformMetricRow(row);
|
|
513
|
+
const indexSizeBytes = parseInt(String(transformed.index_size_bytes || 0), 10);
|
|
514
|
+
const tableSizeBytes = parseInt(String(transformed.table_size_bytes || 0), 10);
|
|
515
|
+
return {
|
|
516
|
+
schema_name: String(transformed.schema_name || ""),
|
|
517
|
+
table_name: String(transformed.table_name || ""),
|
|
518
|
+
index_name: String(transformed.index_name || ""),
|
|
519
|
+
relation_name: String(transformed.relation_name || ""),
|
|
520
|
+
access_method: String(transformed.access_method || ""),
|
|
521
|
+
reason: String(transformed.reason || ""),
|
|
522
|
+
index_size_bytes: indexSizeBytes,
|
|
523
|
+
table_size_bytes: tableSizeBytes,
|
|
524
|
+
index_usage: parseInt(String(transformed.index_usage || 0), 10),
|
|
525
|
+
supports_fk: transformed.supports_fk === true || transformed.supports_fk === 1,
|
|
526
|
+
index_definition: String(transformed.index_definition || ""),
|
|
527
|
+
index_size_pretty: formatBytes(indexSizeBytes),
|
|
528
|
+
table_size_pretty: formatBytes(tableSizeBytes),
|
|
529
|
+
};
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Create base report structure
|
|
535
|
+
*/
|
|
536
|
+
export function createBaseReport(
|
|
537
|
+
checkId: string,
|
|
538
|
+
checkTitle: string,
|
|
539
|
+
nodeName: string
|
|
540
|
+
): Report {
|
|
541
|
+
const buildTs = resolveBuildTs();
|
|
542
|
+
return {
|
|
543
|
+
version: pkg.version || null,
|
|
544
|
+
build_ts: buildTs,
|
|
545
|
+
checkId,
|
|
546
|
+
checkTitle,
|
|
547
|
+
timestamptz: new Date().toISOString(),
|
|
548
|
+
nodes: {
|
|
549
|
+
primary: nodeName,
|
|
550
|
+
standbys: [],
|
|
551
|
+
},
|
|
552
|
+
results: {},
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function readTextFileSafe(p: string): string | null {
|
|
557
|
+
try {
|
|
558
|
+
const value = fs.readFileSync(p, "utf8").trim();
|
|
559
|
+
return value || null;
|
|
560
|
+
} catch {
|
|
561
|
+
return null;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function resolveBuildTs(): string | null {
|
|
566
|
+
// Follow reporter.py approach: read BUILD_TS from filesystem, with env override.
|
|
567
|
+
// Default: /BUILD_TS (useful in container images).
|
|
568
|
+
const envPath = process.env.PGAI_BUILD_TS_FILE;
|
|
569
|
+
const p = (envPath && envPath.trim()) ? envPath.trim() : "/BUILD_TS";
|
|
570
|
+
|
|
571
|
+
const fromFile = readTextFileSafe(p);
|
|
572
|
+
if (fromFile) return fromFile;
|
|
573
|
+
|
|
574
|
+
// Fallback for packaged CLI: allow placing BUILD_TS next to dist/ (package root).
|
|
575
|
+
// dist/lib/checkup.js => package root: dist/..
|
|
576
|
+
try {
|
|
577
|
+
const pkgRoot = path.resolve(__dirname, "..");
|
|
578
|
+
const fromPkgFile = readTextFileSafe(path.join(pkgRoot, "BUILD_TS"));
|
|
579
|
+
if (fromPkgFile) return fromPkgFile;
|
|
580
|
+
} catch {
|
|
581
|
+
// ignore
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// Last resort: use package.json mtime as an approximation (non-null, stable-ish).
|
|
585
|
+
try {
|
|
586
|
+
const pkgJsonPath = path.resolve(__dirname, "..", "package.json");
|
|
587
|
+
const st = fs.statSync(pkgJsonPath);
|
|
588
|
+
return st.mtime.toISOString();
|
|
589
|
+
} catch {
|
|
590
|
+
return new Date().toISOString();
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Generate A002 report - Postgres major version
|
|
596
|
+
*/
|
|
597
|
+
export async function generateA002(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
598
|
+
const report = createBaseReport("A002", "Postgres major version", nodeName);
|
|
599
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
600
|
+
|
|
601
|
+
report.results[nodeName] = {
|
|
602
|
+
data: {
|
|
603
|
+
version: postgresVersion,
|
|
604
|
+
},
|
|
605
|
+
};
|
|
606
|
+
|
|
607
|
+
return report;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Generate A003 report - Postgres settings
|
|
612
|
+
*/
|
|
613
|
+
export async function generateA003(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
614
|
+
const report = createBaseReport("A003", "Postgres settings", nodeName);
|
|
615
|
+
const settings = await getSettings(client);
|
|
616
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
617
|
+
|
|
618
|
+
report.results[nodeName] = {
|
|
619
|
+
data: settings,
|
|
620
|
+
postgres_version: postgresVersion,
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
return report;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
/**
|
|
627
|
+
* Generate A004 report - Cluster information
|
|
628
|
+
*/
|
|
629
|
+
export async function generateA004(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
630
|
+
const report = createBaseReport("A004", "Cluster information", nodeName);
|
|
631
|
+
const generalInfo = await getClusterInfo(client);
|
|
632
|
+
const databaseSizes = await getDatabaseSizes(client);
|
|
633
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
634
|
+
|
|
635
|
+
report.results[nodeName] = {
|
|
636
|
+
data: {
|
|
637
|
+
general_info: generalInfo,
|
|
638
|
+
database_sizes: databaseSizes,
|
|
639
|
+
},
|
|
640
|
+
postgres_version: postgresVersion,
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
return report;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
/**
|
|
647
|
+
* Generate A007 report - Altered settings
|
|
648
|
+
*/
|
|
649
|
+
export async function generateA007(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
650
|
+
const report = createBaseReport("A007", "Altered settings", nodeName);
|
|
651
|
+
const alteredSettings = await getAlteredSettings(client);
|
|
652
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
653
|
+
|
|
654
|
+
report.results[nodeName] = {
|
|
655
|
+
data: alteredSettings,
|
|
656
|
+
postgres_version: postgresVersion,
|
|
657
|
+
};
|
|
658
|
+
|
|
659
|
+
return report;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/**
|
|
663
|
+
* Generate A013 report - Postgres minor version
|
|
664
|
+
*/
|
|
665
|
+
export async function generateA013(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
666
|
+
const report = createBaseReport("A013", "Postgres minor version", nodeName);
|
|
667
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
668
|
+
|
|
669
|
+
report.results[nodeName] = {
|
|
670
|
+
data: {
|
|
671
|
+
version: postgresVersion,
|
|
672
|
+
},
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
return report;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
/**
|
|
679
|
+
* Generate H001 report - Invalid indexes
|
|
680
|
+
*/
|
|
681
|
+
export async function generateH001(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
682
|
+
const report = createBaseReport("H001", "Invalid indexes", nodeName);
|
|
683
|
+
const invalidIndexes = await getInvalidIndexes(client);
|
|
684
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
685
|
+
|
|
686
|
+
// Get current database name and size
|
|
687
|
+
const { datname: dbName, size_bytes: dbSizeBytes } = await getCurrentDatabaseInfo(client);
|
|
688
|
+
|
|
689
|
+
// Calculate totals
|
|
690
|
+
const totalCount = invalidIndexes.length;
|
|
691
|
+
const totalSizeBytes = invalidIndexes.reduce((sum, idx) => sum + idx.index_size_bytes, 0);
|
|
692
|
+
|
|
693
|
+
// Structure data by database name per schema
|
|
694
|
+
report.results[nodeName] = {
|
|
695
|
+
data: {
|
|
696
|
+
[dbName]: {
|
|
697
|
+
invalid_indexes: invalidIndexes,
|
|
698
|
+
total_count: totalCount,
|
|
699
|
+
total_size_bytes: totalSizeBytes,
|
|
700
|
+
total_size_pretty: formatBytes(totalSizeBytes),
|
|
701
|
+
database_size_bytes: dbSizeBytes,
|
|
702
|
+
database_size_pretty: formatBytes(dbSizeBytes),
|
|
703
|
+
},
|
|
704
|
+
},
|
|
705
|
+
postgres_version: postgresVersion,
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
return report;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Generate H002 report - Unused indexes
|
|
713
|
+
*/
|
|
714
|
+
export async function generateH002(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
715
|
+
const report = createBaseReport("H002", "Unused indexes", nodeName);
|
|
716
|
+
const unusedIndexes = await getUnusedIndexes(client);
|
|
717
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
718
|
+
const statsReset = await getStatsReset(client);
|
|
719
|
+
|
|
720
|
+
// Get current database name and size
|
|
721
|
+
const { datname: dbName, size_bytes: dbSizeBytes } = await getCurrentDatabaseInfo(client);
|
|
722
|
+
|
|
723
|
+
// Calculate totals
|
|
724
|
+
const totalCount = unusedIndexes.length;
|
|
725
|
+
const totalSizeBytes = unusedIndexes.reduce((sum, idx) => sum + idx.index_size_bytes, 0);
|
|
726
|
+
|
|
727
|
+
// Structure data by database name per schema
|
|
728
|
+
report.results[nodeName] = {
|
|
729
|
+
data: {
|
|
730
|
+
[dbName]: {
|
|
731
|
+
unused_indexes: unusedIndexes,
|
|
732
|
+
total_count: totalCount,
|
|
733
|
+
total_size_bytes: totalSizeBytes,
|
|
734
|
+
total_size_pretty: formatBytes(totalSizeBytes),
|
|
735
|
+
database_size_bytes: dbSizeBytes,
|
|
736
|
+
database_size_pretty: formatBytes(dbSizeBytes),
|
|
737
|
+
stats_reset: statsReset,
|
|
738
|
+
},
|
|
739
|
+
},
|
|
740
|
+
postgres_version: postgresVersion,
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
return report;
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
/**
|
|
747
|
+
* Generate H004 report - Redundant indexes
|
|
748
|
+
*/
|
|
749
|
+
export async function generateH004(client: Client, nodeName: string = "node-01"): Promise<Report> {
|
|
750
|
+
const report = createBaseReport("H004", "Redundant indexes", nodeName);
|
|
751
|
+
const redundantIndexes = await getRedundantIndexes(client);
|
|
752
|
+
const postgresVersion = await getPostgresVersion(client);
|
|
753
|
+
|
|
754
|
+
// Get current database name and size
|
|
755
|
+
const { datname: dbName, size_bytes: dbSizeBytes } = await getCurrentDatabaseInfo(client);
|
|
756
|
+
|
|
757
|
+
// Calculate totals
|
|
758
|
+
const totalCount = redundantIndexes.length;
|
|
759
|
+
const totalSizeBytes = redundantIndexes.reduce((sum, idx) => sum + idx.index_size_bytes, 0);
|
|
760
|
+
|
|
761
|
+
// Structure data by database name per schema
|
|
762
|
+
report.results[nodeName] = {
|
|
763
|
+
data: {
|
|
764
|
+
[dbName]: {
|
|
765
|
+
redundant_indexes: redundantIndexes,
|
|
766
|
+
total_count: totalCount,
|
|
767
|
+
total_size_bytes: totalSizeBytes,
|
|
768
|
+
total_size_pretty: formatBytes(totalSizeBytes),
|
|
769
|
+
database_size_bytes: dbSizeBytes,
|
|
770
|
+
database_size_pretty: formatBytes(dbSizeBytes),
|
|
771
|
+
},
|
|
772
|
+
},
|
|
773
|
+
postgres_version: postgresVersion,
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
return report;
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
/**
|
|
780
|
+
* Available report generators
|
|
781
|
+
*/
|
|
782
|
+
export const REPORT_GENERATORS: Record<string, (client: Client, nodeName: string) => Promise<Report>> = {
|
|
783
|
+
A002: generateA002,
|
|
784
|
+
A003: generateA003,
|
|
785
|
+
A004: generateA004,
|
|
786
|
+
A007: generateA007,
|
|
787
|
+
A013: generateA013,
|
|
788
|
+
H001: generateH001,
|
|
789
|
+
H002: generateH002,
|
|
790
|
+
H004: generateH004,
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* Check IDs and titles
|
|
795
|
+
*/
|
|
796
|
+
export const CHECK_INFO: Record<string, string> = {
|
|
797
|
+
A002: "Postgres major version",
|
|
798
|
+
A003: "Postgres settings",
|
|
799
|
+
A004: "Cluster information",
|
|
800
|
+
A007: "Altered settings",
|
|
801
|
+
A013: "Postgres minor version",
|
|
802
|
+
H001: "Invalid indexes",
|
|
803
|
+
H002: "Unused indexes",
|
|
804
|
+
H004: "Redundant indexes",
|
|
805
|
+
};
|
|
806
|
+
|
|
807
|
+
/**
|
|
808
|
+
* Generate all available reports
|
|
809
|
+
*/
|
|
810
|
+
export async function generateAllReports(
|
|
811
|
+
client: Client,
|
|
812
|
+
nodeName: string = "node-01",
|
|
813
|
+
onProgress?: (info: { checkId: string; checkTitle: string; index: number; total: number }) => void
|
|
814
|
+
): Promise<Record<string, Report>> {
|
|
815
|
+
const reports: Record<string, Report> = {};
|
|
816
|
+
|
|
817
|
+
const entries = Object.entries(REPORT_GENERATORS);
|
|
818
|
+
const total = entries.length;
|
|
819
|
+
let index = 0;
|
|
820
|
+
|
|
821
|
+
for (const [checkId, generator] of entries) {
|
|
822
|
+
index += 1;
|
|
823
|
+
onProgress?.({
|
|
824
|
+
checkId,
|
|
825
|
+
checkTitle: CHECK_INFO[checkId] || checkId,
|
|
826
|
+
index,
|
|
827
|
+
total,
|
|
828
|
+
});
|
|
829
|
+
reports[checkId] = await generator(client, nodeName);
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
return reports;
|
|
833
|
+
}
|