postgresai 0.18.1-rc.3 → 0.18.1-rc.5

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.
@@ -13538,7 +13538,7 @@ var {
13538
13538
  // package.json
13539
13539
  var package_default = {
13540
13540
  name: "postgresai",
13541
- version: "0.18.1-rc.3",
13541
+ version: "0.18.1-rc.5",
13542
13542
  description: "postgres_ai CLI",
13543
13543
  license: "Apache-2.0",
13544
13544
  private: false,
@@ -16898,7 +16898,7 @@ var Result = import_lib.default.Result;
16898
16898
  var TypeOverrides = import_lib.default.TypeOverrides;
16899
16899
  var defaults = import_lib.default.defaults;
16900
16900
  // package.json
16901
- var version = "0.18.1-rc.3";
16901
+ var version = "0.18.1-rc.5";
16902
16902
  var package_default2 = {
16903
16903
  name: "postgresai",
16904
16904
  version,
@@ -40569,23 +40569,12 @@ where
40569
40569
  description: "This metric identifies invalid indexes in the database with decision tree data for remediation. It provides insights into whether to DROP (if duplicate exists), RECREATE (if backs constraint), or flag as UNCERTAIN (if additional RCA is needed to check query plans). Decision tree: 1) Valid duplicate exists -> DROP, 2) Backs PK/UNIQUE constraint -> RECREATE, 3) Table < 10K rows -> RECREATE (small tables rebuild quickly, typically under 1 second), 4) Otherwise -> UNCERTAIN (need query plan analysis to assess impact). Adapts the top-N + `'$other$'` bucket pattern from !262 to this metric: ranks invalid indexes by `index_size_bytes desc` (ties broken by schema, table, then index name for stability), keeps the top 100, and folds the tail into a single `'$other$'` row whose `index_size_bytes` / `table_row_estimate` are summed and whose tag columns carry the literal `'$other$'` sentinel. The `'$other$'` row is omitted entirely (via `HAVING count(*) > 0`) when all invalid indexes fit within the top-100 cap, so its absence on healthy clusters is normal. System schemas (`pg_catalog`, `information_schema`, `pg_toast`, `pg_temp_*`, `pg_toast_temp_*`) are excluded: their indexes cannot be dropped and must never be recommended for removal.",
40570
40570
  sqls: {
40571
40571
  11: `with fk_indexes as materialized ( /* pgwatch_generated */
40572
- select
40573
- schemaname as schema_name,
40574
- indexrelid,
40575
- (indexrelid::regclass)::text as index_name,
40576
- (relid::regclass)::text as table_name,
40577
- (confrelid::regclass)::text as fk_table_ref,
40578
- array_to_string(indclass, ', ') as opclasses
40579
- from pg_stat_all_indexes
40580
- join pg_index using (indexrelid)
40581
- left join pg_constraint
40582
- on array_to_string(indkey, ',') = array_to_string(conkey, ',')
40583
- and schemaname = (connamespace::regnamespace)::text
40584
- and conrelid = relid
40585
- and contype = 'f'
40586
- where idx_scan = 0
40587
- and indisunique is false
40588
- and conkey is not null
40572
+ -- An index backs a foreign key when the key is on the index's own table
40573
+ -- and the key's columns are the index's leading columns (#476).
40574
+ select distinct i.indexrelid
40575
+ from pg_index i
40576
+ join pg_constraint c on c.conrelid = i.indrelid and c.contype = 'f'
40577
+ where (string_to_array(i.indkey::text, ' ')::int2[])[1:cardinality(c.conkey)] @> c.conkey
40589
40578
  ),
40590
40579
  -- Find valid indexes that could be duplicates (same table, same columns)
40591
40580
  valid_duplicates as (
@@ -40612,6 +40601,13 @@ data as (
40612
40601
  -- Constraint info
40613
40602
  pidx.indisprimary as is_pk,
40614
40603
  pidx.indisunique as is_unique,
40604
+ -- H001-O1: false = writes do not maintain it (the build failed before its first scan ended)
40605
+ pidx.indisready as is_ready,
40606
+ -- H001-O2 / §9: an index whose concurrent build is still running reads invalid too
40607
+ exists (
40608
+ select from pg_stat_progress_create_index as p
40609
+ where p.index_relid = pidx.indexrelid
40610
+ ) as build_in_progress,
40615
40611
  con.conname as constraint_name,
40616
40612
  -- Table row estimate
40617
40613
  pct.reltuples::bigint as table_row_estimate,
@@ -40620,12 +40616,7 @@ data as (
40620
40616
  vd.valid_index_name,
40621
40617
  vd.valid_index_definition,
40622
40618
  -- FK support check
40623
- ((
40624
- select count(1)
40625
- from fk_indexes fi
40626
- where fi.fk_table_ref = pct.relname
40627
- and fi.opclasses like (array_to_string(pidx.indclass, ', ') || '%')
40628
- ) > 0)::int as supports_fk
40619
+ (exists (select from fk_indexes fi where fi.indexrelid = pidx.indexrelid))::int as supports_fk
40629
40620
  from pg_index pidx
40630
40621
  join pg_class pci on pci.oid = pidx.indexrelid
40631
40622
  join pg_class pct on pct.oid = pidx.indrelid
@@ -40660,6 +40651,8 @@ select
40660
40651
  index_size_bytes,
40661
40652
  is_pk,
40662
40653
  is_unique,
40654
+ is_ready,
40655
+ build_in_progress,
40663
40656
  constraint_name,
40664
40657
  table_row_estimate,
40665
40658
  has_valid_duplicate,
@@ -40681,6 +40674,8 @@ select
40681
40674
  coalesce(sum(index_size_bytes), 0)::int8 as index_size_bytes,
40682
40675
  false as is_pk,
40683
40676
  false as is_unique,
40677
+ bool_and(is_ready) as is_ready,
40678
+ bool_or(build_in_progress) as build_in_progress,
40684
40679
  '$other$'::text as constraint_name,
40685
40680
  coalesce(sum(table_row_estimate), 0)::bigint as table_row_estimate,
40686
40681
  bool_or(has_valid_duplicate) as has_valid_duplicate,
@@ -40700,24 +40695,12 @@ having count(*) > 0;
40700
40695
  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. Adapts the top-N + `'$other$'` bucket pattern from !262 to this metric: within the `idx_scan = 0 AND idx_is_btree` filter, ranks indexes by `index_size_bytes desc` (ties broken by schema, table, index name), keeps the top 100, and folds the tail into a single `'$other$'` row. Counter columns (`idx_scan`, `all_scans`, `writes`, `index_size_bytes`, `table_size_bytes`, `relpages`) are summed across the tail; ratio columns (`index_scan_pct`, `scans_per_write`) and the `supports_fk` boolean are deliberately zeroed/false on the aggregate row because the tail-level average would mislead and the per-row FK relationship has no meaningful aggregate. Tag columns carry the literal `'$other$'` sentinel. The `'$other$'` row is omitted entirely (via `HAVING count(*) > 0`) when ≤100 indexes match the unused filter. System schemas (`pg_catalog`, `information_schema`, `pg_toast`, `pg_temp_*`, `pg_toast_temp_*`) are excluded: their indexes cannot be dropped and must never be recommended for removal.",
40701
40696
  sqls: {
40702
40697
  11: `with fk_indexes as materialized ( /* pgwatch_generated */
40703
- select
40704
- n.nspname as schema_name,
40705
- ci.relname as index_name,
40706
- cr.relname as table_name,
40707
- (confrelid::regclass)::text as fk_table_ref,
40708
- array_to_string(indclass, ', ') as opclasses
40698
+ -- An index backs a foreign key when the key is on the index's own table
40699
+ -- and the key's columns are the index's leading columns (#476).
40700
+ select distinct i.indexrelid
40709
40701
  from pg_index i
40710
- join pg_class ci on ci.oid = i.indexrelid and ci.relkind = 'i'
40711
- join pg_class cr on cr.oid = i.indrelid and cr.relkind = 'r'
40712
- join pg_namespace n on n.oid = ci.relnamespace
40713
- join pg_constraint cn on cn.conrelid = cr.oid
40714
- left join pg_stat_all_indexes as si on si.indexrelid = i.indexrelid
40715
- where
40716
- contype = 'f'
40717
- and i.indisunique is false
40718
- and conkey is not null
40719
- and ci.relpages > 5
40720
- and si.idx_scan < 10
40702
+ join pg_constraint c on c.conrelid = i.indrelid and c.contype = 'f'
40703
+ where (string_to_array(i.indkey::text, ' ')::int2[])[1:cardinality(c.conkey)] @> c.conkey
40721
40704
  ), table_scans as (
40722
40705
  select relid,
40723
40706
  tables.idx_scan + tables.seq_scan as all_scans,
@@ -40769,13 +40752,7 @@ having count(*) > 0;
40769
40752
  i.relpages,
40770
40753
  idx_is_btree,
40771
40754
  i.opclasses,
40772
- (
40773
- select count(1)
40774
- from fk_indexes fi
40775
- where fi.fk_table_ref = i.table_name
40776
- and fi.schema_name = i.schema_name
40777
- and fi.opclasses like (i.opclasses || '%')
40778
- ) > 0 as supports_fk
40755
+ exists (select from fk_indexes fi where fi.indexrelid = i.indexrelid) as supports_fk
40779
40756
  from indexes i
40780
40757
  join table_scans ts on ts.relid = i.indrelid
40781
40758
  )
@@ -40844,24 +40821,12 @@ having count(*) > 0;
40844
40821
  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. Adapts the top-N + `'$other$'` bucket pattern from !262 to this metric: ranks redundant indexes by `index_size_bytes desc` (ties broken by `table_name`), keeps the top 100, and folds the tail into a single `'$other$'` row whose `table_size_bytes`, `index_size_bytes` and `index_usage` columns are summed and whose tag columns carry the literal `'$other$'` sentinel. The `redundant_indexes_grouped` CTE intentionally preserves duplicate column aliases (`tag_schema_name` / `tag_index_name` appear twice — once from the raw name and once from the `formated_*` variant) because the dashboards rely on both spellings; the duplication is preserved on the `'$other$'` row for consistency. The `'$other$'` row is omitted entirely (via `HAVING count(*) > 0`) when there are ≤100 redundant pairs, so its absence on healthy clusters is normal. System schemas (`pg_catalog`, `information_schema`, `pg_toast`, `pg_temp_*`, `pg_toast_temp_*`) are excluded: their indexes cannot be dropped and must never be recommended for removal.",
40845
40822
  sqls: {
40846
40823
  11: `with fk_indexes as materialized ( /* pgwatch_generated */
40847
- select
40848
- n.nspname as schema_name,
40849
- ci.relname as index_name,
40850
- cr.relname as table_name,
40851
- (confrelid::regclass)::text as fk_table_ref,
40852
- array_to_string(indclass, ', ') as opclasses
40824
+ -- An index backs a foreign key when the key is on the index's own table
40825
+ -- and the key's columns are the index's leading columns (#476).
40826
+ select distinct i.indexrelid
40853
40827
  from pg_index i
40854
- join pg_class ci on ci.oid = i.indexrelid and ci.relkind = 'i'
40855
- join pg_class cr on cr.oid = i.indrelid and cr.relkind = 'r'
40856
- join pg_namespace n on n.oid = ci.relnamespace
40857
- join pg_constraint cn on cn.conrelid = cr.oid
40858
- left join pg_stat_all_indexes as si on si.indexrelid = i.indexrelid
40859
- where
40860
- contype = 'f'
40861
- and i.indisunique is false
40862
- and conkey is not null
40863
- and ci.relpages > 5
40864
- and si.idx_scan < 10
40828
+ join pg_constraint c on c.conrelid = i.indrelid and c.contype = 'f'
40829
+ where (string_to_array(i.indkey::text, ' ')::int2[])[1:cardinality(c.conkey)] @> c.conkey
40865
40830
  ),
40866
40831
  -- Redundant indexes
40867
40832
  index_data as (
@@ -40927,13 +40892,7 @@ index_data as (
40927
40892
  ), redundant_indexes_fk as (
40928
40893
  select
40929
40894
  ri.*,
40930
- ((
40931
- select count(1)
40932
- from fk_indexes fi
40933
- where
40934
- fi.fk_table_ref = ri.table_name
40935
- and fi.opclasses like (ri.opclasses || '%')
40936
- ) > 0)::int as supports_fk
40895
+ (exists (select from fk_indexes fi where fi.indexrelid = ri.index_id))::int as supports_fk
40937
40896
  from redundant_indexes ri
40938
40897
  ),
40939
40898
  -- Cut recursive links
@@ -41167,7 +41126,7 @@ order by cmb.total_relation_size_b desc, cmb.schemaname, cmb.relname
41167
41126
  from
41168
41127
  pg_catalog.pg_database d
41169
41128
  where
41170
- d.datallowconn
41129
+ d.datconnlimit <> -2
41171
41130
  `
41172
41131
  },
41173
41132
  gauges: ["age_datfrozenxid", "age_datminmxid"],
@@ -43809,7 +43768,9 @@ async function getInvalidIndexes(client, pgMajorVersion = 16) {
43809
43768
  table_row_estimate: parseInt(String(transformed.table_row_estimate || 0), 10),
43810
43769
  has_valid_duplicate: toBool(transformed.has_valid_duplicate),
43811
43770
  valid_duplicate_name: transformed.valid_index_name ? String(transformed.valid_index_name) : null,
43812
- valid_duplicate_definition: transformed.valid_index_definition ? String(transformed.valid_index_definition) : null
43771
+ valid_duplicate_definition: transformed.valid_index_definition ? String(transformed.valid_index_definition) : null,
43772
+ ...transformed.is_ready !== undefined ? { is_ready: toBool(transformed.is_ready) } : {},
43773
+ ...transformed.build_in_progress !== undefined ? { build_in_progress: toBool(transformed.build_in_progress) } : {}
43813
43774
  };
43814
43775
  }).filter((index) => !isSystemSchema(index.schema_name));
43815
43776
  }
@@ -44739,19 +44700,259 @@ async function generateD004(client, nodeName) {
44739
44700
  };
44740
44701
  return report;
44741
44702
  }
44703
+ var D001_SETTING_PATTERN = "^(log_.*|logging_collector|auto_explain\\..*|shared_preload_libraries|session_preload_libraries|deadlock_timeout)$";
44704
+ var D001_OVERRIDE_PATTERN = /^(log_.*|auto_explain\..*|session_preload_libraries|statement_timeout)$/;
44705
+ var D001_BASE_THRESHOLDS_MS = [1000, 1e4];
44706
+ var D001_MAX_ROLES = 60;
44707
+ var D001_SETTINGS_SQL = `
44708
+ select name, reset_val as setting, coalesce(unit, '') as unit, category, context, vartype, source,
44709
+ boot_val, pending_restart
44710
+ from pg_settings
44711
+ where name ~ $1
44712
+ order by name`;
44713
+ var D001_OVERRIDES_SQL = `
44714
+ select r.rolname as role, d.datname as database, s.setconfig as config
44715
+ from pg_db_role_setting as s
44716
+ left join pg_roles as r on r.oid = s.setrole
44717
+ left join pg_database as d on d.oid = s.setdatabase
44718
+ order by r.rolname nulls first, d.datname nulls first`;
44719
+ var D001_LOGIN_ROLES_SQL = `
44720
+ select r.rolname as role, r.rolsuper as superuser,
44721
+ coalesce((select array_agg(g.rolname::text order by g.rolname)
44722
+ from pg_auth_members as m join pg_roles as g on g.oid = m.roleid
44723
+ where m.member = r.oid), '{}') as member_of,
44724
+ count(a.pid) filter (where a.backend_type = 'client backend') as sessions,
44725
+ min(a.backend_start) filter (where a.backend_type = 'client backend') as oldest_session_start,
44726
+ (array_agg(distinct a.application_name) filter (where a.backend_type = 'client backend'
44727
+ and a.application_name <> ''))[1:5] as applications
44728
+ from pg_roles as r
44729
+ left join pg_stat_activity as a on a.usesysid = r.oid
44730
+ where r.rolcanlogin and r.rolname !~ '^pg_'
44731
+ group by r.oid, r.rolname, r.rolsuper
44732
+ order by count(a.pid) filter (where a.backend_type = 'client backend') desc, r.rolname
44733
+ limit 60`;
44734
+ var D001_SEES_SESSIONS_SQL = "select pg_has_role(current_user, 'pg_read_all_stats', 'usage') as visible";
44735
+ function d001StatementsSql(pgss) {
44736
+ return `
44737
+ with s as (
44738
+ select p.userid, p.calls,
44739
+ coalesce((to_jsonb(p) ->> 'max_exec_time'), (to_jsonb(p) ->> 'max_time'))::float8 as max_ms,
44740
+ coalesce(to_jsonb(p) ->> 'minmax_stats_since', to_jsonb(p) ->> 'stats_since') as since
44741
+ from ${pgss} as p
44742
+ )
44743
+ select r.rolname as role, count(*) as groups, sum(s.calls)::bigint as calls, max(s.max_ms) as longest_ms,
44744
+ min(s.since) as stats_since,
44745
+ (select jsonb_object_agg(t::text, (select count(*) from s as x where x.userid = s.userid and x.max_ms >= t) order by t)
44746
+ from unnest($1::float8[]) as t) as groups_at_or_over,
44747
+ (select jsonb_object_agg(t::text, (select coalesce(sum(x.calls), 0) from s as x
44748
+ where x.userid = s.userid and x.max_ms >= t) order by t)
44749
+ from unnest($1::float8[]) as t) as calls_of_groups_at_or_over
44750
+ from s join pg_roles as r on r.oid = s.userid
44751
+ group by r.rolname, s.userid
44752
+ order by 4 desc nulls last
44753
+ limit ${D001_MAX_ROLES}`;
44754
+ }
44755
+ var D001_COUNTERS_SQL = `
44756
+ select d.stats_reset, now() as read_at, d.temp_files, d.temp_bytes,
44757
+ (select coalesce(sum(t.autovacuum_count), 0) from pg_stat_all_tables as t) as autovacuum_runs
44758
+ from pg_stat_database as d
44759
+ where d.datname = current_database()`;
44760
+ function d001Iso(value) {
44761
+ if (value === null || value === undefined)
44762
+ return null;
44763
+ return value instanceof Date ? value.toISOString() : String(value);
44764
+ }
44765
+ var D001_DURATION_SETTINGS = ["log_min_duration_statement", "auto_explain.log_min_duration", "log_min_duration_sample"];
44766
+ var D001_MS_PER = { us: 0.001, ms: 1, s: 1000, min: 60000, h: 3600000, d: 86400000 };
44767
+ var D001_MAX_MS = 2147483647;
44768
+ var D001_MAX_STORED_THRESHOLDS = 20;
44769
+ function d001RoundMs(value) {
44770
+ const clean = Math.round(value * 1e6) / 1e6;
44771
+ const floor = Math.floor(clean);
44772
+ if (clean - floor !== 0.5)
44773
+ return Math.round(clean);
44774
+ return floor % 2 === 0 ? floor : floor + 1;
44775
+ }
44776
+ function d001StoredMs(value) {
44777
+ const match = /^[ \t\n\r\f\v]*([0-9]+(?:\.[0-9]+)?)[ \t\n\r\f\v]*([a-z]*)[ \t\n\r\f\v]*$/.exec(String(value));
44778
+ const factor = match ? D001_MS_PER[match[2] || "ms"] : undefined;
44779
+ if (!match || factor === undefined || match[1].length > 20)
44780
+ return null;
44781
+ const ms = d001RoundMs(parseFloat(match[1]) * factor);
44782
+ return ms <= D001_MAX_MS ? ms : null;
44783
+ }
44784
+ function d001Thresholds(settings, overrides) {
44785
+ const out = new Set(D001_BASE_THRESHOLDS_MS);
44786
+ for (const name of D001_DURATION_SETTINGS) {
44787
+ const info = settings[name];
44788
+ const value = info ? parseInt(info.setting, 10) : NaN;
44789
+ if (value > 0 && (info.unit || "ms") === "ms")
44790
+ out.add(value);
44791
+ }
44792
+ const stored = new Set;
44793
+ for (const item of overrides?.values ?? []) {
44794
+ if (!D001_DURATION_SETTINGS.includes(String(item.setting)))
44795
+ continue;
44796
+ const value = d001StoredMs(item.value);
44797
+ if (value)
44798
+ stored.add(value);
44799
+ }
44800
+ for (const value of [...stored].sort((a, b) => a - b).slice(0, D001_MAX_STORED_THRESHOLDS))
44801
+ out.add(value);
44802
+ return [...out].sort((a, b) => a - b);
44803
+ }
44804
+ function d001Config(config) {
44805
+ const out = {};
44806
+ for (const item of Array.isArray(config) ? config : []) {
44807
+ const text = String(item);
44808
+ const at = text.indexOf("=");
44809
+ const name = at < 0 ? text : text.slice(0, at);
44810
+ if (D001_OVERRIDE_PATTERN.test(name))
44811
+ out[name] = at < 0 ? "" : text.slice(at + 1);
44812
+ }
44813
+ return out;
44814
+ }
44815
+ function d001Window(start, end, total) {
44816
+ const to = end ? new Date(String(end)) : null;
44817
+ if (!to || isNaN(to.getTime()))
44818
+ return null;
44819
+ const from = start ? new Date(String(start)) : null;
44820
+ const bounded = from !== null && !isNaN(from.getTime()) && to > from;
44821
+ return {
44822
+ start: bounded ? from.toISOString().replace(/\.\d{3}Z$/, "Z") : null,
44823
+ end: to.toISOString().replace(/\.\d{3}Z$/, "Z"),
44824
+ days: bounded ? Math.round((to.getTime() - from.getTime()) / 86400000 * 100) / 100 : null,
44825
+ total: Number(total) || 0
44826
+ };
44827
+ }
44828
+ async function d001CatalogFacts(client) {
44829
+ const out = {};
44830
+ const fact = async (key, read) => {
44831
+ try {
44832
+ const value = await read();
44833
+ out[key] = value;
44834
+ return value;
44835
+ } catch (err) {
44836
+ out[`${key}_error`] = a005Error(err);
44837
+ return;
44838
+ }
44839
+ };
44840
+ await fact("read_as", async () => (await client.query("select current_user as role, current_database() as database")).rows[0]);
44841
+ let settings = null;
44842
+ const sources = {};
44843
+ await fact("settings", async () => {
44844
+ const rows = (await client.query(D001_SETTINGS_SQL, [D001_SETTING_PATTERN])).rows;
44845
+ settings = {};
44846
+ for (const row of rows) {
44847
+ const setting = row.setting === null ? "" : String(row.setting);
44848
+ settings[row.name] = {
44849
+ setting,
44850
+ unit: row.unit || "",
44851
+ category: row.category || "",
44852
+ context: row.context || "",
44853
+ vartype: row.vartype || "",
44854
+ pretty_value: row.unit ? `${setting} ${row.unit}` : setting
44855
+ };
44856
+ sources[row.name] = { source: row.source, is_default: setting === row.boot_val, pending_restart: Boolean(row.pending_restart) };
44857
+ }
44858
+ return true;
44859
+ });
44860
+ delete out.settings;
44861
+ if (settings)
44862
+ out.setting_sources = sources;
44863
+ const overrides = await fact("overrides", async () => {
44864
+ const rows = (await client.query(D001_OVERRIDES_SQL)).rows;
44865
+ const values = [];
44866
+ for (const row of rows) {
44867
+ for (const [name, value] of Object.entries(d001Config(row.config))) {
44868
+ const scope = row.role && row.database ? "role in database" : row.role ? "role" : row.database ? "database" : "all";
44869
+ values.push({ scope, role: row.role ?? null, database: row.database ?? null, setting: name, value });
44870
+ }
44871
+ }
44872
+ return { rows_read: rows.length, values };
44873
+ });
44874
+ const roles = await fact("login_roles", async () => (await client.query(D001_LOGIN_ROLES_SQL)).rows.map((row) => ({
44875
+ role: row.role,
44876
+ superuser: Boolean(row.superuser),
44877
+ member_of: Array.isArray(row.member_of) ? row.member_of : [],
44878
+ sessions: parseInt(row.sessions, 10) || 0,
44879
+ oldest_session_start: d001Iso(row.oldest_session_start),
44880
+ applications: Array.isArray(row.applications) ? row.applications : []
44881
+ })));
44882
+ if (roles) {
44883
+ let reason = null;
44884
+ try {
44885
+ const sees = (await client.query(D001_SEES_SESSIONS_SQL)).rows[0];
44886
+ if (sees && sees.visible === false)
44887
+ reason = "the reading role has no pg_read_all_stats: other roles' sessions are not visible";
44888
+ } catch (err) {
44889
+ reason = a005Error(err);
44890
+ }
44891
+ if (reason) {
44892
+ for (const role of roles)
44893
+ Object.assign(role, { sessions: null, oldest_session_start: null, applications: [] });
44894
+ out.sessions_error = reason;
44895
+ }
44896
+ }
44897
+ await fact("statements", async () => {
44898
+ const schema = (await client.query(G003_PGSS_SCHEMA_SQL)).rows[0]?.schema;
44899
+ if (!schema)
44900
+ throw new Error("pg_stat_statements is not installed");
44901
+ const quoted = `"${String(schema).replace(/"/g, '""')}"`;
44902
+ const thresholds = d001Thresholds(settings || {}, overrides);
44903
+ const byRole = (await client.query(d001StatementsSql(`${quoted}.pg_stat_statements`), [thresholds])).rows.map((row) => {
44904
+ const counts = (value) => Object.fromEntries(Object.entries(value || {}).map(([k, v]) => [String(parseFloat(k)), Number(v) || 0]));
44905
+ return {
44906
+ role: row.role,
44907
+ groups: parseInt(row.groups, 10) || 0,
44908
+ calls: parseInt(row.calls, 10) || 0,
44909
+ longest_call_ms: row.longest_ms === null ? null : Math.round(parseFloat(row.longest_ms) * 10) / 10,
44910
+ stats_since: d001Iso(row.stats_since),
44911
+ groups_at_or_over_ms: counts(row.groups_at_or_over),
44912
+ calls_of_groups_at_or_over_ms: counts(row.calls_of_groups_at_or_over)
44913
+ };
44914
+ });
44915
+ const result = { thresholds_ms: thresholds, by_role: byRole };
44916
+ try {
44917
+ const info = (await client.query(`select dealloc, stats_reset from ${quoted}.pg_stat_statements_info`)).rows[0];
44918
+ if (info) {
44919
+ result.dealloc = parseInt(info.dealloc, 10) || 0;
44920
+ result.stats_reset = d001Iso(info.stats_reset);
44921
+ }
44922
+ } catch {}
44923
+ return result;
44924
+ });
44925
+ try {
44926
+ const row = (await client.query(D001_COUNTERS_SQL)).rows[0];
44927
+ for (const [key, total] of [["temp_files", row?.temp_files], ["temp_bytes", row?.temp_bytes], ["autovacuum_runs", row?.autovacuum_runs]]) {
44928
+ const window = row ? d001Window(row.stats_reset, row.read_at, total) : null;
44929
+ if (window)
44930
+ out[key] = window;
44931
+ else
44932
+ out[`${key}_error`] = "the counters could not be read";
44933
+ }
44934
+ } catch (err) {
44935
+ for (const key of ["temp_files", "temp_bytes", "autovacuum_runs"])
44936
+ out[`${key}_error`] = a005Error(err);
44937
+ }
44938
+ return { settings, logging: out };
44939
+ }
44742
44940
  async function generateD001(client, nodeName) {
44743
44941
  const report = createBaseReport("D001", "Logging settings", nodeName);
44744
44942
  const postgresVersion = await getPostgresVersion(client);
44745
- const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
44746
- const allSettings = await getSettings(client, pgMajorVersion);
44747
- const loggingSettings = {};
44748
- for (const [name, setting] of Object.entries(allSettings)) {
44749
- if (name.startsWith("log_") || name.startsWith("logging_")) {
44750
- loggingSettings[name] = setting;
44943
+ const { settings, logging } = await d001CatalogFacts(client);
44944
+ let data = settings || {};
44945
+ if (!settings || !Object.keys(settings).length) {
44946
+ const pgMajorVersion = parseInt(postgresVersion.server_major_ver, 10) || 16;
44947
+ data = {};
44948
+ for (const [name, setting] of Object.entries(await getSettings(client, pgMajorVersion))) {
44949
+ if (new RegExp(D001_SETTING_PATTERN).test(name))
44950
+ data[name] = setting;
44751
44951
  }
44752
44952
  }
44753
44953
  report.results[nodeName] = {
44754
- data: loggingSettings,
44954
+ data,
44955
+ logging,
44755
44956
  postgres_version: postgresVersion
44756
44957
  };
44757
44958
  return report;
@@ -45772,6 +45973,194 @@ async function generateG001(client, nodeName) {
45772
45973
  };
45773
45974
  return report;
45774
45975
  }
45976
+ var G003_TIMEOUT_SETTINGS = [
45977
+ "statement_timeout",
45978
+ "transaction_timeout",
45979
+ "lock_timeout",
45980
+ "idle_in_transaction_session_timeout",
45981
+ "idle_session_timeout",
45982
+ "deadlock_timeout",
45983
+ "log_lock_waits"
45984
+ ];
45985
+ var G003_MAX_ROLES = 60;
45986
+ var G003_MAX_ROUTINES = 30;
45987
+ var G003_QUERY_CHARS = 120;
45988
+ var G003_ROLES_SQL = `
45989
+ select r.rolname as role, r.rolcanlogin as can_login, r.rolsuper as superuser, r.rolbypassrls as bypass_rls,
45990
+ coalesce((select array_agg(g.rolname::text order by g.rolname)
45991
+ from pg_auth_members as m join pg_roles as g on g.oid = m.roleid
45992
+ where m.member = r.oid), '{}') as member_of,
45993
+ (select s.setconfig from pg_db_role_setting as s
45994
+ where s.setrole = r.oid and s.setdatabase = 0) as config
45995
+ from pg_roles as r
45996
+ where r.rolname !~ '^pg_'
45997
+ order by r.rolname
45998
+ `;
45999
+ var G003_ROLE_DATABASE_SQL = `
46000
+ select r.rolname as role, d.datname as database, s.setconfig as config
46001
+ from pg_db_role_setting as s
46002
+ join pg_roles as r on r.oid = s.setrole
46003
+ join pg_database as d on d.oid = s.setdatabase
46004
+ order by r.rolname, d.datname
46005
+ `;
46006
+ var G003_DATABASE_SQL = `
46007
+ select d.datname as database, s.setconfig as config
46008
+ from pg_db_role_setting as s
46009
+ join pg_database as d on d.oid = s.setdatabase
46010
+ where s.setrole = 0
46011
+ order by d.datname
46012
+ `;
46013
+ var G003_SERVER_SQL = `
46014
+ select name, reset_val as setting, coalesce(unit, '') as unit, source
46015
+ from pg_settings
46016
+ where name = any($1)
46017
+ order by name
46018
+ `;
46019
+ var G003_PGSS_SCHEMA_SQL = `
46020
+ select n.nspname as schema
46021
+ from pg_extension as e join pg_namespace as n on n.oid = e.extnamespace
46022
+ where e.extname = 'pg_stat_statements'
46023
+ `;
46024
+ var g003CallsSql = (quotedSchema) => `
46025
+ with s as (
46026
+ select p.userid, p.queryid, p.calls, p.query,
46027
+ coalesce((to_jsonb(p) ->> 'max_exec_time'), (to_jsonb(p) ->> 'max_time'))::float8 as max_ms,
46028
+ coalesce(to_jsonb(p) ->> 'minmax_stats_since', to_jsonb(p) ->> 'stats_since') as since
46029
+ from ${quotedSchema}.pg_stat_statements as p
46030
+ )
46031
+ select r.rolname as role, sum(s.calls)::bigint as calls,
46032
+ (array_agg(s.max_ms order by s.max_ms desc nulls last))[1] as longest_ms,
46033
+ (array_agg(s.queryid::text order by s.max_ms desc nulls last))[1] as longest_queryid,
46034
+ -- Utility text stays verbatim in pg_stat_statements, passwords included: only plain queries.
46035
+ (array_agg(case when s.query ~* '^\\s*(insert|update|delete|merge|values)\\M'
46036
+ or (s.query ~* '^\\s*(select|with)\\M' and s.query !~* '\\minto\\M')
46037
+ then left(s.query, ${G003_QUERY_CHARS}) end order by s.max_ms desc nulls last))[1] as longest_query,
46038
+ (array_agg(s.since order by s.max_ms desc nulls last))[1] as longest_since,
46039
+ min(s.since) as stats_since
46040
+ from s join pg_roles as r on r.oid = s.userid
46041
+ group by r.rolname
46042
+ order by 3 desc nulls last
46043
+ limit ${G003_MAX_ROLES}
46044
+ `;
46045
+ var G003_ROUTINES_SQL = `
46046
+ select n.nspname as schema, p.proname as name, p.prokind as kind, pg_get_userbyid(p.proowner) as owner,
46047
+ b.src ~* '\\mcommit\\M' as commits,
46048
+ b.src ~* '\\m(query_canceled|57014)\\M' as catches_cancel,
46049
+ (select array_agg(c) from unnest(p.proconfig) as c
46050
+ where split_part(c, '=', 1) = any($1)) as config
46051
+ from pg_proc as p
46052
+ join pg_namespace as n on n.oid = p.pronamespace
46053
+ -- The code without its comments and \`on commit drop\`, which are not a commit.
46054
+ cross join lateral (select regexp_replace(regexp_replace(regexp_replace(p.prosrc, '/\\*.*?\\*/', '', 'g'),
46055
+ '--[^\\n]*', '', 'g'),
46056
+ 'on\\s+commit\\s+\\w+(\\s+\\w+)?', '', 'gi') as src) as b
46057
+ where n.nspname not in ('pg_catalog', 'information_schema') and n.nspname !~ '^pg_(toast|temp)'
46058
+ and (p.prokind = 'p' or exists (select 1 from unnest(p.proconfig) as c
46059
+ where split_part(c, '=', 1) = any($1)))
46060
+ and not exists (select 1 from pg_depend as d
46061
+ where d.classid = 'pg_proc'::regclass and d.objid = p.oid and d.deptype = 'e')
46062
+ order by n.nspname, p.proname
46063
+ limit ${G003_MAX_ROUTINES}
46064
+ `;
46065
+ function g003Settings(config) {
46066
+ const out = {};
46067
+ for (const item of Array.isArray(config) ? config : []) {
46068
+ const text = String(item);
46069
+ const at = text.indexOf("=");
46070
+ const name = at < 0 ? text : text.slice(0, at);
46071
+ if (G003_TIMEOUT_SETTINGS.includes(name))
46072
+ out[name] = at < 0 ? "" : text.slice(at + 1);
46073
+ }
46074
+ return out;
46075
+ }
46076
+ async function g003ReadRoles(client) {
46077
+ const perDatabase = Object.create(null);
46078
+ for (const row of (await client.query(G003_ROLE_DATABASE_SQL)).rows) {
46079
+ const values = g003Settings(row.config);
46080
+ if (Object.keys(values).length) {
46081
+ (perDatabase[row.role] ??= Object.create(null))[row.database] = values;
46082
+ }
46083
+ }
46084
+ return (await client.query(G003_ROLES_SQL)).rows.map((row) => ({
46085
+ role: row.role,
46086
+ can_login: Boolean(row.can_login),
46087
+ superuser: Boolean(row.superuser),
46088
+ bypass_rls: Boolean(row.bypass_rls),
46089
+ member_of: row.member_of || [],
46090
+ settings: g003Settings(row.config),
46091
+ database_settings: perDatabase[row.role] || {}
46092
+ }));
46093
+ }
46094
+ async function g003ReadCalls(client) {
46095
+ const schema = (await client.query(G003_PGSS_SCHEMA_SQL)).rows[0]?.schema;
46096
+ if (!schema)
46097
+ throw new Error("pg_stat_statements is not installed");
46098
+ const quoted = `"${String(schema).replace(/"/g, '""')}"`;
46099
+ const calls = new Map;
46100
+ for (const row of (await client.query(g003CallsSql(quoted))).rows) {
46101
+ calls.set(row.role, {
46102
+ calls: parseInt(row.calls, 10) || 0,
46103
+ longest_call_ms: row.longest_ms === null ? null : Math.round(parseFloat(row.longest_ms) * 10) / 10,
46104
+ longest_call_queryid: row.longest_queryid,
46105
+ longest_call_query: row.longest_query,
46106
+ longest_call_since: row.longest_since,
46107
+ stats_since: row.stats_since
46108
+ });
46109
+ }
46110
+ return calls;
46111
+ }
46112
+ async function g003CatalogFacts(client) {
46113
+ const out = {};
46114
+ const fact = async (key, read) => {
46115
+ try {
46116
+ const value = await read();
46117
+ out[key] = value;
46118
+ return value;
46119
+ } catch (err) {
46120
+ out[`${key}_error`] = a005Error(err);
46121
+ return;
46122
+ }
46123
+ };
46124
+ await fact("read_as", async () => (await client.query("select current_user as role, current_database() as database")).rows[0]);
46125
+ await fact("server_settings", async () => {
46126
+ const server = {};
46127
+ for (const row of (await client.query(G003_SERVER_SQL, [G003_TIMEOUT_SETTINGS])).rows) {
46128
+ server[row.name] = { setting: row.setting, unit: row.unit, source: row.source };
46129
+ }
46130
+ return server;
46131
+ });
46132
+ await fact("database_settings", async () => {
46133
+ const values = {};
46134
+ for (const row of (await client.query(G003_DATABASE_SQL)).rows) {
46135
+ const stored = g003Settings(row.config);
46136
+ if (Object.keys(stored).length)
46137
+ values[row.database] = stored;
46138
+ }
46139
+ return values;
46140
+ });
46141
+ const roles = await fact("roles", () => g003ReadRoles(client));
46142
+ const calls = await fact("calls", () => g003ReadCalls(client));
46143
+ delete out.calls;
46144
+ if (roles && calls) {
46145
+ for (const role of roles)
46146
+ Object.assign(role, calls.get(role.role) || {});
46147
+ }
46148
+ if (roles && roles.length > G003_MAX_ROLES) {
46149
+ const useful = (role) => Object.keys(role.settings).length || Object.keys(role.database_settings).length || role.calls;
46150
+ out.roles_total = roles.length;
46151
+ out.roles = [...roles.filter(useful), ...roles.filter((role) => !useful(role))].slice(0, G003_MAX_ROLES);
46152
+ }
46153
+ await fact("routines", async () => (await client.query(G003_ROUTINES_SQL, [G003_TIMEOUT_SETTINGS])).rows.map((row) => ({
46154
+ schema: row.schema,
46155
+ name: row.name,
46156
+ kind: row.kind === "p" ? "procedure" : "function",
46157
+ owner: row.owner,
46158
+ commits: Boolean(row.commits),
46159
+ catches_cancel: Boolean(row.catches_cancel),
46160
+ settings: g003Settings(row.config)
46161
+ })));
46162
+ return out;
46163
+ }
45775
46164
  async function generateG003(client, nodeName) {
45776
46165
  const report = createBaseReport("G003", "Timeouts, locks, deadlocks", nodeName);
45777
46166
  const postgresVersion = await getPostgresVersion(client);
@@ -45824,7 +46213,8 @@ async function generateG003(client, nodeName) {
45824
46213
  data: {
45825
46214
  settings: lockSettings,
45826
46215
  deadlock_stats: deadlockStats,
45827
- ...deadlockError && { deadlock_stats_error: deadlockError }
46216
+ ...deadlockError && { deadlock_stats_error: deadlockError },
46217
+ ...await g003CatalogFacts(client)
45828
46218
  },
45829
46219
  postgres_version: postgresVersion
45830
46220
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresai",
3
- "version": "0.18.1-rc.3",
3
+ "version": "0.18.1-rc.5",
4
4
  "description": "postgres_ai CLI",
5
5
  "license": "Apache-2.0",
6
6
  "private": false,
@@ -58,12 +58,283 @@
58
58
  "type": "object",
59
59
  "additionalProperties": { "$ref": "#/$defs/setting" }
60
60
  },
61
+ "counterWindow": {
62
+ "type": "object",
63
+ "additionalProperties": false,
64
+ "required": [
65
+ "start",
66
+ "end",
67
+ "days",
68
+ "total"
69
+ ],
70
+ "properties": {
71
+ "start": {
72
+ "type": [
73
+ "string",
74
+ "null"
75
+ ]
76
+ },
77
+ "end": {
78
+ "type": "string"
79
+ },
80
+ "days": {
81
+ "type": [
82
+ "number",
83
+ "null"
84
+ ]
85
+ },
86
+ "total": {
87
+ "type": "number"
88
+ },
89
+ "floor": {
90
+ "type": "boolean"
91
+ }
92
+ }
93
+ },
94
+ "logging": {
95
+ "type": "object",
96
+ "additionalProperties": false,
97
+ "properties": {
98
+ "read_as": {
99
+ "type": "object",
100
+ "additionalProperties": false,
101
+ "required": [
102
+ "role",
103
+ "database"
104
+ ],
105
+ "properties": {
106
+ "role": {
107
+ "type": "string"
108
+ },
109
+ "database": {
110
+ "type": "string"
111
+ }
112
+ }
113
+ },
114
+ "setting_sources": {
115
+ "type": "object",
116
+ "additionalProperties": {
117
+ "type": "object",
118
+ "additionalProperties": false,
119
+ "required": [
120
+ "source",
121
+ "is_default",
122
+ "pending_restart"
123
+ ],
124
+ "properties": {
125
+ "source": {
126
+ "type": "string"
127
+ },
128
+ "is_default": {
129
+ "type": "boolean"
130
+ },
131
+ "pending_restart": {
132
+ "type": "boolean"
133
+ }
134
+ }
135
+ }
136
+ },
137
+ "overrides": {
138
+ "type": "object",
139
+ "additionalProperties": false,
140
+ "required": [
141
+ "rows_read",
142
+ "values"
143
+ ],
144
+ "properties": {
145
+ "rows_read": {
146
+ "type": "integer"
147
+ },
148
+ "values": {
149
+ "type": "array",
150
+ "items": {
151
+ "type": "object",
152
+ "additionalProperties": false,
153
+ "required": [
154
+ "scope",
155
+ "role",
156
+ "database",
157
+ "setting",
158
+ "value"
159
+ ],
160
+ "properties": {
161
+ "scope": {
162
+ "enum": [
163
+ "role",
164
+ "database",
165
+ "role in database",
166
+ "all"
167
+ ]
168
+ },
169
+ "role": {
170
+ "type": [
171
+ "string",
172
+ "null"
173
+ ]
174
+ },
175
+ "database": {
176
+ "type": [
177
+ "string",
178
+ "null"
179
+ ]
180
+ },
181
+ "setting": {
182
+ "type": "string"
183
+ },
184
+ "value": {
185
+ "type": "string"
186
+ }
187
+ }
188
+ }
189
+ }
190
+ }
191
+ },
192
+ "login_roles": {
193
+ "type": "array",
194
+ "items": {
195
+ "type": "object",
196
+ "additionalProperties": false,
197
+ "required": [
198
+ "role",
199
+ "superuser",
200
+ "member_of",
201
+ "sessions",
202
+ "oldest_session_start",
203
+ "applications"
204
+ ],
205
+ "properties": {
206
+ "role": {
207
+ "type": "string"
208
+ },
209
+ "superuser": {
210
+ "type": "boolean"
211
+ },
212
+ "sessions": {
213
+ "type": [
214
+ "integer",
215
+ "null"
216
+ ]
217
+ },
218
+ "oldest_session_start": {
219
+ "type": [
220
+ "string",
221
+ "null"
222
+ ]
223
+ },
224
+ "applications": {
225
+ "type": "array",
226
+ "items": {
227
+ "type": "string"
228
+ }
229
+ },
230
+ "member_of": {
231
+ "type": "array",
232
+ "items": {
233
+ "type": "string"
234
+ }
235
+ }
236
+ }
237
+ }
238
+ },
239
+ "statements": {
240
+ "type": "object",
241
+ "additionalProperties": false,
242
+ "required": [
243
+ "thresholds_ms",
244
+ "by_role"
245
+ ],
246
+ "properties": {
247
+ "thresholds_ms": {
248
+ "type": "array",
249
+ "items": {
250
+ "type": "number"
251
+ }
252
+ },
253
+ "dealloc": {
254
+ "type": "integer"
255
+ },
256
+ "stats_reset": {
257
+ "type": [
258
+ "string",
259
+ "null"
260
+ ]
261
+ },
262
+ "by_role": {
263
+ "type": "array",
264
+ "items": {
265
+ "type": "object",
266
+ "additionalProperties": false,
267
+ "required": [
268
+ "role",
269
+ "groups",
270
+ "calls",
271
+ "longest_call_ms",
272
+ "stats_since",
273
+ "groups_at_or_over_ms",
274
+ "calls_of_groups_at_or_over_ms"
275
+ ],
276
+ "properties": {
277
+ "role": {
278
+ "type": "string"
279
+ },
280
+ "groups": {
281
+ "type": "integer"
282
+ },
283
+ "calls": {
284
+ "type": "integer"
285
+ },
286
+ "longest_call_ms": {
287
+ "type": [
288
+ "number",
289
+ "null"
290
+ ]
291
+ },
292
+ "stats_since": {
293
+ "type": [
294
+ "string",
295
+ "null"
296
+ ]
297
+ },
298
+ "groups_at_or_over_ms": {
299
+ "type": "object",
300
+ "additionalProperties": {
301
+ "type": "integer"
302
+ }
303
+ },
304
+ "calls_of_groups_at_or_over_ms": {
305
+ "type": "object",
306
+ "additionalProperties": {
307
+ "type": "integer"
308
+ }
309
+ }
310
+ }
311
+ }
312
+ }
313
+ }
314
+ },
315
+ "temp_files": {
316
+ "$ref": "#/$defs/counterWindow"
317
+ },
318
+ "temp_bytes": {
319
+ "$ref": "#/$defs/counterWindow"
320
+ },
321
+ "autovacuum_runs": {
322
+ "$ref": "#/$defs/counterWindow"
323
+ }
324
+ },
325
+ "patternProperties": {
326
+ "_error$": {
327
+ "type": "string"
328
+ }
329
+ }
330
+ },
61
331
  "nodeResult": {
62
332
  "type": "object",
63
333
  "additionalProperties": false,
64
334
  "required": ["data"],
65
335
  "properties": {
66
336
  "data": { "$ref": "#/$defs/data" },
337
+ "logging": { "$ref": "#/$defs/logging" },
67
338
  "postgres_version": { "$ref": "#/$defs/postgresVersion" }
68
339
  }
69
340
  }
@@ -78,7 +78,60 @@
78
78
  "real_size_pretty": { "type": "string" },
79
79
  "table_size_pretty": { "type": "string" },
80
80
  "extra_size_pretty": { "type": "string" },
81
- "bloat_size_pretty": { "type": "string" }
81
+ "bloat_size_pretty": { "type": "string" },
82
+ "bloat_size_min_in_window_bytes": { "type": ["integer", "null"] },
83
+ "bloat_size_max_in_window_bytes": { "type": ["integer", "null"] },
84
+ "real_size_min_in_window_bytes": { "type": ["integer", "null"] },
85
+ "real_size_max_in_window_bytes": { "type": ["integer", "null"] },
86
+ "on_unused_list": { "type": ["boolean", "null"] },
87
+ "on_redundant_list": { "type": ["boolean", "null"] }
88
+ }
89
+ },
90
+ "tableFact": {
91
+ "type": "object",
92
+ "additionalProperties": false,
93
+ "required": ["schema_name", "table_name"],
94
+ "properties": {
95
+ "schema_name": { "type": "string" },
96
+ "table_name": { "type": "string" },
97
+ "n_live_tup": { "type": ["integer", "null"] },
98
+ "n_dead_tup": { "type": ["integer", "null"] },
99
+ "vacuum_threshold": { "type": ["integer", "null"] },
100
+ "autovacuum_runs_in_window": { "type": ["integer", "null"] },
101
+ "vacuum_runs_in_window": { "type": ["integer", "null"] },
102
+ "n_tup_ins_in_window": { "type": ["integer", "null"] },
103
+ "n_tup_upd_in_window": { "type": ["integer", "null"] },
104
+ "n_tup_del_in_window": { "type": ["integer", "null"] },
105
+ "n_tup_hot_upd_in_window": { "type": ["integer", "null"] },
106
+ "table_size_bytes": { "type": ["integer", "null"] },
107
+ "toast_size_bytes": { "type": ["integer", "null"] },
108
+ "table_size_min_in_window_bytes": { "type": ["integer", "null"] },
109
+ "table_size_max_in_window_bytes": { "type": ["integer", "null"] },
110
+ "seconds_since_last_vacuum": { "type": ["integer", "null"] },
111
+ "vacuum_recorded": { "type": ["boolean", "null"] },
112
+ "autovacuum_disabled": { "type": ["boolean", "null"] }
113
+ }
114
+ },
115
+ "history": {
116
+ "type": "object",
117
+ "additionalProperties": false,
118
+ "required": ["daily", "first_reading_at"],
119
+ "properties": {
120
+ "daily": {
121
+ "type": "array",
122
+ "items": {
123
+ "type": "object",
124
+ "additionalProperties": false,
125
+ "required": ["days_ago", "at", "total_bloat_size_bytes"],
126
+ "properties": {
127
+ "days_ago": { "type": "integer" },
128
+ "at": { "type": ["string", "null"] },
129
+ "total_bloat_size_bytes": { "type": "integer" }
130
+ }
131
+ }
132
+ },
133
+ "first_reading_at": { "type": ["string", "null"] },
134
+ "window_seconds": { "type": "integer" }
82
135
  }
83
136
  },
84
137
  "dbEntry": {
@@ -99,7 +152,22 @@
99
152
  "total_bloat_size_bytes": { "type": "number" },
100
153
  "total_bloat_size_pretty": { "type": "string" },
101
154
  "database_size_bytes": { "type": "number" },
102
- "database_size_pretty": { "type": "string" }
155
+ "database_size_pretty": { "type": "string" },
156
+ "history": { "$ref": "#/$defs/history" },
157
+ "table_facts": { "type": "array", "items": { "$ref": "#/$defs/tableFact" } },
158
+ "traffic_by_hour_utc": {
159
+ "type": "array",
160
+ "items": {
161
+ "type": "object",
162
+ "additionalProperties": false,
163
+ "required": ["hour", "xact_commit_per_s", "readings"],
164
+ "properties": {
165
+ "hour": { "type": "integer" },
166
+ "xact_commit_per_s": { "type": "number" },
167
+ "readings": { "type": "integer" }
168
+ }
169
+ }
170
+ }
103
171
  }
104
172
  },
105
173
  "status": {
@@ -64,6 +64,104 @@
64
64
  "stats_reset": { "type": ["string", "null"] }
65
65
  }
66
66
  },
