postgresai 0.14.0-dev.7 → 0.14.0-dev.70

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.
Files changed (82) hide show
  1. package/README.md +161 -61
  2. package/bin/postgres-ai.ts +1957 -404
  3. package/bun.lock +258 -0
  4. package/bunfig.toml +20 -0
  5. package/dist/bin/postgres-ai.js +29351 -1576
  6. package/dist/sql/01.role.sql +16 -0
  7. package/dist/sql/02.permissions.sql +37 -0
  8. package/dist/sql/03.optional_rds.sql +6 -0
  9. package/dist/sql/04.optional_self_managed.sql +8 -0
  10. package/dist/sql/05.helpers.sql +439 -0
  11. package/dist/sql/sql/01.role.sql +16 -0
  12. package/dist/sql/sql/02.permissions.sql +37 -0
  13. package/dist/sql/sql/03.optional_rds.sql +6 -0
  14. package/dist/sql/sql/04.optional_self_managed.sql +8 -0
  15. package/dist/sql/sql/05.helpers.sql +439 -0
  16. package/lib/auth-server.ts +124 -106
  17. package/lib/checkup-api.ts +386 -0
  18. package/lib/checkup.ts +1396 -0
  19. package/lib/config.ts +6 -3
  20. package/lib/init.ts +512 -156
  21. package/lib/issues.ts +400 -191
  22. package/lib/mcp-server.ts +213 -90
  23. package/lib/metrics-embedded.ts +79 -0
  24. package/lib/metrics-loader.ts +127 -0
  25. package/lib/supabase.ts +769 -0
  26. package/lib/util.ts +61 -0
  27. package/package.json +20 -10
  28. package/packages/postgres-ai/README.md +26 -0
  29. package/packages/postgres-ai/bin/postgres-ai.js +27 -0
  30. package/packages/postgres-ai/package.json +27 -0
  31. package/scripts/embed-metrics.ts +154 -0
  32. package/sql/01.role.sql +16 -0
  33. package/sql/02.permissions.sql +37 -0
  34. package/sql/03.optional_rds.sql +6 -0
  35. package/sql/04.optional_self_managed.sql +8 -0
  36. package/sql/05.helpers.sql +439 -0
  37. package/test/auth.test.ts +258 -0
  38. package/test/checkup.integration.test.ts +321 -0
  39. package/test/checkup.test.ts +1117 -0
  40. package/test/init.integration.test.ts +500 -0
  41. package/test/init.test.ts +527 -0
  42. package/test/issues.cli.test.ts +314 -0
  43. package/test/issues.test.ts +456 -0
  44. package/test/mcp-server.test.ts +988 -0
  45. package/test/schema-validation.test.ts +81 -0
  46. package/test/supabase.test.ts +568 -0
  47. package/test/test-utils.ts +128 -0
  48. package/tsconfig.json +12 -20
  49. package/dist/bin/postgres-ai.d.ts +0 -3
  50. package/dist/bin/postgres-ai.d.ts.map +0 -1
  51. package/dist/bin/postgres-ai.js.map +0 -1
  52. package/dist/lib/auth-server.d.ts +0 -31
  53. package/dist/lib/auth-server.d.ts.map +0 -1
  54. package/dist/lib/auth-server.js +0 -263
  55. package/dist/lib/auth-server.js.map +0 -1
  56. package/dist/lib/config.d.ts +0 -45
  57. package/dist/lib/config.d.ts.map +0 -1
  58. package/dist/lib/config.js +0 -181
  59. package/dist/lib/config.js.map +0 -1
  60. package/dist/lib/init.d.ts +0 -61
  61. package/dist/lib/init.d.ts.map +0 -1
  62. package/dist/lib/init.js +0 -359
  63. package/dist/lib/init.js.map +0 -1
  64. package/dist/lib/issues.d.ts +0 -75
  65. package/dist/lib/issues.d.ts.map +0 -1
  66. package/dist/lib/issues.js +0 -336
  67. package/dist/lib/issues.js.map +0 -1
  68. package/dist/lib/mcp-server.d.ts +0 -9
  69. package/dist/lib/mcp-server.d.ts.map +0 -1
  70. package/dist/lib/mcp-server.js +0 -168
  71. package/dist/lib/mcp-server.js.map +0 -1
  72. package/dist/lib/pkce.d.ts +0 -32
  73. package/dist/lib/pkce.d.ts.map +0 -1
  74. package/dist/lib/pkce.js +0 -101
  75. package/dist/lib/pkce.js.map +0 -1
  76. package/dist/lib/util.d.ts +0 -27
  77. package/dist/lib/util.d.ts.map +0 -1
  78. package/dist/lib/util.js +0 -46
  79. package/dist/lib/util.js.map +0 -1
  80. package/dist/package.json +0 -46
  81. package/test/init.integration.test.cjs +0 -269
  82. package/test/init.test.cjs +0 -69
