postgresai 0.14.0-dev.8 → 0.14.0-dev.80

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