67
+ "timeoutValues": {
68
+ "type": "object",
69
+ "additionalProperties": { "type": "string" }
70
+ },
71
+ "role": {
72
+ "type": "object",
73
+ "additionalProperties": false,
74
+ "required": ["role", "can_login", "superuser", "bypass_rls", "member_of", "settings", "database_settings"],
75
+ "properties": {
76
+ "role": { "type": "string" },
77
+ "can_login": { "type": "boolean" },
78
+ "superuser": { "type": "boolean" },
79
+ "bypass_rls": { "type": "boolean" },
80
+ "member_of": { "type": "array", "items": { "type": "string" } },
81
+ "settings": { "$ref": "#/$defs/timeoutValues" },
82
+ "database_settings": { "type": "object", "additionalProperties": { "$ref": "#/$defs/timeoutValues" } },
83
+ "calls": { "type": "integer" },
84
+ "longest_call_ms": { "type": ["number", "null"] },
85
+ "longest_call_queryid": { "type": ["string", "null"] },
86
+ "longest_call_query": { "type": ["string", "null"] },
87
+ "longest_call_since": { "type": ["string", "null"] },
88
+ "stats_since": { "type": ["string", "null"] }
89
+ }
90
+ },
91
+ "routine": {
92
+ "type": "object",
93
+ "additionalProperties": false,
94
+ "required": ["schema", "name", "kind", "owner", "commits", "catches_cancel", "settings"],
95
+ "properties": {
96
+ "schema": { "type": "string" },
97
+ "name": { "type": "string" },
98
+ "kind": { "enum": ["procedure", "function"] },
99
+ "owner": { "type": "string" },
100
+ "commits": { "type": "boolean" },
101
+ "catches_cancel": { "type": "boolean" },
102
+ "settings": { "$ref": "#/$defs/timeoutValues" }
103
+ }
104
+ },
105
+ "deadlockWindow": {
106
+ "type": "object",
107
+ "additionalProperties": false,
108
+ "required": ["start", "end", "days", "step_seconds", "deadlocks", "days_with_deadlocks", "per_day", "by_database"],
109
+ "properties": {
110
+ "start": { "type": "string" },
111
+ "end": { "type": "string" },
112
+ "days": { "type": "number" },
113
+ "step_seconds": { "type": "integer" },
114
+ "deadlocks": { "type": "integer" },
115
+ "days_with_deadlocks": { "type": "integer" },
116
+ "per_day": {
117
+ "type": "array",
118
+ "items": {
119
+ "type": "object",
120
+ "additionalProperties": false,
121
+ "required": ["date", "deadlocks"],
122
+ "properties": { "date": { "type": "string" }, "deadlocks": { "type": "integer" } }
123
+ }
124
+ },
125
+ "by_database": { "type": "object", "additionalProperties": { "type": "integer" } },
126
+ "peak_hour_utc": { "type": "integer" },
127
+ "peak_hour_deadlocks": { "type": "integer" },
128
+ "worst_step_end": { "type": "string" },
129
+ "worst_step_deadlocks": { "type": "integer" }
130
+ }
131
+ },
132
+ "lockWaits": {
133
+ "type": "object",
134
+ "additionalProperties": false,
135
+ "required": ["start", "end", "days", "pairs_total", "session_pairs_total", "pairs"],
136
+ "properties": {
137
+ "start": { "type": ["string", "null"] },
138
+ "end": { "type": ["string", "null"] },
139
+ "days": { "type": ["number", "null"] },
140
+ "pairs_total": { "type": "integer" },
141
+ "session_pairs_total": { "type": "integer" },
142
+ "pairs": {
143
+ "type": "array",
144
+ "items": {
145
+ "type": "object",
146
+ "additionalProperties": false,
147
+ "required": ["blocked_query_id", "blocked_user", "blocked_table", "blocker_query_id", "blocker_user", "blocker_table", "sessions", "longest_blocked_statement_age_ms", "longest_at"],
148
+ "properties": {
149
+ "blocked_query_id": { "type": "string" },
150
+ "blocked_user": { "type": "string" },
151
+ "blocked_table": { "type": "string" },
152
+ "blocker_query_id": { "type": "string" },
153
+ "blocker_user": { "type": "string" },
154
+ "blocker_table": { "type": "string" },
155
+ "sessions": { "type": "integer" },
156
+ "longest_blocked_statement_age_ms": { "type": "integer" },
157
+ "longest_at": { "type": ["string", "null"] },
158
+ "blocked_query": { "type": ["string", "null"] },
159
+ "blocker_query": { "type": ["string", "null"] }
160
+ }
161
+ }
162
+ }
163
+ }
164
+ },
67
165
  "data": {
68
166
  "type": "object",
69
167
  "additionalProperties": false,
@@ -74,7 +172,36 @@
74
172
  "additionalProperties": { "$ref": "#/$defs/setting" }
75
173
  },
