postgresai 0.14.0-dev.53 → 0.14.0-dev.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -35
- package/bin/postgres-ai.ts +436 -4
- package/bun.lock +3 -1
- package/bunfig.toml +11 -0
- package/dist/bin/postgres-ai.js +2184 -218
- package/lib/auth-server.ts +52 -5
- package/lib/checkup-api.ts +386 -0
- package/lib/checkup.ts +1327 -0
- package/lib/config.ts +3 -0
- package/lib/issues.ts +5 -41
- package/lib/metrics-embedded.ts +79 -0
- package/lib/metrics-loader.ts +127 -0
- package/lib/util.ts +61 -0
- package/package.json +12 -6
- package/packages/postgres-ai/README.md +26 -0
- package/packages/postgres-ai/bin/postgres-ai.js +27 -0
- package/packages/postgres-ai/package.json +27 -0
- package/scripts/embed-metrics.ts +154 -0
- package/test/checkup.integration.test.ts +273 -0
- package/test/checkup.test.ts +890 -0
- package/test/init.integration.test.ts +36 -33
- package/test/schema-validation.test.ts +81 -0
- package/test/test-utils.ts +122 -0
- package/dist/sql/01.role.sql +0 -16
- package/dist/sql/02.permissions.sql +0 -37
- package/dist/sql/03.optional_rds.sql +0 -6
- package/dist/sql/04.optional_self_managed.sql +0 -8
- package/dist/sql/05.helpers.sql +0 -415
package/lib/config.ts
CHANGED
|
@@ -9,6 +9,7 @@ export interface Config {
|
|
|
9
9
|
apiKey: string | null;
|
|
10
10
|
baseUrl: string | null;
|
|
11
11
|
orgId: number | null;
|
|
12
|
+
defaultProject: string | null;
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
/**
|
|
@@ -46,6 +47,7 @@ export function readConfig(): Config {
|
|
|
46
47
|
apiKey: null,
|
|
47
48
|
baseUrl: null,
|
|
48
49
|
orgId: null,
|
|
50
|
+
defaultProject: null,
|
|
49
51
|
};
|
|
50
52
|
|
|
51
53
|
// Try user-level config first
|
|
@@ -57,6 +59,7 @@ export function readConfig(): Config {
|
|
|
57
59
|
config.apiKey = parsed.apiKey || null;
|
|
58
60
|
config.baseUrl = parsed.baseUrl || null;
|
|
59
61
|
config.orgId = parsed.orgId || null;
|
|
62
|
+
config.defaultProject = parsed.defaultProject || null;
|
|
60
63
|
return config;
|
|
61
64
|
} catch (err) {
|
|
62
65
|
const message = err instanceof Error ? err.message : String(err);
|
package/lib/issues.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { maskSecret, normalizeBaseUrl } from "./util";
|
|
1
|
+
import { formatHttpError, maskSecret, normalizeBaseUrl } from "./util";
|
|
2
2
|
|
|
3
3
|
export interface IssueActionItem {
|
|
4
4
|
id: string;
|
|
@@ -98,16 +98,7 @@ export async function fetchIssues(params: FetchIssuesParams): Promise<IssueListI
|
|
|
98
98
|
throw new Error(`Failed to parse issues response: ${data}`);
|
|
99
99
|
}
|
|
100
100
|
} else {
|
|
101
|
-
|
|
102
|
-
if (data) {
|
|
103
|
-
try {
|
|
104
|
-
const errObj = JSON.parse(data);
|
|
105
|
-
errMsg += `\n${JSON.stringify(errObj, null, 2)}`;
|
|
106
|
-
} catch {
|
|
107
|
-
errMsg += `\n${data}`;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
throw new Error(errMsg);
|
|
101
|
+
throw new Error(formatHttpError("Failed to fetch issues", response.status, data));
|
|
111
102
|
}
|
|
112
103
|
}
|
|
113
104
|
|
|
@@ -164,16 +155,7 @@ export async function fetchIssueComments(params: FetchIssueCommentsParams): Prom
|
|
|
164
155
|
throw new Error(`Failed to parse issue comments response: ${data}`);
|
|
165
156
|
}
|
|
166
157
|
} else {
|
|
167
|
-
|
|
168
|
-
if (data) {
|
|
169
|
-
try {
|
|
170
|
-
const errObj = JSON.parse(data);
|
|
171
|
-
errMsg += `\n${JSON.stringify(errObj, null, 2)}`;
|
|
172
|
-
} catch {
|
|
173
|
-
errMsg += `\n${data}`;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
throw new Error(errMsg);
|
|
158
|
+
throw new Error(formatHttpError("Failed to fetch issue comments", response.status, data));
|
|
177
159
|
}
|
|
178
160
|
}
|
|
179
161
|
|
|
@@ -237,16 +219,7 @@ export async function fetchIssue(params: FetchIssueParams): Promise<IssueDetail
|
|
|
237
219
|
throw new Error(`Failed to parse issue response: ${data}`);
|
|
238
220
|
}
|
|
239
221
|
} else {
|
|
240
|
-
|
|
241
|
-
if (data) {
|
|
242
|
-
try {
|
|
243
|
-
const errObj = JSON.parse(data);
|
|
244
|
-
errMsg += `\n${JSON.stringify(errObj, null, 2)}`;
|
|
245
|
-
} catch {
|
|
246
|
-
errMsg += `\n${data}`;
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
throw new Error(errMsg);
|
|
222
|
+
throw new Error(formatHttpError("Failed to fetch issue", response.status, data));
|
|
250
223
|
}
|
|
251
224
|
}
|
|
252
225
|
|
|
@@ -318,15 +291,6 @@ export async function createIssueComment(params: CreateIssueCommentParams): Prom
|
|
|
318
291
|
throw new Error(`Failed to parse create comment response: ${data}`);
|
|
319
292
|
}
|
|
320
293
|
} else {
|
|
321
|
-
|
|
322
|
-
if (data) {
|
|
323
|
-
try {
|
|
324
|
-
const errObj = JSON.parse(data);
|
|
325
|
-
errMsg += `\n${JSON.stringify(errObj, null, 2)}`;
|
|
326
|
-
} catch {
|
|
327
|
-
errMsg += `\n${data}`;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
throw new Error(errMsg);
|
|
294
|
+
throw new Error(formatHttpError("Failed to create issue comment", response.status, data));
|
|
331
295
|
}
|
|
332
296
|
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// AUTO-GENERATED FILE - DO NOT EDIT
|
|
2
|
+
// Generated from config/pgwatch-prometheus/metrics.yml by scripts/embed-metrics.ts
|
|
3
|
+
// Generated at: 2025-12-28T15:08:23.760Z
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Metric definition from metrics.yml
|
|
7
|
+
*/
|
|
8
|
+
export interface MetricDefinition {
|
|
9
|
+
description?: string;
|
|
10
|
+
sqls: Record<number, string>; // PG major version -> SQL query
|
|
11
|
+
gauges?: string[];
|
|
12
|
+
statement_timeout_seconds?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Embedded metrics for express mode reports.
|
|
17
|
+
* Only includes metrics required for CLI checkup reports.
|
|
18
|
+
*/
|
|
19
|
+
export const METRICS: Record<string, MetricDefinition> = {
|
|
20
|
+
"settings": {
|
|
21
|
+
description: "This metric collects various PostgreSQL server settings and configurations. It provides insights into the server's configuration, including version, memory settings, and other important parameters. This metric is useful for monitoring server settings and ensuring optimal performance. Note: For lock_timeout and statement_timeout, we use reset_val instead of setting because pgwatch overrides these during metric collection, which would mask the actual configured values.",
|
|
22
|
+
sqls: {
|
|
23
|
+
11: "with base as ( /* pgwatch_generated */\n select\n name,\n -- Use reset_val for lock_timeout/statement_timeout because pgwatch overrides them\n -- during collection (lock_timeout=100ms, statement_timeout per-metric).\n case\n when name in ('lock_timeout', 'statement_timeout') then reset_val\n else setting\n end as effective_setting,\n unit,\n category,\n vartype,\n -- For lock_timeout/statement_timeout, compare reset_val with boot_val\n -- since source becomes 'session' during collection.\n case\n when name in ('lock_timeout', 'statement_timeout') then (reset_val = boot_val)\n else (source = 'default')\n end as is_default_bool\n from pg_settings\n), with_numeric as (\n select\n *,\n case\n when effective_setting ~ '^-?[0-9]+$' then effective_setting::bigint\n else null\n end as numeric_value\n from base\n)\nselect\n (extract(epoch from now()) * 1e9)::int8 as epoch_ns,\n current_database() as tag_datname,\n name as tag_setting_name,\n effective_setting as tag_setting_value,\n unit as tag_unit,\n category as tag_category,\n vartype as tag_vartype,\n numeric_value,\n case\n when numeric_value is null then null\n when unit = '8kB' then numeric_value * 8192\n when unit = 'kB' then numeric_value * 1024\n when unit = 'MB' then numeric_value * 1024 * 1024\n when unit = 'B' then numeric_value\n when unit = 'ms' then numeric_value::numeric / 1000\n when unit = 's' then numeric_value::numeric\n when unit = 'min' then numeric_value::numeric * 60\n else null\n end as setting_normalized,\n case unit\n when '8kB' then 'bytes'\n when 'kB' then 'bytes'\n when 'MB' then 'bytes'\n when 'B' then 'bytes'\n when 'ms' then 'seconds'\n when 's' then 'seconds'\n when 'min' then 'seconds'\n else null\n end as unit_normalized,\n case when is_default_bool then 1 else 0 end as is_default,\n 1 as configured\nfrom with_numeric",
|
|
24
|
+
},
|
|
25
|
+
gauges: ["*"],
|
|
26
|
+
statement_timeout_seconds: 15,
|
|
27
|
+
},
|
|
28
|
+
"db_stats": {
|
|
29
|
+
description: "Retrieves key statistics from the PostgreSQL `pg_stat_database` view, providing insights into the current database's performance. It returns the number of backends, transaction commits and rollbacks, buffer reads and hits, tuple statistics, conflicts, temporary files and bytes, deadlocks, block read and write times, postmaster uptime, backup duration, recovery status, system identifier, and invalid indexes. This metric helps administrators monitor database activity and performance.",
|
|
30
|
+
sqls: {
|
|
31
|
+
11: "select /* pgwatch_generated */\n (extract(epoch from now()) * 1e9)::int8 as epoch_ns,\n current_database() as tag_datname,\n numbackends,\n xact_commit,\n xact_rollback,\n blks_read,\n blks_hit,\n tup_returned,\n tup_fetched,\n tup_inserted,\n tup_updated,\n tup_deleted,\n conflicts,\n temp_files,\n temp_bytes,\n deadlocks,\n blk_read_time,\n blk_write_time,\n extract(epoch from (now() - pg_postmaster_start_time()))::int8 as postmaster_uptime_s,\n case when pg_is_in_recovery() then 1 else 0 end as in_recovery_int,\n system_identifier::text as tag_sys_id,\n (select count(*) from pg_index i\n where not indisvalid\n and not exists ( /* leave out ones that are being actively rebuilt */\n select * from pg_locks l\n join pg_stat_activity a using (pid)\n where l.relation = i.indexrelid\n and a.state = 'active'\n and a.query ~* 'concurrently'\n )) as invalid_indexes\nfrom\n pg_stat_database, pg_control_system()\nwhere\n datname = current_database()",
|
|
32
|
+
12: "select /* pgwatch_generated */\n (extract(epoch from now()) * 1e9)::int8 as epoch_ns,\n current_database() as tag_datname,\n numbackends,\n xact_commit,\n xact_rollback,\n blks_read,\n blks_hit,\n tup_returned,\n tup_fetched,\n tup_inserted,\n tup_updated,\n tup_deleted,\n conflicts,\n temp_files,\n temp_bytes,\n deadlocks,\n blk_read_time,\n blk_write_time,\n extract(epoch from (now() - pg_postmaster_start_time()))::int8 as postmaster_uptime_s,\n extract(epoch from (now() - pg_backup_start_time()))::int8 as backup_duration_s,\n checksum_failures,\n extract(epoch from (now() - checksum_last_failure))::int8 as checksum_last_failure_s,\n case when pg_is_in_recovery() then 1 else 0 end as in_recovery_int,\n system_identifier::text as tag_sys_id,\n (select count(*) from pg_index i\n where not indisvalid\n and not exists ( /* leave out ones that are being actively rebuilt */\n select * from pg_locks l\n join pg_stat_activity a using (pid)\n where l.relation = i.indexrelid\n and a.state = 'active'\n and a.query ~* 'concurrently'\n )) as invalid_indexes\nfrom\n pg_stat_database, pg_control_system()\nwhere\n datname = current_database()",
|
|
33
|
+
14: "select /* pgwatch_generated */\n (extract(epoch from now()) * 1e9)::int8 as epoch_ns,\n current_database() as tag_datname,\n numbackends,\n xact_commit,\n xact_rollback,\n blks_read,\n blks_hit,\n tup_returned,\n tup_fetched,\n tup_inserted,\n tup_updated,\n tup_deleted,\n conflicts,\n temp_files,\n temp_bytes,\n deadlocks,\n blk_read_time,\n blk_write_time,\n extract(epoch from (now() - pg_postmaster_start_time()))::int8 as postmaster_uptime_s,\n extract(epoch from (now() - pg_backup_start_time()))::int8 as backup_duration_s,\n checksum_failures,\n extract(epoch from (now() - checksum_last_failure))::int8 as checksum_last_failure_s,\n case when pg_is_in_recovery() then 1 else 0 end as in_recovery_int,\n system_identifier::text as tag_sys_id,\n session_time::int8,\n active_time::int8,\n idle_in_transaction_time::int8,\n sessions,\n sessions_abandoned,\n sessions_fatal,\n sessions_killed,\n (select count(*) from pg_index i\n where not indisvalid\n and not exists ( /* leave out ones that are being actively rebuilt */\n select * from pg_locks l\n join pg_stat_activity a using (pid)\n where l.relation = i.indexrelid\n and a.state = 'active'\n and a.query ~* 'concurrently'\n )) as invalid_indexes\nfrom\n pg_stat_database, pg_control_system()\nwhere\n datname = current_database()",
|
|
34
|
+
15: "select /* pgwatch_generated */\n (extract(epoch from now()) * 1e9)::int8 as epoch_ns,\n current_database() as tag_datname,\n numbackends,\n xact_commit,\n xact_rollback,\n blks_read,\n blks_hit,\n tup_returned,\n tup_fetched,\n tup_inserted,\n tup_updated,\n tup_deleted,\n conflicts,\n temp_files,\n temp_bytes,\n deadlocks,\n blk_read_time,\n blk_write_time,\n extract(epoch from (now() - pg_postmaster_start_time()))::int8 as postmaster_uptime_s,\n checksum_failures,\n extract(epoch from (now() - checksum_last_failure))::int8 as checksum_last_failure_s,\n case when pg_is_in_recovery() then 1 else 0 end as in_recovery_int,\n system_identifier::text as tag_sys_id,\n session_time::int8,\n active_time::int8,\n idle_in_transaction_time::int8,\n sessions,\n sessions_abandoned,\n sessions_fatal,\n sessions_killed,\n (select count(*) from pg_index i\n where not indisvalid\n and not exists ( /* leave out ones that are being actively rebuilt */\n select * from pg_locks l\n join pg_stat_activity a using (pid)\n where l.relation = i.indexrelid\n and a.state = 'active'\n and a.query ~* 'concurrently'\n )) as invalid_indexes\nfrom\n pg_stat_database, pg_control_system()\nwhere\n datname = current_database()",
|
|
35
|
+
},
|
|
36
|
+
gauges: ["*"],
|
|
37
|
+
statement_timeout_seconds: 15,
|
|
38
|
+
},
|
|
39
|
+
"db_size": {
|
|
40
|
+
description: "Retrieves the size of the current database and the size of the `pg_catalog` schema, providing insights into the storage usage of the database. It returns the size in bytes for both the current database and the catalog schema. This metric helps administrators monitor database size and storage consumption.",
|
|
41
|
+
sqls: {
|
|
42
|
+
11: "select /* pgwatch_generated */\n (extract(epoch from now()) * 1e9)::int8 as epoch_ns,\n current_database() as tag_datname,\n pg_database_size(current_database()) as size_b,\n (select sum(pg_total_relation_size(c.oid))::int8\n from pg_class c join pg_namespace n on n.oid = c.relnamespace\n where nspname = 'pg_catalog' and relkind = 'r'\n ) as catalog_size_b",
|
|
43
|
+
},
|
|
44
|
+
gauges: ["*"],
|
|
45
|
+
statement_timeout_seconds: 300,
|
|
46
|
+
},
|
|
47
|
+
"pg_invalid_indexes": {
|
|
48
|
+
description: "This metric identifies invalid indexes in the database. It provides insights into the number of invalid indexes and their details. This metric helps administrators identify and fix invalid indexes to improve database performance.",
|
|
49
|
+
sqls: {
|
|
50
|
+
11: "with fk_indexes as ( /* pgwatch_generated */\n select\n schemaname as tag_schema_name,\n (indexrelid::regclass)::text as tag_index_name,\n (relid::regclass)::text as tag_table_name,\n (confrelid::regclass)::text as tag_fk_table_ref,\n array_to_string(indclass, ', ') as tag_opclasses\n from\n pg_stat_all_indexes\n join pg_index using (indexrelid)\n left join pg_constraint\n on array_to_string(indkey, ',') = array_to_string(conkey, ',')\n and schemaname = (connamespace::regnamespace)::text\n and conrelid = relid\n and contype = 'f'\n where idx_scan = 0\n and indisunique is false\n and conkey is not null --conkey is not null then true else false end as is_fk_idx\n), data as (\n select\n pci.relname as tag_index_name,\n pn.nspname as tag_schema_name,\n pct.relname as tag_table_name,\n quote_ident(pn.nspname) as tag_schema_name,\n quote_ident(pci.relname) as tag_index_name,\n quote_ident(pct.relname) as tag_table_name,\n coalesce(nullif(quote_ident(pn.nspname), 'public') || '.', '') || quote_ident(pct.relname) as tag_relation_name,\n pg_relation_size(pidx.indexrelid) index_size_bytes,\n ((\n select count(1)\n from fk_indexes fi\n where\n fi.tag_fk_table_ref = pct.relname\n and fi.tag_opclasses like (array_to_string(pidx.indclass, ', ') || '%')\n ) > 0)::int as supports_fk\n from pg_index pidx\n join pg_class as pci on pci.oid = pidx.indexrelid\n join pg_class as pct on pct.oid = pidx.indrelid\n left join pg_namespace pn on pn.oid = pct.relnamespace\n where pidx.indisvalid = false\n), data_total as (\n select\n sum(index_size_bytes) as index_size_bytes_sum\n from data\n), num_data as (\n select\n row_number() over () num,\n data.*\n from data\n)\nselect\n (extract(epoch from now()) * 1e9)::int8 as epoch_ns,\n current_database() as tag_datname,\n num_data.*\nfrom num_data\nlimit 1000;\n",
|
|
51
|
+
},
|
|
52
|
+
gauges: ["*"],
|
|
53
|
+
statement_timeout_seconds: 15,
|
|
54
|
+
},
|
|
55
|
+
"unused_indexes": {
|
|
56
|
+
description: "This metric identifies unused indexes in the database. It provides insights into the number of unused indexes and their details. This metric helps administrators identify and fix unused indexes to improve database performance.",
|
|
57
|
+
sqls: {
|
|
58
|
+
11: "with fk_indexes as ( /* pgwatch_generated */\n select\n n.nspname as schema_name,\n ci.relname as index_name,\n cr.relname as table_name,\n (confrelid::regclass)::text as fk_table_ref,\n array_to_string(indclass, ', ') as opclasses\n from pg_index i\n join pg_class ci on ci.oid = i.indexrelid and ci.relkind = 'i'\n join pg_class cr on cr.oid = i.indrelid and cr.relkind = 'r'\n join pg_namespace n on n.oid = ci.relnamespace\n join pg_constraint cn on cn.conrelid = cr.oid\n left join pg_stat_all_indexes as si on si.indexrelid = i.indexrelid\n where\n contype = 'f'\n and i.indisunique is false\n and conkey is not null\n and ci.relpages > 5\n and si.idx_scan < 10\n), table_scans as (\n select relid,\n tables.idx_scan + tables.seq_scan as all_scans,\n ( tables.n_tup_ins + tables.n_tup_upd + tables.n_tup_del ) as writes,\n pg_relation_size(relid) as table_size\n from pg_stat_all_tables as tables\n join pg_class c on c.oid = relid\n where c.relpages > 5\n), indexes as (\n select\n i.indrelid,\n i.indexrelid,\n n.nspname as schema_name,\n cr.relname as table_name,\n ci.relname as index_name,\n si.idx_scan,\n pg_relation_size(i.indexrelid) as index_bytes,\n ci.relpages,\n (case when a.amname = 'btree' then true else false end) as idx_is_btree,\n array_to_string(i.indclass, ', ') as opclasses\n from pg_index i\n join pg_class ci on ci.oid = i.indexrelid and ci.relkind = 'i'\n join pg_class cr on cr.oid = i.indrelid and cr.relkind = 'r'\n join pg_namespace n on n.oid = ci.relnamespace\n join pg_am a on ci.relam = a.oid\n left join pg_stat_all_indexes as si on si.indexrelid = i.indexrelid\n where\n i.indisunique = false\n and i.indisvalid = true\n and ci.relpages > 5\n), index_ratios as (\n select\n i.indexrelid as index_id,\n i.schema_name,\n i.table_name,\n i.index_name,\n idx_scan,\n all_scans,\n round(( case when all_scans = 0 then 0.0::numeric\n else idx_scan::numeric/all_scans * 100 end), 2) as index_scan_pct,\n writes,\n round((case when writes = 0 then idx_scan::numeric else idx_scan::numeric/writes end), 2)\n as scans_per_write,\n index_bytes as index_size_bytes,\n table_size as table_size_bytes,\n i.relpages,\n idx_is_btree,\n i.opclasses,\n (\n select count(1)\n from fk_indexes fi\n where fi.fk_table_ref = i.table_name\n and fi.schema_name = i.schema_name\n and fi.opclasses like (i.opclasses || '%')\n ) > 0 as supports_fk\n from indexes i\n join table_scans ts on ts.relid = i.indrelid\n)\nselect\n 'Never Used Indexes' as tag_reason,\n current_database() as tag_datname,\n index_id,\n schema_name as tag_schema_name,\n table_name as tag_table_name,\n index_name as tag_index_name,\n pg_get_indexdef(index_id) as index_definition,\n idx_scan,\n all_scans,\n index_scan_pct,\n writes,\n scans_per_write,\n index_size_bytes,\n table_size_bytes,\n relpages,\n idx_is_btree,\n opclasses as tag_opclasses,\n supports_fk\nfrom index_ratios\nwhere\n idx_scan = 0\n and idx_is_btree\norder by index_size_bytes desc\nlimit 1000;\n",
|
|
59
|
+
},
|
|
60
|
+
gauges: ["*"],
|
|
61
|
+
statement_timeout_seconds: 15,
|
|
62
|
+
},
|
|
63
|
+
"redundant_indexes": {
|
|
64
|
+
description: "This metric identifies redundant indexes that can potentially be dropped to save storage space and improve write performance. It analyzes index relationships and finds indexes that are covered by other indexes, considering column order, operator classes, and foreign key constraints. Uses the exact logic from tmp.sql with JSON aggregation and proper thresholds.",
|
|
65
|
+
sqls: {
|
|
66
|
+
11: "with fk_indexes as ( /* pgwatch_generated */\n select\n n.nspname as schema_name,\n ci.relname as index_name,\n cr.relname as table_name,\n (confrelid::regclass)::text as fk_table_ref,\n array_to_string(indclass, ', ') as opclasses\n from pg_index i\n join pg_class ci on ci.oid = i.indexrelid and ci.relkind = 'i'\n join pg_class cr on cr.oid = i.indrelid and cr.relkind = 'r'\n join pg_namespace n on n.oid = ci.relnamespace\n join pg_constraint cn on cn.conrelid = cr.oid\n left join pg_stat_all_indexes as si on si.indexrelid = i.indexrelid\n where\n contype = 'f'\n and i.indisunique is false\n and conkey is not null\n and ci.relpages > 5\n and si.idx_scan < 10\n),\n-- Redundant indexes\nindex_data as (\n select\n *,\n indkey::text as columns,\n array_to_string(indclass, ', ') as opclasses\n from pg_index i\n join pg_class ci on ci.oid = i.indexrelid and ci.relkind = 'i'\n where indisvalid = true and ci.relpages > 5\n), redundant_indexes as (\n select\n i2.indexrelid as index_id,\n tnsp.nspname as schema_name,\n trel.relname as table_name,\n pg_relation_size(trel.oid) as table_size_bytes,\n irel.relname as index_name,\n am1.amname as access_method,\n (i1.indexrelid::regclass)::text as reason,\n i1.indexrelid as reason_index_id,\n pg_get_indexdef(i1.indexrelid) main_index_def,\n pg_relation_size(i1.indexrelid) main_index_size_bytes,\n pg_get_indexdef(i2.indexrelid) index_def,\n pg_relation_size(i2.indexrelid) index_size_bytes,\n s.idx_scan as index_usage,\n quote_ident(tnsp.nspname) as formated_schema_name,\n coalesce(nullif(quote_ident(tnsp.nspname), 'public') || '.', '') || quote_ident(irel.relname) as formated_index_name,\n quote_ident(trel.relname) as formated_table_name,\n coalesce(nullif(quote_ident(tnsp.nspname), 'public') || '.', '') || quote_ident(trel.relname) as formated_relation_name,\n i2.opclasses\n from (\n select indrelid, indexrelid, opclasses, indclass, indexprs, indpred, indisprimary, indisunique, columns\n from index_data\n order by indexrelid\n ) as i1\n join index_data as i2 on (\n i1.indrelid = i2.indrelid -- same table\n and i1.indexrelid <> i2.indexrelid -- NOT same index\n )\n inner join pg_opclass op1 on i1.indclass[0] = op1.oid\n inner join pg_opclass op2 on i2.indclass[0] = op2.oid\n inner join pg_am am1 on op1.opcmethod = am1.oid\n inner join pg_am am2 on op2.opcmethod = am2.oid\n join pg_stat_all_indexes as s on s.indexrelid = i2.indexrelid\n join pg_class as trel on trel.oid = i2.indrelid\n join pg_namespace as tnsp on trel.relnamespace = tnsp.oid\n join pg_class as irel on irel.oid = i2.indexrelid\n where\n not i2.indisprimary -- index 1 is not primary\n and not i2.indisunique -- index 1 is not unique (unique indexes serve constraint purpose)\n and am1.amname = am2.amname -- same access type\n and i1.columns like (i2.columns || '%') -- index 2 includes all columns from index 1\n and i1.opclasses like (i2.opclasses || '%')\n -- index expressions is same\n and pg_get_expr(i1.indexprs, i1.indrelid) is not distinct from pg_get_expr(i2.indexprs, i2.indrelid)\n -- index predicates is same\n and pg_get_expr(i1.indpred, i1.indrelid) is not distinct from pg_get_expr(i2.indpred, i2.indrelid)\n), redundant_indexes_fk as (\n select\n ri.*,\n ((\n select count(1)\n from fk_indexes fi\n where\n fi.fk_table_ref = ri.table_name\n and fi.opclasses like (ri.opclasses || '%')\n ) > 0)::int as supports_fk\n from redundant_indexes ri\n),\n-- Cut recursive links\nredundant_indexes_tmp_num as (\n select row_number() over () num, rig.*\n from redundant_indexes_fk rig\n), redundant_indexes_tmp_links as (\n select\n ri1.*,\n ri2.num as r_num\n from redundant_indexes_tmp_num ri1\n left join redundant_indexes_tmp_num ri2 on ri2.reason_index_id = ri1.index_id and ri1.reason_index_id = ri2.index_id\n), redundant_indexes_tmp_cut as (\n select\n *\n from redundant_indexes_tmp_links\n where num < r_num or r_num is null\n), redundant_indexes_cut_grouped as (\n select\n distinct(num),\n *\n from redundant_indexes_tmp_cut\n order by index_size_bytes desc\n), redundant_indexes_grouped as (\n select\n index_id,\n schema_name as tag_schema_name,\n table_name,\n table_size_bytes,\n index_name as tag_index_name,\n access_method as tag_access_method,\n string_agg(distinct reason, ', ') as tag_reason,\n index_size_bytes,\n index_usage,\n index_def as index_definition,\n formated_index_name as tag_index_name,\n formated_schema_name as tag_schema_name,\n formated_table_name as tag_table_name,\n formated_relation_name as tag_relation_name,\n supports_fk::int as supports_fk,\n json_agg(\n distinct jsonb_build_object(\n 'index_name', reason,\n 'index_definition', main_index_def,\n 'index_size_bytes', main_index_size_bytes\n )\n )::text as redundant_to_json\n from redundant_indexes_cut_grouped\n group by\n index_id,\n table_size_bytes,\n schema_name,\n table_name,\n index_name,\n access_method,\n index_def,\n index_size_bytes,\n index_usage,\n formated_index_name,\n formated_schema_name,\n formated_table_name,\n formated_relation_name,\n supports_fk\n order by index_size_bytes desc\n)\nselect * from redundant_indexes_grouped\nlimit 1000;\n",
|
|
67
|
+
},
|
|
68
|
+
gauges: ["*"],
|
|
69
|
+
statement_timeout_seconds: 15,
|
|
70
|
+
},
|
|
71
|
+
"stats_reset": {
|
|
72
|
+
description: "This metric tracks when statistics were last reset at the database level. It provides visibility into the freshness of statistics data, which is essential for understanding the reliability of usage metrics. A recent reset time indicates that usage statistics may not reflect long-term patterns. Note that Postgres tracks stats resets at the database level, not per-index or per-table.",
|
|
73
|
+
sqls: {
|
|
74
|
+
11: "select /* pgwatch_generated */\n datname as tag_database_name,\n extract(epoch from stats_reset)::int as stats_reset_epoch,\n extract(epoch from now() - stats_reset)::int as seconds_since_reset\nfrom pg_stat_database\nwhere datname = current_database()\n and stats_reset is not null;\n",
|
|
75
|
+
},
|
|
76
|
+
gauges: ["stats_reset_epoch","seconds_since_reset"],
|
|
77
|
+
statement_timeout_seconds: 15,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metrics loader for express checkup reports
|
|
3
|
+
*
|
|
4
|
+
* Loads SQL queries from embedded metrics data (generated from metrics.yml at build time).
|
|
5
|
+
* Provides version-aware query selection and row transformation utilities.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { METRICS, MetricDefinition } from "./metrics-embedded";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get SQL query for a specific metric, selecting the appropriate version.
|
|
12
|
+
*
|
|
13
|
+
* @param metricName - Name of the metric (e.g., "settings", "db_stats")
|
|
14
|
+
* @param pgMajorVersion - PostgreSQL major version (default: 16)
|
|
15
|
+
* @returns SQL query string
|
|
16
|
+
* @throws Error if metric not found or no compatible version available
|
|
17
|
+
*/
|
|
18
|
+
export function getMetricSql(metricName: string, pgMajorVersion: number = 16): string {
|
|
19
|
+
const metric = METRICS[metricName];
|
|
20
|
+
|
|
21
|
+
if (!metric) {
|
|
22
|
+
throw new Error(`Metric "${metricName}" not found. Available metrics: ${Object.keys(METRICS).join(", ")}`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Find the best matching version: highest version <= pgMajorVersion
|
|
26
|
+
const availableVersions = Object.keys(metric.sqls)
|
|
27
|
+
.map(v => parseInt(v, 10))
|
|
28
|
+
.sort((a, b) => b - a); // Sort descending
|
|
29
|
+
|
|
30
|
+
const matchingVersion = availableVersions.find(v => v <= pgMajorVersion);
|
|
31
|
+
|
|
32
|
+
if (matchingVersion === undefined) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`No compatible SQL version for metric "${metricName}" with PostgreSQL ${pgMajorVersion}. ` +
|
|
35
|
+
`Available versions: ${availableVersions.join(", ")}`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return metric.sqls[matchingVersion];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Get metric definition including all metadata.
|
|
44
|
+
*
|
|
45
|
+
* @param metricName - Name of the metric
|
|
46
|
+
* @returns MetricDefinition or undefined if not found
|
|
47
|
+
*/
|
|
48
|
+
export function getMetricDefinition(metricName: string): MetricDefinition | undefined {
|
|
49
|
+
return METRICS[metricName];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* List all available metric names.
|
|
54
|
+
*/
|
|
55
|
+
export function listMetricNames(): string[] {
|
|
56
|
+
return Object.keys(METRICS);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Metric names that correspond to express report checks.
|
|
61
|
+
* Maps check IDs and logical names to metric names in the METRICS object.
|
|
62
|
+
*/
|
|
63
|
+
export const METRIC_NAMES = {
|
|
64
|
+
// Index health checks
|
|
65
|
+
H001: "pg_invalid_indexes",
|
|
66
|
+
H002: "unused_indexes",
|
|
67
|
+
H004: "redundant_indexes",
|
|
68
|
+
// Settings and version info (A002, A003, A007, A013)
|
|
69
|
+
settings: "settings",
|
|
70
|
+
// Database statistics (A004)
|
|
71
|
+
dbStats: "db_stats",
|
|
72
|
+
dbSize: "db_size",
|
|
73
|
+
// Stats reset info (H002)
|
|
74
|
+
statsReset: "stats_reset",
|
|
75
|
+
} as const;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Transform a row from metrics query output to JSON report format.
|
|
79
|
+
* Metrics use `tag_` prefix for dimensions; we strip it for JSON reports.
|
|
80
|
+
* Also removes Prometheus-specific fields like epoch_ns, num, tag_datname.
|
|
81
|
+
*/
|
|
82
|
+
export function transformMetricRow(row: Record<string, unknown>): Record<string, unknown> {
|
|
83
|
+
const result: Record<string, unknown> = {};
|
|
84
|
+
|
|
85
|
+
for (const [key, value] of Object.entries(row)) {
|
|
86
|
+
// Skip Prometheus-specific fields
|
|
87
|
+
if (key === "epoch_ns" || key === "num" || key === "tag_datname") {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Strip tag_ prefix
|
|
92
|
+
const newKey = key.startsWith("tag_") ? key.slice(4) : key;
|
|
93
|
+
result[newKey] = value;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Transform settings metric row to the format expected by express reports.
|
|
101
|
+
* The settings metric returns one row per setting with tag_setting_name as key.
|
|
102
|
+
*/
|
|
103
|
+
export function transformSettingsRow(row: Record<string, unknown>): {
|
|
104
|
+
name: string;
|
|
105
|
+
setting: string;
|
|
106
|
+
unit: string;
|
|
107
|
+
category: string;
|
|
108
|
+
vartype: string;
|
|
109
|
+
is_default: boolean;
|
|
110
|
+
} {
|
|
111
|
+
return {
|
|
112
|
+
name: String(row.tag_setting_name || ""),
|
|
113
|
+
setting: String(row.tag_setting_value || ""),
|
|
114
|
+
unit: String(row.tag_unit || ""),
|
|
115
|
+
category: String(row.tag_category || ""),
|
|
116
|
+
vartype: String(row.tag_vartype || ""),
|
|
117
|
+
is_default: row.is_default === 1 || row.is_default === true,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Re-export types for convenience
|
|
122
|
+
export type { MetricDefinition } from "./metrics-embedded";
|
|
123
|
+
|
|
124
|
+
// Legacy export for backward compatibility
|
|
125
|
+
export function loadMetricsYml(): { metrics: Record<string, unknown> } {
|
|
126
|
+
return { metrics: METRICS };
|
|
127
|
+
}
|
package/lib/util.ts
CHANGED
|
@@ -1,3 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Map of HTTP status codes to human-friendly messages.
|
|
3
|
+
*/
|
|
4
|
+
const HTTP_STATUS_MESSAGES: Record<number, string> = {
|
|
5
|
+
400: "Bad Request",
|
|
6
|
+
401: "Unauthorized - check your API key",
|
|
7
|
+
403: "Forbidden - access denied",
|
|
8
|
+
404: "Not Found",
|
|
9
|
+
408: "Request Timeout",
|
|
10
|
+
429: "Too Many Requests - rate limited",
|
|
11
|
+
500: "Internal Server Error",
|
|
12
|
+
502: "Bad Gateway - server temporarily unavailable",
|
|
13
|
+
503: "Service Unavailable - server temporarily unavailable",
|
|
14
|
+
504: "Gateway Timeout - server temporarily unavailable",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Check if a string looks like HTML content.
|
|
19
|
+
*/
|
|
20
|
+
function isHtmlContent(text: string): boolean {
|
|
21
|
+
const trimmed = text.trim();
|
|
22
|
+
return trimmed.startsWith("<!DOCTYPE") || trimmed.startsWith("<html") || trimmed.startsWith("<HTML");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Format an HTTP error response into a clean, developer-friendly message.
|
|
27
|
+
* Handles HTML error pages (e.g., from Cloudflare) by showing just the status code and message.
|
|
28
|
+
*/
|
|
29
|
+
export function formatHttpError(operation: string, status: number, responseBody?: string): string {
|
|
30
|
+
const statusMessage = HTTP_STATUS_MESSAGES[status] || "Request failed";
|
|
31
|
+
let errMsg = `${operation}: HTTP ${status} - ${statusMessage}`;
|
|
32
|
+
|
|
33
|
+
if (responseBody) {
|
|
34
|
+
// If it's HTML (like Cloudflare error pages), don't dump the raw HTML
|
|
35
|
+
if (isHtmlContent(responseBody)) {
|
|
36
|
+
// Just use the status message, don't append HTML
|
|
37
|
+
return errMsg;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Try to parse as JSON for structured error info
|
|
41
|
+
try {
|
|
42
|
+
const errObj = JSON.parse(responseBody);
|
|
43
|
+
// Extract common error message fields
|
|
44
|
+
const message = errObj.message || errObj.error || errObj.detail;
|
|
45
|
+
if (message && typeof message === "string") {
|
|
46
|
+
errMsg += `\n${message}`;
|
|
47
|
+
} else {
|
|
48
|
+
errMsg += `\n${JSON.stringify(errObj, null, 2)}`;
|
|
49
|
+
}
|
|
50
|
+
} catch {
|
|
51
|
+
// Plain text error - append it if it's short and useful
|
|
52
|
+
const trimmed = responseBody.trim();
|
|
53
|
+
if (trimmed.length > 0 && trimmed.length < 500) {
|
|
54
|
+
errMsg += `\n${trimmed}`;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return errMsg;
|
|
60
|
+
}
|
|
61
|
+
|
|
1
62
|
export function maskSecret(secret: string): string {
|
|
2
63
|
if (!secret) return "";
|
|
3
64
|
if (secret.length <= 8) return "****";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postgresai",
|
|
3
|
-
"version": "0.14.0-dev.
|
|
3
|
+
"version": "0.14.0-dev.54",
|
|
4
4
|
"description": "postgres_ai CLI",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"private": false,
|
|
@@ -13,22 +13,26 @@
|
|
|
13
13
|
"url": "https://gitlab.com/postgres-ai/postgres_ai/-/issues"
|
|
14
14
|
},
|
|
15
15
|
"bin": {
|
|
16
|
-
"postgres-ai": "./dist/bin/postgres-ai.js",
|
|
17
16
|
"postgresai": "./dist/bin/postgres-ai.js",
|
|
18
17
|
"pgai": "./dist/bin/postgres-ai.js"
|
|
19
18
|
},
|
|
19
|
+
"exports": {
|
|
20
|
+
".": "./dist/bin/postgres-ai.js",
|
|
21
|
+
"./cli": "./dist/bin/postgres-ai.js"
|
|
22
|
+
},
|
|
20
23
|
"type": "module",
|
|
21
24
|
"engines": {
|
|
22
25
|
"node": ">=18"
|
|
23
26
|
},
|
|
24
27
|
"scripts": {
|
|
25
|
-
"
|
|
28
|
+
"embed-metrics": "bun run scripts/embed-metrics.ts",
|
|
29
|
+
"build": "bun run embed-metrics && bun build ./bin/postgres-ai.ts --outdir ./dist/bin --target node && node -e \"const fs=require('fs');const f='./dist/bin/postgres-ai.js';fs.writeFileSync(f,fs.readFileSync(f,'utf8').replace('#!/usr/bin/env bun','#!/usr/bin/env node'))\"",
|
|
26
30
|
"prepublishOnly": "npm run build",
|
|
27
31
|
"start": "bun ./bin/postgres-ai.ts --help",
|
|
28
32
|
"start:node": "node ./dist/bin/postgres-ai.js --help",
|
|
29
|
-
"dev": "bun --watch ./bin/postgres-ai.ts",
|
|
30
|
-
"test": "bun test",
|
|
31
|
-
"typecheck": "bunx tsc --noEmit"
|
|
33
|
+
"dev": "bun run embed-metrics && bun --watch ./bin/postgres-ai.ts",
|
|
34
|
+
"test": "bun run embed-metrics && bun test",
|
|
35
|
+
"typecheck": "bun run embed-metrics && bunx tsc --noEmit"
|
|
32
36
|
},
|
|
33
37
|
"dependencies": {
|
|
34
38
|
"@modelcontextprotocol/sdk": "^1.20.2",
|
|
@@ -40,6 +44,8 @@
|
|
|
40
44
|
"@types/bun": "^1.1.14",
|
|
41
45
|
"@types/js-yaml": "^4.0.9",
|
|
42
46
|
"@types/pg": "^8.15.6",
|
|
47
|
+
"ajv": "^8.17.1",
|
|
48
|
+
"ajv-formats": "^3.0.1",
|
|
43
49
|
"typescript": "^5.3.3"
|
|
44
50
|
},
|
|
45
51
|
"publishConfig": {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# postgres-ai
|
|
2
|
+
|
|
3
|
+
This is a wrapper package for [postgresai](https://www.npmjs.com/package/postgresai).
|
|
4
|
+
|
|
5
|
+
## Prefer installing postgresai directly
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g postgresai
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
This gives you two commands:
|
|
12
|
+
- `postgresai` — canonical, discoverable
|
|
13
|
+
- `pgai` — short and convenient
|
|
14
|
+
|
|
15
|
+
## Why this package exists
|
|
16
|
+
|
|
17
|
+
This package exists for discoverability on npm. If you search for "postgres-ai", you'll find this package which depends on and forwards to `postgresai`.
|
|
18
|
+
|
|
19
|
+
Installing this package (`npm install -g postgres-ai`) will install both packages, giving you all three command aliases:
|
|
20
|
+
- `postgres-ai` (from this package)
|
|
21
|
+
- `postgresai` (from the main package)
|
|
22
|
+
- `pgai` (from the main package)
|
|
23
|
+
|
|
24
|
+
## Documentation
|
|
25
|
+
|
|
26
|
+
See the main package for full documentation: https://www.npmjs.com/package/postgresai
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* postgres-ai wrapper - forwards all commands to postgresai CLI
|
|
4
|
+
*
|
|
5
|
+
* This package exists for discoverability. For direct installation,
|
|
6
|
+
* prefer: npm install -g postgresai
|
|
7
|
+
*/
|
|
8
|
+
const { spawn } = require('child_process');
|
|
9
|
+
|
|
10
|
+
// Find postgresai binary from the dependency
|
|
11
|
+
// Uses the "cli" export defined in postgresai's package.json
|
|
12
|
+
const postgresaiBin = require.resolve('postgresai/cli');
|
|
13
|
+
|
|
14
|
+
// Forward all arguments to postgresai
|
|
15
|
+
const child = spawn(process.execPath, [postgresaiBin, ...process.argv.slice(2)], {
|
|
16
|
+
stdio: 'inherit',
|
|
17
|
+
env: process.env,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
child.on('close', (code) => {
|
|
21
|
+
process.exit(code ?? 0);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
child.on('error', (err) => {
|
|
25
|
+
console.error(`Failed to start postgresai: ${err.message}`);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "postgres-ai",
|
|
3
|
+
"version": "0.0.0-dev.0",
|
|
4
|
+
"description": "PostgresAI CLI (wrapper package - prefer installing postgresai directly)",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"private": false,
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://gitlab.com/postgres-ai/postgres_ai.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://gitlab.com/postgres-ai/postgres_ai",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://gitlab.com/postgres-ai/postgres_ai/-/issues"
|
|
14
|
+
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"postgres-ai": "./bin/postgres-ai.js"
|
|
17
|
+
},
|
|
18
|
+
"engines": {
|
|
19
|
+
"node": ">=18"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"postgresai": ">=0.12.0"
|
|
23
|
+
},
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
}
|
|
27
|
+
}
|