package/lib/checkup.ts ADDED
@@ -0,0 +1,1396 @@
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
+ * Complex metrics (index health, settings, db_stats) are loaded from
11
+ * config/pgwatch-prometheus/metrics.yml via getMetricSql() from metrics-loader.ts.
12
+ *
13
+ * Simple queries (version, database list, connection states, uptime) use
14
+ * inline SQL as they're trivial and CLI-specific.
15
+ *
16
+ * 2. JSON SCHEMA COMPLIANCE
17
+ * All generated reports MUST comply with JSON schemas in reporter/schemas/.
18
+ * These schemas define the expected format for both:
19
+ * - Full-fledged monitoring reporter output
20
+ * - Express checkup output
21
+ *
22
+ * Before adding or modifying a report, verify the corresponding schema exists
23
+ * and ensure the output matches. Run schema validation tests to confirm.
24
+ *
25
+ * 3. ERROR HANDLING STRATEGY
26
+ * Functions follow two patterns based on criticality:
27
+ *
28
+ * PROPAGATING (throws on error):
29
+ * - Core data functions: getPostgresVersion, getSettings, getAlteredSettings,
30
+ * getDatabaseSizes, getInvalidIndexes, getUnusedIndexes, getRedundantIndexes
31
+ * - If these fail, the entire report should fail (data is required)
32
+ * - Callers should handle errors at the report generation level
33
+ *
34
+ * GRACEFUL DEGRADATION (catches errors, includes error in output):
35
+ * - Optional/supplementary queries: pg_stat_statements, pg_stat_kcache checks,
36
+ * memory calculations, postmaster startup time
37
+ * - These are nice-to-have; missing data shouldn't fail the whole report
38
+ * - Errors are logged and included in report output for visibility
39
+ *
40
+ * ADDING NEW REPORTS
41
+ * ------------------
42
+ * 1. Add/verify the metric exists in config/pgwatch-prometheus/metrics.yml
43
+ * 2. Add the metric name mapping to METRIC_NAMES in metrics-loader.ts
44
+ * 3. Verify JSON schema exists in reporter/schemas/{CHECK_ID}.schema.json
45
+ * 4. Implement the generator function using getMetricSql()
46
+ * 5. Add schema validation test in test/schema-validation.test.ts
47
+ */
48
+
49
+ import { Client } from "pg";
50
+ import * as fs from "fs";
51
+ import * as path from "path";
52
+ import * as pkg from "../package.json";
53
+ import { getMetricSql, transformMetricRow, METRIC_NAMES } from "./metrics-loader";
54
+
55
+ // Time constants
56
+ const SECONDS_PER_DAY = 86400;
57
+ const SECONDS_PER_HOUR = 3600;
58
+ const SECONDS_PER_MINUTE = 60;
59
+
60
+ /**
61
+ * Convert various boolean representations to boolean.
62
+ * PostgreSQL returns booleans as true/false, 1/0, 't'/'f', or 'true'/'false'
63
+ * depending on context (query result, JDBC driver, etc.).
64
+ */
65
+ function toBool(val: unknown): boolean {
66
+ return val === true || val === 1 || val === "t" || val === "true";
67
+ }
68
+
69
+ /**
70
+ * PostgreSQL version information
71
+ */
72
+ export interface PostgresVersion {
73
+ version: string;
74
+ server_version_num: string;
75
+ server_major_ver: string;
76
+ server_minor_ver: string;
77
+ }
78
+
79
+ /**
80
+ * Setting information from pg_settings
81
+ */
82
+ export interface SettingInfo {
83
+ setting: string;
84
+ unit: string;
85
+ category: string;
86
+ context: string;
87
+ vartype: string;
88
+ pretty_value: string;
89
+ }
90
+
91
+ /**
92
+ * Altered setting (A007) - subset of SettingInfo
93
+ */
94
+ export interface AlteredSetting {
95
+ value: string;
96
+ unit: string;
97
+ category: string;
98
+ pretty_value: string;
99
+ }
100
+
101
+ /**
102
+ * Cluster metric (A004)
103
+ */
104
+ export interface ClusterMetric {
105
+ value: string;
106
+ unit: string;
107
+ description: string;
108
+ }
109
+
110
+ /**
111
+ * Invalid index entry (H001) - matches H001.schema.json invalidIndex
112
+ *
113
+ * Decision tree for remediation recommendations:
114
+ * 1. has_valid_duplicate=true → DROP (valid duplicate exists, safe to remove)
115
+ * 2. is_pk=true or is_unique=true → RECREATE (backs a constraint, must restore)
116
+ * 3. table_row_estimate < 10000 → RECREATE (small table, quick rebuild)
117
+ * 4. Otherwise → UNCERTAIN (needs manual analysis of query plans)
118
+ */
119
+ export interface InvalidIndex {
120
+ schema_name: string;
121
+ table_name: string;
122
+ index_name: string;
123
+ relation_name: string;
124
+ index_size_bytes: number;
125
+ index_size_pretty: string;
126
+ /** Full CREATE INDEX statement from pg_get_indexdef() - useful for DROP/RECREATE migrations */
127
+ index_definition: string;
128
+ supports_fk: boolean;
129
+ /** True if this index backs a PRIMARY KEY constraint */
130
+ is_pk: boolean;
131
+ /** True if this is a UNIQUE index (includes PK indexes) */
132
+ is_unique: boolean;
133
+ /** Name of the constraint this index backs, or null if none */
134
+ constraint_name: string | null;
135
+ /** Estimated row count of the table from pg_class.reltuples */
136
+ table_row_estimate: number;
137
+ /** True if there is a valid index on the same column(s) */
138
+ has_valid_duplicate: boolean;
139
+ /** Name of the valid duplicate index if one exists */
140
+ valid_duplicate_name: string | null;
141
+ /** Full CREATE INDEX statement of the valid duplicate index */
142
+ valid_duplicate_definition: string | null;
143
+ }
144
+
145
+ /** Recommendation for handling an invalid index */
146
+ export type InvalidIndexRecommendation = "DROP" | "RECREATE" | "UNCERTAIN";
147
+
148
+ /** Threshold for considering a table "small" (quick to rebuild) */
149
+ const SMALL_TABLE_ROW_THRESHOLD = 10000;
150
+
151
+ /**
152
+ * Compute remediation recommendation for an invalid index using decision tree.
153
+ *
154
+ * Decision tree logic:
155
+ * 1. If has_valid_duplicate is true → DROP (valid duplicate exists, safe to remove)
156
+ * 2. If is_pk or is_unique is true → RECREATE (backs a constraint, must restore)
157
+ * 3. If table_row_estimate < 10000 → RECREATE (small table, quick rebuild)
158
+ * 4. Otherwise → UNCERTAIN (needs manual analysis of query plans)
159
+ *
160
+ * @param index - Invalid index with observation data
161
+ * @returns Recommendation: "DROP", "RECREATE", or "UNCERTAIN"
162
+ */
163
+ export function getInvalidIndexRecommendation(index: InvalidIndex): InvalidIndexRecommendation {
164
+ // 1. Valid duplicate exists - safe to drop
165
+ if (index.has_valid_duplicate) {
166
+ return "DROP";
167
+ }
168
+
169
+ // 2. Backs a constraint - must recreate
170
+ if (index.is_pk || index.is_unique) {
171
+ return "RECREATE";
172
+ }
173
+
174
+ // 3. Small table - quick to recreate
175
+ if (index.table_row_estimate < SMALL_TABLE_ROW_THRESHOLD) {
176
+ return "RECREATE";
177
+ }
178
+
179
+ // 4. Large table without clear path - needs manual analysis
180
+ return "UNCERTAIN";
181
+ }
182
+
183
+ /**
184
+ * Unused index entry (H002) - matches H002.schema.json unusedIndex
185
+ */
186
+ export interface UnusedIndex {
187
+ schema_name: string;
188
+ table_name: string;
189
+ index_name: string;
190
+ index_definition: string;
191
+ reason: string;
192
+ idx_scan: number;
193
+ index_size_bytes: number;
194
+ idx_is_btree: boolean;
195
+ supports_fk: boolean;
196
+ index_size_pretty: string;
197
+ }
198
+
199
+ /**
200
+ * Stats reset info for H002 - matches H002.schema.json statsReset
201
+ */
202
+ export interface StatsReset {
203
+ stats_reset_epoch: number | null;
204
+ stats_reset_time: string | null;
205
+ days_since_reset: number | null;
206
+ postmaster_startup_epoch: number | null;
207
+ postmaster_startup_time: string | null;
208
+ /** Set when postmaster startup time query fails - indicates data availability issue */
209
+ postmaster_startup_error?: string;
210
+ }
211
+
212
+ /**
213
+ * Redundant index entry (H004) - matches H004.schema.json redundantIndex
214
+ */
215
+ /**
216
+ * Index that makes another index redundant.
217
+ * Used in redundant_to array to show which indexes this one is redundant to.
218
+ */
219
+ export interface RedundantToIndex {
220
+ index_name: string;
221
+ index_definition: string;
222
+ index_size_bytes: number;
223
+ index_size_pretty: string;
224
+ }
225
+
226
+ export interface RedundantIndex {
227
+ schema_name: string;
228
+ table_name: string;
229
+ index_name: string;
230
+ relation_name: string;
231
+ access_method: string;
232
+ reason: string;
233
+ index_size_bytes: number;
234
+ table_size_bytes: number;
235
+ index_usage: number;
236
+ supports_fk: boolean;
237
+ index_definition: string;
238
+ index_size_pretty: string;
239
+ table_size_pretty: string;
240
+ redundant_to: RedundantToIndex[];
241
+ /** Set when redundant_to_json parsing fails - indicates data quality issue */
242
+ redundant_to_parse_error?: string;
243
+ }
244
+
245
+ /**
246
+ * Node result for reports
247
+ */
248
+ export interface NodeResult {
249
+ data: Record<string, any>;
250
+ postgres_version?: PostgresVersion;
251
+ }
252
+
253
+ /**
254
+ * Report structure matching JSON schemas
255
+ */
256
+ export interface Report {
257
+ version: string | null;
258
+ build_ts: string | null;
259
+ generation_mode: string | null;
260
+ checkId: string;
261
+ checkTitle: string;
262
+ timestamptz: string;
263
+ nodes: {
264
+ primary: string;
265
+ standbys: string[];
266
+ };
267
+ results: Record<string, NodeResult>;
268
+ }
269
+
270
+ /**
271
+ * Parse PostgreSQL version number into major and minor components
272
+ */
273
+ export function parseVersionNum(versionNum: string): { major: string; minor: string } {
274
+ if (!versionNum || versionNum.length < 6) {
275
+ return { major: "", minor: "" };
276
+ }
277
+ try {
278
+ const num = parseInt(versionNum, 10);
279
+ return {
280
+ major: Math.floor(num / 10000).toString(),
281
+ minor: (num % 10000).toString(),
282
+ };
283
+ } catch (err) {
284
+ // parseInt shouldn't throw, but handle edge cases defensively
285
+ const errorMsg = err instanceof Error ? err.message : String(err);
286
+ console.log(`[parseVersionNum] Warning: Failed to parse "${versionNum}": ${errorMsg}`);
287
+ return { major: "", minor: "" };
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Format bytes to human readable string using binary units (1024-based).
293
+ * Uses IEC standard: KiB, MiB, GiB, etc.
294
+ *
295
+ * Note: PostgreSQL's pg_size_pretty() uses kB/MB/GB with 1024 base (technically
296
+ * incorrect SI usage), but we follow IEC binary units per project style guide.
297
+ */
298
+ export function formatBytes(bytes: number): string {
299
+ if (bytes === 0) return "0 B";
300
+ if (bytes < 0) return `-${formatBytes(-bytes)}`; // Handle negative values
301
+ if (!Number.isFinite(bytes)) return `${bytes} B`; // Handle NaN/Infinity
302
+ const units = ["B", "KiB", "MiB", "GiB", "TiB", "PiB"];
303
+ const i = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1);
304
+ return `${(bytes / Math.pow(1024, i)).toFixed(2)} ${units[i]}`;
305
+ }
306
+
307
+ /**
308
+ * Format a setting's pretty value from the normalized value and unit.
309
+ * The settings metric provides setting_normalized (bytes or seconds) and unit_normalized.
310
+ */
311
+ function formatSettingPrettyValue(
312
+ settingNormalized: number | null,
313
+ unitNormalized: string | null,
314
+ rawValue: string
315
+ ): string {
316
+ if (settingNormalized === null || unitNormalized === null) {
317
+ return rawValue;
318
+ }
319
+
320
+ if (unitNormalized === "bytes") {
321
+ return formatBytes(settingNormalized);
322
+ }
323
+
324
+ if (unitNormalized === "seconds") {
325
+ // Format time values with appropriate units based on magnitude:
326
+ // - Sub-second values (< 1s): show in milliseconds for precision
327
+ // - Small values (< 60s): show in seconds
328
+ // - Larger values (>= 60s): show in minutes for readability
329
+ const MS_PER_SECOND = 1000;
330
+ if (settingNormalized < 1) {
331
+ return `${(settingNormalized * MS_PER_SECOND).toFixed(0)} ms`;
332
+ } else if (settingNormalized < SECONDS_PER_MINUTE) {
333
+ return `${settingNormalized} s`;
334
+ } else {
335
+ return `${(settingNormalized / SECONDS_PER_MINUTE).toFixed(1)} min`;
336
+ }
337
+ }
338
+
339
+ return rawValue;
340
+ }
341
+
342
+ /**
343
+ * Get PostgreSQL version information.
344
+ * Uses simple inline SQL (trivial query, CLI-specific).
345
+ *
346
+ * @throws {Error} If database query fails (propagating - critical data)
347
+ */
348
+ export async function getPostgresVersion(client: Client): Promise<PostgresVersion> {
349
+ const result = await client.query(`
350
+ select name, setting
351
+ from pg_settings
352
+ where name in ('server_version', 'server_version_num')
353
+ `);
354
+
355
+ let version = "";
356
+ let serverVersionNum = "";
357
+
358
+ for (const row of result.rows) {
359
+ if (row.name === "server_version") {
360
+ version = row.setting;
361
+ } else if (row.name === "server_version_num") {
362
+ serverVersionNum = row.setting;
363
+ }
364
+ }
365
+
366
+ const { major, minor } = parseVersionNum(serverVersionNum);
367
+
368
+ return {
369
+ version,
370
+ server_version_num: serverVersionNum,
371
+ server_major_ver: major,
372
+ server_minor_ver: minor,
373
+ };
374
+ }
375
+
376
+ /**
377
+ * Get all PostgreSQL settings
378
+ * Uses 'settings' metric from metrics.yml
379
+ */
380
+ export async function getSettings(client: Client, pgMajorVersion: number = 16): Promise<Record<string, SettingInfo>> {
381
+ const sql = getMetricSql(METRIC_NAMES.settings, pgMajorVersion);
382
+ const result = await client.query(sql);
383
+ const settings: Record<string, SettingInfo> = {};
384
+
385
+ for (const row of result.rows) {
386
+ // The settings metric uses tag_setting_name, tag_setting_value, etc.
387
+ const name = row.tag_setting_name;
388
+ const settingValue = row.tag_setting_value;
389
+ const unit = row.tag_unit || "";
390
+ const category = row.tag_category || "";
391
+ const vartype = row.tag_vartype || "";
392
+ const settingNormalized = row.setting_normalized !== null ? parseFloat(row.setting_normalized) : null;
393
+ const unitNormalized = row.unit_normalized || null;
394
+
395
+ settings[name] = {
396
+ setting: settingValue,
397
+ unit,
398
+ category,
399
+ context: "", // Not available in the monitoring metric
400
+ vartype,
401
+ pretty_value: formatSettingPrettyValue(settingNormalized, unitNormalized, settingValue),
402
+ };
403
+ }
404
+
405
+ return settings;
406
+ }
407
+
408
+ /**
409
+ * Get altered (non-default) PostgreSQL settings
410
+ * Uses 'settings' metric from metrics.yml and filters for non-default
411
+ */
412
+ export async function getAlteredSettings(client: Client, pgMajorVersion: number = 16): Promise<Record<string, AlteredSetting>> {
413
+ const sql = getMetricSql(METRIC_NAMES.settings, pgMajorVersion);
414
+ const result = await client.query(sql);
415
+ const settings: Record<string, AlteredSetting> = {};
416
+
417
+ for (const row of result.rows) {
418
+ // Filter for non-default settings (is_default = 0 means non-default)
419
+ if (!toBool(row.is_default)) {
420
+ const name = row.tag_setting_name;
421
+ const settingValue = row.tag_setting_value;
422
+ const unit = row.tag_unit || "";
423
+ const category = row.tag_category || "";
424
+ const settingNormalized = row.setting_normalized !== null ? parseFloat(row.setting_normalized) : null;
425
+ const unitNormalized = row.unit_normalized || null;
426
+
427
+ settings[name] = {
428
+ value: settingValue,
429
+ unit,
430
+ category,
431
+ pretty_value: formatSettingPrettyValue(settingNormalized, unitNormalized, settingValue),
432
+ };
433
+ }
434
+ }
435
+
436
+ return settings;
437
+ }
438
+
439
+ /**
440
+ * Get database sizes (all non-template databases)
441
+ * Uses simple inline SQL (lists all databases, CLI-specific)
442
+ */
443
+ export async function getDatabaseSizes(client: Client): Promise<Record<string, number>> {
444
+ const result = await client.query(`
445
+ select
446
+ datname,
447
+ pg_database_size(datname) as size_bytes
448
+ from pg_database
449
+ where datistemplate = false
450
+ order by size_bytes desc
451
+ `);
452
+ const sizes: Record<string, number> = {};
453
+
454
+ for (const row of result.rows) {
455
+ sizes[row.datname] = parseInt(row.size_bytes, 10);
456
+ }
457
+
458
+ return sizes;
459
+ }
460
+
461
+ /**
462
+ * Get cluster general info metrics
463
+ * Uses 'db_stats' metric and inline SQL for connection states/uptime
464
+ */
465
+ export async function getClusterInfo(client: Client, pgMajorVersion: number = 16): Promise<Record<string, ClusterMetric>> {
466
+ const info: Record<string, ClusterMetric> = {};
467
+
468
+ // Get database statistics from db_stats metric
469
+ const dbStatsSql = getMetricSql(METRIC_NAMES.dbStats, pgMajorVersion);
470
+ const statsResult = await client.query(dbStatsSql);
471
+ if (statsResult.rows.length > 0) {
472
+ const stats = statsResult.rows[0];
473
+
474
+ info.total_connections = {
475
+ value: String(stats.numbackends || 0),
476
+ unit: "connections",
477
+ description: "Current database connections",
478
+ };
479
+
480
+ info.total_commits = {
481
+ value: String(stats.xact_commit || 0),
482
+ unit: "transactions",
483
+ description: "Total committed transactions",
484
+ };
485
+
486
+ info.total_rollbacks = {
487
+ value: String(stats.xact_rollback || 0),
488
+ unit: "transactions",
489
+ description: "Total rolled back transactions",
490
+ };
491
+
492
+ const blocksHit = parseInt(stats.blks_hit || "0", 10);
493
+ const blocksRead = parseInt(stats.blks_read || "0", 10);
494
+ const totalBlocks = blocksHit + blocksRead;
495
+ const cacheHitRatio = totalBlocks > 0 ? ((blocksHit / totalBlocks) * 100).toFixed(2) : "0.00";
496
+
497
+ info.cache_hit_ratio = {
498
+ value: cacheHitRatio,
499
+ unit: "%",
500
+ description: "Buffer cache hit ratio",
501
+ };
502
+
503
+ info.blocks_read = {
504
+ value: String(blocksRead),
505
+ unit: "blocks",
506
+ description: "Total disk blocks read",
507
+ };
508
+
509
+ info.blocks_hit = {
510
+ value: String(blocksHit),
511
+ unit: "blocks",
512
+ description: "Total buffer cache hits",
513
+ };
514
+
515
+ info.tuples_returned = {
516
+ value: String(stats.tup_returned || 0),
517
+ unit: "rows",
518
+ description: "Total rows returned by queries",
519
+ };
520
+
521
+ info.tuples_fetched = {
522
+ value: String(stats.tup_fetched || 0),
523
+ unit: "rows",
524
+ description: "Total rows fetched by queries",
525
+ };
526
+
527
+ info.tuples_inserted = {
528
+ value: String(stats.tup_inserted || 0),
529
+ unit: "rows",
530
+ description: "Total rows inserted",
531
+ };
532
+
533
+ info.tuples_updated = {
534
+ value: String(stats.tup_updated || 0),
535
+ unit: "rows",
536
+ description: "Total rows updated",
537
+ };
538
+
539
+ info.tuples_deleted = {
540
+ value: String(stats.tup_deleted || 0),
541
+ unit: "rows",
542
+ description: "Total rows deleted",
543
+ };
544
+
545
+ info.total_deadlocks = {
546
+ value: String(stats.deadlocks || 0),
547
+ unit: "deadlocks",
548
+ description: "Total deadlocks detected",
549
+ };
550
+
551
+ info.temp_files_created = {
552
+ value: String(stats.temp_files || 0),
553
+ unit: "files",
554
+ description: "Total temporary files created",
555
+ };
556
+
557
+ const tempBytes = parseInt(stats.temp_bytes || "0", 10);
558
+ info.temp_bytes_written = {
559
+ value: formatBytes(tempBytes),
560
+ unit: "bytes",
561
+ description: "Total temporary file bytes written",
562
+ };
563
+
564
+ // Uptime from db_stats
565
+ if (stats.postmaster_uptime_s) {
566
+ const uptimeSeconds = parseInt(stats.postmaster_uptime_s, 10);
567
+ const days = Math.floor(uptimeSeconds / SECONDS_PER_DAY);
568
+ const hours = Math.floor((uptimeSeconds % SECONDS_PER_DAY) / SECONDS_PER_HOUR);
569
+ const minutes = Math.floor((uptimeSeconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE);
570
+ info.uptime = {
571
+ value: `${days} days ${hours}:${String(minutes).padStart(2, "0")}:${String(uptimeSeconds % SECONDS_PER_MINUTE).padStart(2, "0")}`,
572
+ unit: "interval",
573
+ description: "Server uptime",
574
+ };
575
+ }
576
+ }
577
+
578
+ // Get connection states (simple inline SQL)
579
+ const connResult = await client.query(`
580
+ select
581
+ coalesce(state, 'null') as state,
582
+ count(*) as count
583
+ from pg_stat_activity
584
+ group by state
585
+ `);
586
+ for (const row of connResult.rows) {
587
+ const stateKey = `connections_${row.state.replace(/\s+/g, "_")}`;
588
+ info[stateKey] = {
589
+ value: String(row.count),
590
+ unit: "connections",
591
+ description: `Connections in '${row.state}' state`,
592
+ };
593
+ }
594
+
595
+ // Get uptime info (simple inline SQL)
596
+ const uptimeResult = await client.query(`
597
+ select
598
+ pg_postmaster_start_time() as start_time,
599
+ current_timestamp - pg_postmaster_start_time() as uptime
600
+ `);
601
+ if (uptimeResult.rows.length > 0) {
602
+ const uptime = uptimeResult.rows[0];
603
+ const startTime = uptime.start_time instanceof Date
604
+ ? uptime.start_time.toISOString()
605
+ : String(uptime.start_time);
606
+ info.start_time = {
607
+ value: startTime,
608
+ unit: "timestamp",
609
+ description: "PostgreSQL server start time",
610
+ };
611
+ if (!info.uptime) {
612
+ info.uptime = {
613
+ value: String(uptime.uptime),
614
+ unit: "interval",
615
+ description: "Server uptime",
616
+ };
617
+ }
618
+ }
619
+
620
+ return info;
621
+ }
622
+
623
+ /**
624
+ * Get invalid indexes from the database (H001).
625
+ * Invalid indexes have indisvalid = false, typically from failed CREATE INDEX CONCURRENTLY.
626
+ *
627
+ * @param client - Connected PostgreSQL client
628
+ * @param pgMajorVersion - PostgreSQL major version (default: 16)
629
+ * @returns Array of invalid index entries with observation data for decision tree analysis
630
+ */
631
+ export async function getInvalidIndexes(client: Client, pgMajorVersion: number = 16): Promise<InvalidIndex[]> {
632
+ const sql = getMetricSql(METRIC_NAMES.H001, pgMajorVersion);
633
+ const result = await client.query(sql);
634
+ return result.rows.map((row) => {
635
+ const transformed = transformMetricRow(row);
636
+ const indexSizeBytes = parseInt(String(transformed.index_size_bytes || 0), 10);
637
+
638
+ return {
639
+ schema_name: String(transformed.schema_name || ""),
640
+ table_name: String(transformed.table_name || ""),
641
+ index_name: String(transformed.index_name || ""),
642
+ relation_name: String(transformed.relation_name || ""),
643
+ index_size_bytes: indexSizeBytes,
644
+ index_size_pretty: formatBytes(indexSizeBytes),
645
+ index_definition: String(transformed.index_definition || ""),
646
+ supports_fk: toBool(transformed.supports_fk),
647
+ is_pk: toBool(transformed.is_pk),
648
+ is_unique: toBool(transformed.is_unique),
649
+ constraint_name: transformed.constraint_name ? String(transformed.constraint_name) : null,
650
+ table_row_estimate: parseInt(String(transformed.table_row_estimate || 0), 10),
651
+ has_valid_duplicate: toBool(transformed.has_valid_duplicate),
652
+ valid_duplicate_name: transformed.valid_index_name ? String(transformed.valid_index_name) : null,
653
+ valid_duplicate_definition: transformed.valid_index_definition ? String(transformed.valid_index_definition) : null,
654
+ };
655
+ });
656
+ }
657
+
658
+ /**
659
+ * Get unused indexes from the database (H002).
660
+ * Unused indexes have zero scans since stats were last reset.
661
+ *
662
+ * @param client - Connected PostgreSQL client
663
+ * @param pgMajorVersion - PostgreSQL major version (default: 16)
664
+ * @returns Array of unused index entries with scan counts and FK support info
665
+ */
666
+ export async function getUnusedIndexes(client: Client, pgMajorVersion: number = 16): Promise<UnusedIndex[]> {
667
+ const sql = getMetricSql(METRIC_NAMES.H002, pgMajorVersion);
668
+ const result = await client.query(sql);
669
+ return result.rows.map((row) => {
670
+ const transformed = transformMetricRow(row);
671
+ const indexSizeBytes = parseInt(String(transformed.index_size_bytes || 0), 10);
672
+ return {
673
+ schema_name: String(transformed.schema_name || ""),
674
+ table_name: String(transformed.table_name || ""),
675
+ index_name: String(transformed.index_name || ""),
676
+ index_definition: String(transformed.index_definition || ""),
677
+ reason: String(transformed.reason || ""),
678
+ idx_scan: parseInt(String(transformed.idx_scan || 0), 10),
679
+ index_size_bytes: indexSizeBytes,
680
+ idx_is_btree: toBool(transformed.idx_is_btree),
681
+ supports_fk: toBool(transformed.supports_fk),
682
+ index_size_pretty: formatBytes(indexSizeBytes),
683
+ };
684
+ });
685
+ }
686
+
687
+ /**
688
+ * Get stats reset info (H002)
689
+ * SQL loaded from config/pgwatch-prometheus/metrics.yml (stats_reset)
690
+ */
691
+ export async function getStatsReset(client: Client, pgMajorVersion: number = 16): Promise<StatsReset> {
692
+ const sql = getMetricSql(METRIC_NAMES.statsReset, pgMajorVersion);
693
+ const result = await client.query(sql);
694
+ const row = result.rows[0] || {};
695
+
696
+ // The stats_reset metric returns stats_reset_epoch and seconds_since_reset
697
+ // We need to calculate additional fields
698
+ const statsResetEpoch = row.stats_reset_epoch ? parseFloat(row.stats_reset_epoch) : null;
699
+ const secondsSinceReset = row.seconds_since_reset ? parseInt(row.seconds_since_reset, 10) : null;
700
+
701
+ // Calculate stats_reset_time from epoch
702
+ const statsResetTime = statsResetEpoch
703
+ ? new Date(statsResetEpoch * 1000).toISOString()
704
+ : null;
705
+
706
+ // Calculate days since reset
707
+ const daysSinceReset = secondsSinceReset !== null
708
+ ? Math.floor(secondsSinceReset / SECONDS_PER_DAY)
709
+ : null;
710
+
711
+ // Get postmaster startup time separately (simple inline SQL)
712
+ // This is supplementary data - errors are captured in output, not propagated
713
+ let postmasterStartupEpoch: number | null = null;
714
+ let postmasterStartupTime: string | null = null;
715
+ let postmasterStartupError: string | undefined;
716
+ try {
717
+ const pmResult = await client.query(`
718
+ select
719
+ extract(epoch from pg_postmaster_start_time()) as postmaster_startup_epoch,
720
+ pg_postmaster_start_time()::text as postmaster_startup_time
721
+ `);
722
+ if (pmResult.rows.length > 0) {
723
+ postmasterStartupEpoch = pmResult.rows[0].postmaster_startup_epoch
724
+ ? parseFloat(pmResult.rows[0].postmaster_startup_epoch)
725
+ : null;
726
+ postmasterStartupTime = pmResult.rows[0].postmaster_startup_time || null;
727
+ }
728
+ } catch (err) {
729
+ const errorMsg = err instanceof Error ? err.message : String(err);
730
+ postmasterStartupError = `Failed to query postmaster start time: ${errorMsg}`;
731
+ console.log(`[getStatsReset] Warning: ${postmasterStartupError}`);
732
+ }
733
+
734
+ const statsResult: StatsReset = {
735
+ stats_reset_epoch: statsResetEpoch,
736
+ stats_reset_time: statsResetTime,
737
+ days_since_reset: daysSinceReset,
738
+ postmaster_startup_epoch: postmasterStartupEpoch,
739
+ postmaster_startup_time: postmasterStartupTime,
740
+ };
741
+
742
+ // Only include error field if there was an error (keeps output clean)
743
+ if (postmasterStartupError) {
744
+ statsResult.postmaster_startup_error = postmasterStartupError;
745
+ }
746
+
747
+ return statsResult;
748
+ }
749
+
750
+ /**
751
+ * Get current database name and size
752
+ * Uses 'db_size' metric from metrics.yml
753
+ */
754
+ export async function getCurrentDatabaseInfo(client: Client, pgMajorVersion: number = 16): Promise<{ datname: string; size_bytes: number }> {
755
+ const sql = getMetricSql(METRIC_NAMES.dbSize, pgMajorVersion);
756
+ const result = await client.query(sql);
757
+ const row = result.rows[0] || {};
758
+
759
+ // db_size metric returns tag_datname and size_b
760
+ return {
761
+ datname: row.tag_datname || "postgres",
762
+ size_bytes: parseInt(row.size_b || "0", 10),
763
+ };
764
+ }
765
+
766
+ /**
767
+ * Type guard to validate redundant_to_json item structure.
768
+ * Returns true if item is a valid object (may have expected properties).
769
+ */
770
+ function isValidRedundantToItem(item: unknown): item is Record<string, unknown> {
771
+ return typeof item === "object" && item !== null && !Array.isArray(item);
772
+ }
773
+
774
+ /**
775
+ * Get redundant indexes from the database (H004).
776
+ * Redundant indexes are covered by other indexes (same leading columns).
777
+ *
778
+ * @param client - Connected PostgreSQL client
779
+ * @param pgMajorVersion - PostgreSQL major version (default: 16)
780
+ * @returns Array of redundant index entries with covering index info
781
+ */
782
+ export async function getRedundantIndexes(client: Client, pgMajorVersion: number = 16): Promise<RedundantIndex[]> {
783
+ const sql = getMetricSql(METRIC_NAMES.H004, pgMajorVersion);
784
+ const result = await client.query(sql);
785
+ return result.rows.map((row) => {
786
+ const transformed = transformMetricRow(row);
787
+ const indexSizeBytes = parseInt(String(transformed.index_size_bytes || 0), 10);
788
+ const tableSizeBytes = parseInt(String(transformed.table_size_bytes || 0), 10);
789
+
790
+ // Parse redundant_to JSON array (indexes that make this one redundant)
791
+ let redundantTo: RedundantToIndex[] = [];
792
+ let parseError: string | undefined;
793
+ try {
794
+ const jsonStr = String(transformed.redundant_to_json || "[]");
795
+ const parsed = JSON.parse(jsonStr);
796
+ if (Array.isArray(parsed)) {
797
+ redundantTo = parsed
798
+ .filter(isValidRedundantToItem)
799
+ .map((item) => {
800
+ const sizeBytes = parseInt(String(item.index_size_bytes ?? 0), 10);
801
+ return {
802
+ index_name: String(item.index_name ?? ""),
803
+ index_definition: String(item.index_definition ?? ""),
804
+ index_size_bytes: sizeBytes,
805
+ index_size_pretty: formatBytes(sizeBytes),
806
+ };
807
+ });
808
+ }
809
+ } catch (err) {
810
+ const errorMsg = err instanceof Error ? err.message : String(err);
811
+ const indexName = String(transformed.index_name || "unknown");
812
+ parseError = `Failed to parse redundant_to_json: ${errorMsg}`;
813
+ console.log(`[H004] Warning: ${parseError} for index "${indexName}"`);
814
+ }
815
+
816
+ const result: RedundantIndex = {
817
+ schema_name: String(transformed.schema_name || ""),
818
+ table_name: String(transformed.table_name || ""),
819
+ index_name: String(transformed.index_name || ""),
820
+ relation_name: String(transformed.relation_name || ""),
821
+ access_method: String(transformed.access_method || ""),
822
+ reason: String(transformed.reason || ""),
823
+ index_size_bytes: indexSizeBytes,
824
+ table_size_bytes: tableSizeBytes,
825
+ index_usage: parseInt(String(transformed.index_usage || 0), 10),
826
+ supports_fk: toBool(transformed.supports_fk),
827
+ index_definition: String(transformed.index_definition || ""),
828
+ index_size_pretty: formatBytes(indexSizeBytes),
829
+ table_size_pretty: formatBytes(tableSizeBytes),
830
+ redundant_to: redundantTo,
831
+ };
832
+
833
+ // Only include parse error field if there was an error (keeps output clean)
834
+ if (parseError) {
835
+ result.redundant_to_parse_error = parseError;
836
+ }
837
+
838
+ return result;
839
+ });
840
+ }
841
+
842
+ /**
843
+ * Create base report structure
844
+ */
845
+ export function createBaseReport(
846
+ checkId: string,
847
+ checkTitle: string,
848
+ nodeName: string
849
+ ): Report {
850
+ const buildTs = resolveBuildTs();
851
+ return {
852
+ version: pkg.version || null,
853
+ build_ts: buildTs,
854
+ generation_mode: "express",
855
+ checkId,
856
+ checkTitle,
857
+ timestamptz: new Date().toISOString(),
858
+ nodes: {
859
+ primary: nodeName,
860
+ standbys: [],
861
+ },
862
+ results: {},
863
+ };
864
+ }
865
+
866
+ function readTextFileSafe(p: string): string | null {
867
+ try {
868
+ const value = fs.readFileSync(p, "utf8").trim();
869
+ return value || null;
870
+ } catch {
871
+ // Intentionally silent: this is a "safe" read that returns null on any error
872
+ // (file not found, permission denied, etc.) - used for optional config files
873
+ return null;
874
+ }
875
+ }
876
+
877
+ function resolveBuildTs(): string | null {
878
+ // Follow reporter.py approach: read BUILD_TS from filesystem, with env override.
879
+ // Default: /BUILD_TS (useful in container images).
880
+ const envPath = process.env.PGAI_BUILD_TS_FILE;
881
+ const p = (envPath && envPath.trim()) ? envPath.trim() : "/BUILD_TS";
882
+
883
+ const fromFile = readTextFileSafe(p);
884
+ if (fromFile) return fromFile;
885
+
886
+ // Fallback for packaged CLI: allow placing BUILD_TS next to dist/ (package root).
887
+ // dist/lib/checkup.js => package root: dist/..
888
+ try {
889
+ const pkgRoot = path.resolve(__dirname, "..");
890
+ const fromPkgFile = readTextFileSafe(path.join(pkgRoot, "BUILD_TS"));
891
+ if (fromPkgFile) return fromPkgFile;
892
+ } catch (err) {
893
+ // Path resolution failing is unexpected - warn about it
894
+ const errorMsg = err instanceof Error ? err.message : String(err);
895
+ console.warn(`[resolveBuildTs] Warning: path resolution failed: ${errorMsg}`);
896
+ }
897
+
898
+ // Last resort: use package.json mtime as an approximation (non-null, stable-ish).
899
+ try {
900
+ const pkgJsonPath = path.resolve(__dirname, "..", "package.json");
901
+ const st = fs.statSync(pkgJsonPath);
902
+ return st.mtime.toISOString();
903
+ } catch (err) {
904
+ // package.json not found is expected in some environments (e.g., bundled) - debug only
905
+ if (process.env.DEBUG) {
906
+ const errorMsg = err instanceof Error ? err.message : String(err);
907
+ console.log(`[resolveBuildTs] Could not stat package.json, using current time: ${errorMsg}`);
908
+ }
909
+ return new Date().toISOString();
910
+ }
911
+ }
912
+
913
+ // ============================================================================
914
+ // Unified Report Generator Helpers
915
+ // ============================================================================
916
+
917
+ /**
918
+ * Generate a simple version report (A002, A013).
919
+ * These reports only contain PostgreSQL version information.
920
+ */
921
+ async function generateVersionReport(
922
+ client: Client,
923
+ nodeName: string,
924
+ checkId: string,
925
+ checkTitle: string
926
+ ): Promise<Report> {
927
+ const report = createBaseReport(checkId, checkTitle, nodeName);
928
+ const postgresVersion = await getPostgresVersion(client);
929
+ report.results[nodeName] = { data: { version: postgresVersion } };
930
+ return report;
931
+ }
932
+
933
+ /**
934
+ * Generate a settings-based report (A003, A007).
935
+ * Fetches settings using provided function and includes postgres_version.
936
+ */
937
+ async function generateSettingsReport(
938
+ client: Client,
939
+ nodeName: string,
940
+ checkId: string,
941
+ checkTitle: string,
942
+ fetchSettings: (client: Client, pgMajorVersion: number) => Promise<Record<string, unknown>>
943
+ ): Promise<Report> {
944
+ const report = createBaseReport(checkId, checkTitle, nodeName);
945
+ const postgresVersion = await getPostgresVersion(client);
946
+ const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
947
+ const settings = await fetchSettings(client, pgMajorVersion);
948
+ report.results[nodeName] = { data: settings, postgres_version: postgresVersion };
949
+ return report;
950
+ }
951
+
952
+ /**
953
+ * Generate an index report (H001, H002, H004).
954
+ * Common structure: index list + totals + database info, keyed by database name.
955
+ */
956
+ async function generateIndexReport<T extends { index_size_bytes: number }>(
957
+ client: Client,
958
+ nodeName: string,
959
+ checkId: string,
960
+ checkTitle: string,
961
+ indexFieldName: string,
962
+ fetchIndexes: (client: Client, pgMajorVersion: number) => Promise<T[]>,
963
+ extraFields?: (client: Client, pgMajorVersion: number) => Promise<Record<string, unknown>>
964
+ ): Promise<Report> {
965
+ const report = createBaseReport(checkId, checkTitle, nodeName);
966
+ const postgresVersion = await getPostgresVersion(client);
967
+ const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
968
+ const indexes = await fetchIndexes(client, pgMajorVersion);
969
+ const { datname: dbName, size_bytes: dbSizeBytes } = await getCurrentDatabaseInfo(client, pgMajorVersion);
970
+
971
+ const totalCount = indexes.length;
972
+ const totalSizeBytes = indexes.reduce((sum, idx) => sum + idx.index_size_bytes, 0);
973
+
974
+ const dbEntry: Record<string, unknown> = {
975
+ [indexFieldName]: indexes,
976
+ total_count: totalCount,
977
+ total_size_bytes: totalSizeBytes,
978
+ total_size_pretty: formatBytes(totalSizeBytes),
979
+ database_size_bytes: dbSizeBytes,
980
+ database_size_pretty: formatBytes(dbSizeBytes),
981
+ };
982
+
983
+ // Add extra fields if provided (e.g., stats_reset for H002)
984
+ if (extraFields) {
985
+ Object.assign(dbEntry, await extraFields(client, pgMajorVersion));
986
+ }
987
+
988
+ report.results[nodeName] = { data: { [dbName]: dbEntry }, postgres_version: postgresVersion };
989
+ return report;
990
+ }
991
+
992
+ // ============================================================================
993
+ // Report Generators (using unified helpers)
994
+ // ============================================================================
995
+
996
+ /** Generate A002 report - Postgres major version */
997
+ export const generateA002 = (client: Client, nodeName = "node-01") =>
998
+ generateVersionReport(client, nodeName, "A002", "Postgres major version");
999
+
1000
+ /** Generate A003 report - Postgres settings */
1001
+ export const generateA003 = (client: Client, nodeName = "node-01") =>
1002
+ generateSettingsReport(client, nodeName, "A003", "Postgres settings", getSettings);
1003
+
1004
+ /** Generate A004 report - Cluster information (custom structure) */
1005
+ export async function generateA004(client: Client, nodeName: string = "node-01"): Promise<Report> {
1006
+ const report = createBaseReport("A004", "Cluster information", nodeName);
1007
+ const postgresVersion = await getPostgresVersion(client);
1008
+ const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
1009
+ report.results[nodeName] = {
1010
+ data: {
1011
+ general_info: await getClusterInfo(client, pgMajorVersion),
1012
+ database_sizes: await getDatabaseSizes(client),
1013
+ },
1014
+ postgres_version: postgresVersion,
1015
+ };
1016
+ return report;
1017
+ }
1018
+
1019
+ /** Generate A007 report - Altered settings */
1020
+ export const generateA007 = (client: Client, nodeName = "node-01") =>
1021
+ generateSettingsReport(client, nodeName, "A007", "Altered settings", getAlteredSettings);
1022
+
1023
+ /** Generate A013 report - Postgres minor version */
1024
+ export const generateA013 = (client: Client, nodeName = "node-01") =>
1025
+ generateVersionReport(client, nodeName, "A013", "Postgres minor version");
1026
+
1027
+ /** Generate H001 report - Invalid indexes */
1028
+ export const generateH001 = (client: Client, nodeName = "node-01") =>
1029
+ generateIndexReport(client, nodeName, "H001", "Invalid indexes", "invalid_indexes", getInvalidIndexes);
1030
+
1031
+ /** Generate H002 report - Unused indexes (includes stats_reset) */
1032
+ export const generateH002 = (client: Client, nodeName = "node-01") =>
1033
+ generateIndexReport(client, nodeName, "H002", "Unused indexes", "unused_indexes", getUnusedIndexes,
1034
+ async (c, v) => ({ stats_reset: await getStatsReset(c, v) }));
1035
+
1036
+ /** Generate H004 report - Redundant indexes */
1037
+ export const generateH004 = (client: Client, nodeName = "node-01") =>
1038
+ generateIndexReport(client, nodeName, "H004", "Redundant indexes", "redundant_indexes", getRedundantIndexes);
1039
+
1040
+ /**
1041
+ * Generate D004 report - pg_stat_statements and pg_stat_kcache settings.
1042
+ *
1043
+ * Uses graceful degradation: extension queries are wrapped in try-catch
1044
+ * because extensions may not be installed. Errors are included in the
1045
+ * report output rather than failing the entire report.
1046
+ */
1047
+ async function generateD004(client: Client, nodeName: string): Promise<Report> {
1048
+ const report = createBaseReport("D004", "pg_stat_statements and pg_stat_kcache settings", nodeName);
1049
+ const postgresVersion = await getPostgresVersion(client);
1050
+ const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
1051
+ const allSettings = await getSettings(client, pgMajorVersion);
1052
+
1053
+ // Filter settings related to pg_stat_statements and pg_stat_kcache
1054
+ const pgssSettings: Record<string, SettingInfo> = {};
1055
+ for (const [name, setting] of Object.entries(allSettings)) {
1056
+ if (name.startsWith("pg_stat_statements") || name.startsWith("pg_stat_kcache")) {
1057
+ pgssSettings[name] = setting;
1058
+ }
1059
+ }
1060
+
1061
+ // Check pg_stat_statements extension
1062
+ let pgssAvailable = false;
1063
+ let pgssMetricsCount = 0;
1064
+ let pgssTotalCalls = 0;
1065
+ let pgssError: string | null = null;
1066
+ const pgssSampleQueries: Array<{ queryid: string; user: string; database: string; calls: number }> = [];
1067
+
1068
+ try {
1069
+ const extCheck = await client.query(
1070
+ "select 1 from pg_extension where extname = 'pg_stat_statements'"
1071
+ );
1072
+ if (extCheck.rows.length > 0) {
1073
+ pgssAvailable = true;
1074
+ const statsResult = await client.query(`
1075
+ select count(*) as cnt, coalesce(sum(calls), 0) as total_calls
1076
+ from pg_stat_statements
1077
+ `);
1078
+ pgssMetricsCount = parseInt(statsResult.rows[0]?.cnt || "0", 10);
1079
+ pgssTotalCalls = parseInt(statsResult.rows[0]?.total_calls || "0", 10);
1080
+
1081
+ // Get sample queries (top 5 by calls)
1082
+ const sampleResult = await client.query(`
1083
+ select
1084
+ queryid::text as queryid,
1085
+ coalesce(usename, 'unknown') as "user",
1086
+ coalesce(datname, 'unknown') as database,
1087
+ calls
1088
+ from pg_stat_statements s
1089
+ left join pg_database d on s.dbid = d.oid
1090
+ left join pg_user u on s.userid = u.usesysid
1091
+ order by calls desc
1092
+ limit 5
1093
+ `);
1094
+ for (const row of sampleResult.rows) {
1095
+ pgssSampleQueries.push({
1096
+ queryid: row.queryid,
1097
+ user: row.user,
1098
+ database: row.database,
1099
+ calls: parseInt(row.calls, 10),
1100
+ });
1101
+ }
1102
+ }
1103
+ } catch (err) {
1104
+ const errorMsg = err instanceof Error ? err.message : String(err);
1105
+ console.log(`[D004] Error querying pg_stat_statements: ${errorMsg}`);
1106
+ pgssError = errorMsg;
1107
+ }
1108
+
1109
+ // Check pg_stat_kcache extension
1110
+ let kcacheAvailable = false;
1111
+ let kcacheMetricsCount = 0;
1112
+ let kcacheTotalExecTime = 0;
1113
+ let kcacheTotalUserTime = 0;
1114
+ let kcacheTotalSystemTime = 0;
1115
+ let kcacheError: string | null = null;
1116
+ const kcacheSampleQueries: Array<{ queryid: string; user: string; exec_total_time: number }> = [];
1117
+
1118
+ try {
1119
+ const extCheck = await client.query(
1120
+ "select 1 from pg_extension where extname = 'pg_stat_kcache'"
1121
+ );
1122
+ if (extCheck.rows.length > 0) {
1123
+ kcacheAvailable = true;
1124
+ const statsResult = await client.query(`
1125
+ select
1126
+ count(*) as cnt,
1127
+ coalesce(sum(exec_user_time + exec_system_time), 0) as total_exec_time,
1128
+ coalesce(sum(exec_user_time), 0) as total_user_time,
1129
+ coalesce(sum(exec_system_time), 0) as total_system_time
1130
+ from pg_stat_kcache
1131
+ `);
1132
+ kcacheMetricsCount = parseInt(statsResult.rows[0]?.cnt || "0", 10);
1133
+ kcacheTotalExecTime = parseFloat(statsResult.rows[0]?.total_exec_time || "0");
1134
+ kcacheTotalUserTime = parseFloat(statsResult.rows[0]?.total_user_time || "0");
1135
+ kcacheTotalSystemTime = parseFloat(statsResult.rows[0]?.total_system_time || "0");
1136
+
1137
+ // Get sample queries (top 5 by exec time)
1138
+ const sampleResult = await client.query(`
1139
+ select
1140
+ queryid::text as queryid,
1141
+ coalesce(usename, 'unknown') as "user",
1142
+ (exec_user_time + exec_system_time) as exec_total_time
1143
+ from pg_stat_kcache k
1144
+ left join pg_user u on k.userid = u.usesysid
1145
+ order by (exec_user_time + exec_system_time) desc
1146
+ limit 5
1147
+ `);
1148
+ for (const row of sampleResult.rows) {
1149
+ kcacheSampleQueries.push({
1150
+ queryid: row.queryid,
1151
+ user: row.user,
1152
+ exec_total_time: parseFloat(row.exec_total_time),
1153
+ });
1154
+ }
1155
+ }
1156
+ } catch (err) {
1157
+ const errorMsg = err instanceof Error ? err.message : String(err);
1158
+ console.log(`[D004] Error querying pg_stat_kcache: ${errorMsg}`);
1159
+ kcacheError = errorMsg;
1160
+ }
1161
+
1162
+ report.results[nodeName] = {
1163
+ data: {
1164
+ settings: pgssSettings,
1165
+ pg_stat_statements_status: {
1166
+ extension_available: pgssAvailable,
1167
+ metrics_count: pgssMetricsCount,
1168
+ total_calls: pgssTotalCalls,
1169
+ sample_queries: pgssSampleQueries,
1170
+ ...(pgssError && { error: pgssError }),
1171
+ },
1172
+ pg_stat_kcache_status: {
1173
+ extension_available: kcacheAvailable,
1174
+ metrics_count: kcacheMetricsCount,
1175
+ total_exec_time: kcacheTotalExecTime,
1176
+ total_user_time: kcacheTotalUserTime,
1177
+ total_system_time: kcacheTotalSystemTime,
1178
+ sample_queries: kcacheSampleQueries,
1179
+ ...(kcacheError && { error: kcacheError }),
1180
+ },
1181
+ },
1182
+ postgres_version: postgresVersion,
1183
+ };
1184
+
1185
+ return report;
1186
+ }
1187
+
1188
+ /**
1189
+ * Generate F001 report - Autovacuum: current settings
1190
+ */
1191
+ async function generateF001(client: Client, nodeName: string): Promise<Report> {
1192
+ const report = createBaseReport("F001", "Autovacuum: current settings", nodeName);
1193
+ const postgresVersion = await getPostgresVersion(client);
1194
+ const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
1195
+ const allSettings = await getSettings(client, pgMajorVersion);
1196
+
1197
+ // Filter autovacuum-related settings
1198
+ const autovacuumSettings: Record<string, SettingInfo> = {};
1199
+ for (const [name, setting] of Object.entries(allSettings)) {
1200
+ if (name.includes("autovacuum") || name.includes("vacuum")) {
1201
+ autovacuumSettings[name] = setting;
1202
+ }
1203
+ }
1204
+
1205
+ report.results[nodeName] = {
1206
+ data: autovacuumSettings,
1207
+ postgres_version: postgresVersion,
1208
+ };
1209
+
1210
+ return report;
1211
+ }
1212
+
1213
+ /**
1214
+ * Generate G001 report - Memory-related settings
1215
+ */
1216
+ async function generateG001(client: Client, nodeName: string): Promise<Report> {
1217
+ const report = createBaseReport("G001", "Memory-related settings", nodeName);
1218
+ const postgresVersion = await getPostgresVersion(client);
1219
+ const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
1220
+ const allSettings = await getSettings(client, pgMajorVersion);
1221
+
1222
+ // Memory-related setting names
1223
+ const memorySettingNames = [
1224
+ "shared_buffers",
1225
+ "work_mem",
1226
+ "maintenance_work_mem",
1227
+ "effective_cache_size",
1228
+ "wal_buffers",
1229
+ "temp_buffers",
1230
+ "max_connections",
1231
+ "autovacuum_work_mem",
1232
+ "hash_mem_multiplier",
1233
+ "logical_decoding_work_mem",
1234
+ "max_stack_depth",
1235
+ "max_prepared_transactions",
1236
+ "max_locks_per_transaction",
1237
+ "max_pred_locks_per_transaction",
1238
+ ];
1239
+
1240
+ const memorySettings: Record<string, SettingInfo> = {};
1241
+ for (const name of memorySettingNames) {
1242
+ if (allSettings[name]) {
1243
+ memorySettings[name] = allSettings[name];
1244
+ }
1245
+ }
1246
+
1247
+ // Calculate memory usage estimates
1248
+ interface MemoryUsage {
1249
+ shared_buffers_bytes: number;
1250
+ shared_buffers_pretty: string;
1251
+ wal_buffers_bytes: number;
1252
+ wal_buffers_pretty: string;
1253
+ shared_memory_total_bytes: number;
1254
+ shared_memory_total_pretty: string;
1255
+ work_mem_per_connection_bytes: number;
1256
+ work_mem_per_connection_pretty: string;
1257
+ max_work_mem_usage_bytes: number;
1258
+ max_work_mem_usage_pretty: string;
1259
+ maintenance_work_mem_bytes: number;
1260
+ maintenance_work_mem_pretty: string;
1261
+ effective_cache_size_bytes: number;
1262
+ effective_cache_size_pretty: string;
1263
+ }
1264
+
1265
+ let memoryUsage: MemoryUsage | Record<string, never> = {};
1266
+ let memoryError: string | null = null;
1267
+
1268
+ try {
1269
+ // Get actual byte values from PostgreSQL
1270
+ const memQuery = await client.query(`
1271
+ select
1272
+ pg_size_bytes(current_setting('shared_buffers')) as shared_buffers_bytes,
1273
+ pg_size_bytes(current_setting('wal_buffers')) as wal_buffers_bytes,
1274
+ pg_size_bytes(current_setting('work_mem')) as work_mem_bytes,
1275
+ pg_size_bytes(current_setting('maintenance_work_mem')) as maintenance_work_mem_bytes,
1276
+ pg_size_bytes(current_setting('effective_cache_size')) as effective_cache_size_bytes,
1277
+ current_setting('max_connections')::int as max_connections
1278
+ `);
1279
+
1280
+ if (memQuery.rows.length > 0) {
1281
+ const row = memQuery.rows[0];
1282
+ const sharedBuffersBytes = parseInt(row.shared_buffers_bytes, 10);
1283
+ const walBuffersBytes = parseInt(row.wal_buffers_bytes, 10);
1284
+ const workMemBytes = parseInt(row.work_mem_bytes, 10);
1285
+ const maintenanceWorkMemBytes = parseInt(row.maintenance_work_mem_bytes, 10);
1286
+ const effectiveCacheSizeBytes = parseInt(row.effective_cache_size_bytes, 10);
1287
+ const maxConnections = row.max_connections;
1288
+
1289
+ const sharedMemoryTotal = sharedBuffersBytes + walBuffersBytes;
1290
+ const maxWorkMemUsage = workMemBytes * maxConnections;
1291
+
1292
+ memoryUsage = {
1293
+ shared_buffers_bytes: sharedBuffersBytes,
1294
+ shared_buffers_pretty: formatBytes(sharedBuffersBytes),
1295
+ wal_buffers_bytes: walBuffersBytes,
1296
+ wal_buffers_pretty: formatBytes(walBuffersBytes),
1297
+ shared_memory_total_bytes: sharedMemoryTotal,
1298
+ shared_memory_total_pretty: formatBytes(sharedMemoryTotal),
1299
+ work_mem_per_connection_bytes: workMemBytes,
1300
+ work_mem_per_connection_pretty: formatBytes(workMemBytes),
1301
+ max_work_mem_usage_bytes: maxWorkMemUsage,
1302
+ max_work_mem_usage_pretty: formatBytes(maxWorkMemUsage),
1303
+ maintenance_work_mem_bytes: maintenanceWorkMemBytes,
1304
+ maintenance_work_mem_pretty: formatBytes(maintenanceWorkMemBytes),
1305
+ effective_cache_size_bytes: effectiveCacheSizeBytes,
1306
+ effective_cache_size_pretty: formatBytes(effectiveCacheSizeBytes),
1307
+ };
1308
+ }
1309
+ } catch (err) {
1310
+ const errorMsg = err instanceof Error ? err.message : String(err);
1311
+ console.log(`[G001] Error calculating memory usage: ${errorMsg}`);
1312
+ memoryError = errorMsg;
1313
+ }
1314
+
1315
+ report.results[nodeName] = {
1316
+ data: {
1317
+ settings: memorySettings,
1318
+ analysis: {
1319
+ estimated_total_memory_usage: memoryUsage,
1320
+ ...(memoryError && { error: memoryError }),
1321
+ },
1322
+ },
1323
+ postgres_version: postgresVersion,
1324
+ };
1325
+
1326
+ return report;
1327
+ }
1328
+
1329
+ /**
1330
+ * Available report generators
1331
+ */
1332
+ export const REPORT_GENERATORS: Record<string, (client: Client, nodeName: string) => Promise<Report>> = {
1333
+ A002: generateA002,
1334
+ A003: generateA003,
1335
+ A004: generateA004,
1336
+ A007: generateA007,
1337
+ A013: generateA013,
1338
+ D004: generateD004,
1339
+ F001: generateF001,
1340
+ G001: generateG001,
1341
+ H001: generateH001,
1342
+ H002: generateH002,
1343
+ H004: generateH004,
1344
+ };
1345
+
1346
+ /**
1347
+ * Check IDs and titles
1348
+ */
1349
+ export const CHECK_INFO: Record<string, string> = {
1350
+ A002: "Postgres major version",
1351
+ A003: "Postgres settings",
1352
+ A004: "Cluster information",
1353
+ A007: "Altered settings",
1354
+ A013: "Postgres minor version",
1355
+ D004: "pg_stat_statements and pg_stat_kcache settings",
1356
+ F001: "Autovacuum: current settings",
1357
+ G001: "Memory-related settings",
1358
+ H001: "Invalid indexes",
1359
+ H002: "Unused indexes",
1360
+ H004: "Redundant indexes",
1361
+ };
1362
+
1363
+ /**
1364
+ * Generate all available health check reports.
1365
+ * This is the main entry point for express mode checkup generation.
1366
+ *
1367
+ * @param client - Connected PostgreSQL client
1368
+ * @param nodeName - Node identifier for the report (default: "node-01")
1369
+ * @param onProgress - Optional callback for progress updates during generation
1370
+ * @returns Object mapping check IDs (e.g., "H001", "A002") to their reports
1371
+ * @throws {Error} If any critical report generation fails
1372
+ */
1373
+ export async function generateAllReports(
1374
+ client: Client,
1375
+ nodeName: string = "node-01",
1376
+ onProgress?: (info: { checkId: string; checkTitle: string; index: number; total: number }) => void
1377
+ ): Promise<Record<string, Report>> {
1378
+ const reports: Record<string, Report> = {};
1379
+
1380
+ const entries = Object.entries(REPORT_GENERATORS);
1381
+ const total = entries.length;
1382
+ let index = 0;
1383
+
1384
+ for (const [checkId, generator] of entries) {
1385
+ index += 1;
1386
+ onProgress?.({
1387
+ checkId,
1388
+ checkTitle: CHECK_INFO[checkId] || checkId,
1389
+ index,
1390
+ total,
1391
+ });
1392
+ reports[checkId] = await generator(client, nodeName);
1393
+ }
1394
+
1395
+ return reports;
1396
+ }