76
174
  "deadlock_stats": { "$ref": "#/$defs/deadlockStats" },
77
- "deadlock_stats_error": { "type": "string" }
175
+ "deadlock_stats_error": { "type": "string" },
176
+ "deadlock_window": { "$ref": "#/$defs/deadlockWindow" },
177
+ "deadlock_window_error": { "type": "string" },
178
+ "lock_waits": { "$ref": "#/$defs/lockWaits" },
179
+ "lock_waits_error": { "type": "string" },
180
+ "read_as": {
181
+ "type": "object",
182
+ "additionalProperties": false,
183
+ "required": ["role", "database"],
184
+ "properties": { "role": { "type": "string" }, "database": { "type": "string" } }
185
+ },
186
+ "read_as_error": { "type": "string" },
187
+ "server_settings": {
188
+ "type": "object",
189
+ "additionalProperties": {
190
+ "type": "object",
191
+ "additionalProperties": false,
192
+ "required": ["setting", "unit", "source"],
193
+ "properties": { "setting": { "type": "string" }, "unit": { "type": "string" }, "source": { "type": "string" } }
194
+ }
195
+ },
196
+ "server_settings_error": { "type": "string" },
197
+ "database_settings": { "type": "object", "additionalProperties": { "$ref": "#/$defs/timeoutValues" } },
198
+ "database_settings_error": { "type": "string" },
199
+ "roles": { "type": "array", "items": { "$ref": "#/$defs/role" } },
200
+ "roles_total": { "type": "integer" },
201
+ "roles_error": { "type": "string" },
202
+ "calls_error": { "type": "string" },
203
+ "routines": { "type": "array", "items": { "$ref": "#/$defs/routine" } },
204
+ "routines_error": { "type": "string" }
78
205
  }
79
206
  },
80
207
  "nodeResult": {
@@ -101,7 +101,21 @@
101
101
  "valid_duplicate_definition": {
102
102
  "type": ["string", "null"],
103
103
  "description": "Full CREATE INDEX statement of the valid duplicate index from pg_get_indexdef()"
104
- }
104
+ },
105
+ "is_ready": {
106
+ "type": "boolean",
107
+ "description": "pg_index.indisready: false = writes do not maintain the index; absent when the box does not collect it"
108
+ },
109
+ "build_in_progress": {
110
+ "type": "boolean",
111
+ "description": "A CREATE INDEX for it is running (pg_stat_progress_create_index): not a leftover yet"
112
+ },
113
+ "first_seen_epoch": { "type": "number", "description": "First monitored reading in the lookback that listed it" },
114
+ "first_seen_time": { "type": "string" },
115
+ "last_seen_epoch": { "type": "number" },
116
+ "last_seen_time": { "type": "string" },
117
+ "listed_hours": { "type": "integer", "description": "Hourly steps from first_seen that listed it" },
118
+ "reading_hours": { "type": "integer", "description": "Hourly steps from first_seen with a database-size reading" }
105
119
  }
106
120
  },
107
121
  "dbEntry": {
@@ -121,7 +135,32 @@
121
135
  "total_size_bytes": { "type": "number" },
122
136
  "total_size_pretty": { "type": "string" },
123
137
  "database_size_bytes": { "type": "number" },
124
- "database_size_pretty": { "type": "string" }
138
+ "database_size_pretty": { "type": "string" },
139
+ "not_valid_constraints": {
140
+ "type": "object",
141
+ "additionalProperties": false,
142
+ "required": ["total_count", "constraints"],
143
+ "description": "Constraints added NOT VALID and never validated; absent when the box does not collect them",
144
+ "properties": {
145
+ "total_count": { "type": "integer" },
146
+ "constraints": { "type": "array", "items": { "$ref": "#/$defs/notValidConstraint" } }
147
+ }
148
+ },
149
+ "monitoring_start_epoch": { "type": "number", "description": "First database-size reading in the lookback" },
150
+ "monitoring_start_time": { "type": "string" }
151
+ }
152
+ },
153
+ "notValidConstraint": {
154
+ "type": "object",
155
+ "additionalProperties": false,
156
+ "required": ["schema_name", "table_name", "constraint_name", "constraint_type", "constraint_definition", "is_partition_copy"],
157
+ "properties": {
158
+ "schema_name": { "type": "string" },
159
+ "table_name": { "type": "string" },
160
+ "constraint_name": { "type": "string" },
161
+ "constraint_type": { "type": "string", "description": "c = CHECK, f = FOREIGN KEY" },
162
+ "constraint_definition": { "type": "string" },
163
+ "is_partition_copy": { "type": "boolean", "description": "A partition's copy of a parent's constraint" }
125
164
  }
126
165
  },
127
166
  "data": {