Skip to content

Commit 6196ff5

Browse files
committed
docs: add C4 architecture model
Add a C4 software architecture model for the repo: - docs/architecture/workspace.dsl: Structurizr DSL source of truth (system context, containers, CLI & Reporter components) - docs/architecture/README.md: GitHub-renderable Mermaid C4 diagrams (L1 context, L2 containers, L3 components) - Link the model from the top-level README Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GrG9pynE6hPRG8JYsdKGxZ
1 parent 7999304 commit 6196ff5

3 files changed

Lines changed: 377 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ npx postgresai checkup --json postgresql://... | claude -p "find issues and sugg
217217
|---|---|
218218
| **Demo** | [demo.postgres.ai](https://demo.postgres.ai) (login: `demo` / `demo`) |
219219
| **Docs** | [postgres.ai/docs](https://postgres.ai/docs) |
220+
| **Architecture** | [C4 model](docs/architecture/README.md) |
220221
| **Issues** | [GitLab](https://gitlab.com/postgres-ai/postgresai/-/issues) |
221222
| **Community** | [Postgres.FM](https://postgres.fm) · [Postgres.TV](https://postgres.tv) |
222223

docs/architecture/README.md

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# PostgresAI — C4 Architecture Model
2+
3+
This directory holds the [C4 model](https://c4model.com/) for PostgresAI, an
4+
AI-native PostgreSQL observability platform (monitoring, health checks, and root
5+
cause analysis).
6+
7+
The model is maintained in two complementary forms:
8+
9+
| File | Purpose |
10+
|------|---------|
11+
| [`workspace.dsl`](./workspace.dsl) | **Source of truth.** [Structurizr DSL](https://docs.structurizr.com/dsl) describing people, systems, containers, components and their relationships. Renders all C4 levels and stays in sync as a single definition. |
12+
| `README.md` (this file) | GitHub-renderable [Mermaid](https://mermaid.js.org/syntax/c4.html) diagrams for quick reading without tooling. |
13+
14+
The C4 model describes architecture at four levels of zoom: **System Context**
15+
(L1) → **Containers** (L2) → **Components** (L3) → Code (L4, left to the source).
16+
17+
## How to view the Structurizr model
18+
19+
```bash
20+
# Render/edit interactively with Structurizr Lite
21+
docker run -it --rm -p 8080:8080 \
22+
-v "$(pwd)/docs/architecture:/usr/local/structurizr" \
23+
structurizr/lite
24+
# then open http://localhost:8080
25+
```
26+
27+
---
28+
29+
## Level 1 — System Context
30+
31+
How PostgresAI fits among its users and the external systems it talks to.
32+
33+
```mermaid
34+
C4Context
35+
title System Context — PostgresAI
36+
37+
Person(dba, "Engineer / DBA", "Runs health checks, reviews dashboards, resolves issues")
38+
Person(ai, "AI Coding Agent", "Claude Code, Cursor, Windsurf — consumes reports & MCP tools")
39+
40+
System(pgai, "PostgresAI", "AI-native PostgreSQL observability platform")
41+
42+
System_Ext(target, "Target PostgreSQL", "Databases being observed (self-hosted, RDS/Aurora, CloudSQL, Supabase)")
43+
System_Ext(console, "console.postgres.ai", "Managed cloud: UI, Issues API, report/file storage, auth")
44+
System_Ext(supabase, "Supabase Management API", "Executes SQL on Supabase databases")
45+
System_Ext(aws, "AWS / Amazon Managed Prometheus", "RDS/Aurora + managed metrics")
46+
System_Ext(llm, "LLM Provider", "Anthropic Claude / OpenAI GPT")
47+
48+
Rel(dba, pgai, "Runs checkups, views dashboards, manages issues")
49+
Rel(ai, pgai, "Reads reports, calls MCP tools")
50+
Rel(pgai, target, "Connects to & queries", "SQL")
51+
Rel(pgai, console, "Uploads reports, syncs issues, authenticates", "HTTPS")
52+
Rel(pgai, supabase, "Executes SQL", "HTTPS")
53+
Rel(pgai, aws, "Reads metrics", "HTTPS")
54+
Rel(ai, llm, "Analyzes JSON reports")
55+
56+
UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="2")
57+
```
58+
59+
---
60+
61+
## Level 2 — Containers
62+
63+
The separately deployable/runnable units inside PostgresAI. The CLI delivers the
64+
zero-setup express checkup; the rest form the optional full monitoring stack
65+
(`docker-compose.yml` / Helm chart).
66+
67+
```mermaid
68+
C4Container
69+
title Containers — PostgresAI
70+
71+
Person(dba, "Engineer / DBA")
72+
Person(ai, "AI Coding Agent")
73+
74+
System_Ext(target, "Target PostgreSQL")
75+
System_Ext(console, "console.postgres.ai")
76+
System_Ext(supabase, "Supabase Management API")
77+
System_Ext(aws, "Amazon Managed Prometheus")
78+
79+
System_Boundary(pgai, "PostgresAI") {
80+
Container(cli, "CLI", "Node.js / TypeScript", "Express checkup, stack install, prepare-db, auth (postgresai / pgai)")
81+
Container(mcp, "MCP Server", "Node.js / TypeScript", "Exposes Issues tools to AI agents")
82+
Container(reporter, "Reporter", "Python", "Generates 45+ structured health-check reports")
83+
Container(flask, "Monitoring Backend", "Python / Flask", "Query history + PromQL proxy API")
84+
Container(pgwPg, "pgwatch → Postgres", "Go", "Collects metrics into the Postgres sink")
85+
Container(pgwProm, "pgwatch → Prometheus", "Go", "Collects metrics into VictoriaMetrics")
86+
ContainerDb(vm, "VictoriaMetrics", "TSDB", "Prometheus-compatible metrics store")
87+
ContainerDb(sink, "Postgres Sink", "PostgreSQL 17", "Historical metrics & check data")
88+
Container(grafana, "Grafana", "Grafana 12", "13+ observability dashboards")
89+
Container(telemetry, "Telemetry Agent", "Node.js", "Hourly system telemetry")
90+
Container(pilot, "pg_index_pilot", "SQL / PL-pgSQL", "Autonomous index lifecycle")
91+
}
92+
93+
Rel(dba, cli, "Runs commands")
94+
Rel(dba, grafana, "Views dashboards", "HTTPS")
95+
Rel(ai, mcp, "Calls tools", "MCP/stdio")
96+
97+
Rel(cli, target, "Health-check & prepare-db SQL", "SQL")
98+
Rel(cli, console, "Uploads reports, syncs issues, auth", "HTTPS")
99+
Rel(cli, supabase, "Runs queries", "HTTPS")
100+
Rel(cli, reporter, "Triggers report generation")
101+
Rel(mcp, console, "Reads/writes issues", "HTTPS")
102+
103+
Rel(pgwPg, target, "Scrapes", "SQL")
104+
Rel(pgwProm, target, "Scrapes", "SQL")
105+
Rel(pgwPg, sink, "Writes metrics", "SQL")
106+
Rel(pgwProm, vm, "Writes metrics", "HTTP")
107+
108+
Rel(reporter, vm, "Reads metrics", "PromQL")
109+
Rel(reporter, sink, "Reads history", "SQL")
110+
Rel(reporter, aws, "Reads metrics", "HTTPS")
111+
Rel(reporter, console, "Uploads reports", "HTTPS")
112+
113+
Rel(flask, vm, "Proxies PromQL", "HTTP")
114+
Rel(flask, sink, "Reads query history", "SQL")
115+
Rel(grafana, vm, "Queries metrics", "PromQL")
116+
Rel(grafana, sink, "Queries history", "SQL")
117+
Rel(grafana, flask, "Calls backend API", "HTTP")
118+
119+
Rel(telemetry, console, "Posts telemetry", "HTTPS")
120+
Rel(pilot, target, "Manages indexes", "SQL")
121+
122+
UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="1")
123+
```
124+
125+
---
126+
127+
## Level 3 — Components: CLI
128+
129+
The CLI is the primary entry point and the most component-rich container
130+
(`cli/lib/*.ts`).
131+
132+
```mermaid
133+
C4Component
134+
title Components — CLI
135+
136+
Person(dba, "Engineer / DBA")
137+
System_Ext(target, "Target PostgreSQL")
138+
System_Ext(console, "console.postgres.ai")
139+
System_Ext(supabase, "Supabase Management API")
140+
141+
Container_Boundary(cli, "CLI") {
142+
Component(cmd, "Command Dispatch", "postgres-ai.ts", "Parses commands/flags, orchestrates workflows")
143+
Component(checkup, "Checkup Engine", "checkup.ts", "Runs 45+ health checks, builds JSON reports")
144+
Component(checkupApi, "Checkup API Client", "checkup-api.ts", "Uploads/fetches reports")
145+
Component(init, "DB Init / prepare-db", "init.ts", "Creates monitoring role, schema, permissions")
146+
Component(instances, "Targets / Instances", "instances.ts", "Manages monitored targets")
147+
Component(issues, "Issues Client", "issues.ts", "Console Issues API CRUD")
148+
Component(auth, "Auth (OAuth2/PKCE)", "auth-server.ts, pkce.ts", "Login & API-key handling")
149+
Component(storage, "Storage Client", "storage.ts", "File upload/download")
150+
Component(sb, "Supabase Client", "supabase.ts", "Runs SQL via Supabase API")
151+
Component(cfg, "Local Config", "config.ts, util.ts", "~/.postgres-ai settings")
152+
}
153+
154+
Rel(dba, cmd, "Runs commands")
155+
Rel(cmd, checkup, "Invokes")
156+
Rel(cmd, init, "Invokes")
157+
Rel(cmd, instances, "Invokes")
158+
Rel(cmd, issues, "Invokes")
159+
Rel(cmd, auth, "Invokes")
160+
161+
Rel(checkup, target, "Runs SQL", "SQL")
162+
Rel(checkup, checkupApi, "Uploads report")
163+
Rel(checkup, cfg, "Reads settings")
164+
Rel(checkupApi, console, "HTTPS")
165+
Rel(init, target, "Creates role/schema", "SQL")
166+
Rel(issues, console, "HTTPS")
167+
Rel(auth, console, "OAuth2/PKCE", "HTTPS")
168+
Rel(storage, console, "Uploads files", "HTTPS")
169+
Rel(sb, supabase, "HTTPS")
170+
171+
UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="1")
172+
```
173+
174+
---
175+
176+
## Level 3 — Components: Reporter
177+
178+
```mermaid
179+
C4Component
180+
title Components — Reporter
181+
182+
System_Ext(aws, "Amazon Managed Prometheus")
183+
System_Ext(console, "console.postgres.ai")
184+
ContainerDb(vm, "VictoriaMetrics")
185+
ContainerDb(sink, "Postgres Sink")
186+
187+
Container_Boundary(reporter, "Reporter") {
188+
Component(gen, "Report Generators", "postgres_reports.py", "Per-check report logic (A/D/F/H/I/K/M/N series)")
189+
Component(schemas, "Report Schemas", "schemas/*.schema.json", "29 JSON Schemas validating output")
190+
}
191+
192+
Rel(gen, vm, "Reads metrics", "PromQL")
193+
Rel(gen, sink, "Reads history", "SQL")
194+
Rel(gen, aws, "Reads metrics", "HTTPS")
195+
Rel(gen, schemas, "Validates against")
196+
Rel(gen, console, "Uploads reports", "HTTPS")
197+
198+
UpdateLayoutConfig($c4ShapeInRow="2", $c4BoundaryInRow="1")
199+
```
200+
201+
---
202+
203+
## Maintaining this model
204+
205+
- Treat `workspace.dsl` as the source of truth; update it when containers,
206+
components, or integrations change.
207+
- Keep the Mermaid diagrams above in sync for at-a-glance reading on GitHub.
208+
- Useful references: [c4model.com](https://c4model.com/),
209+
[Structurizr DSL docs](https://docs.structurizr.com/dsl),
210+
[Mermaid C4 syntax](https://mermaid.js.org/syntax/c4.html).

docs/architecture/workspace.dsl

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
workspace "PostgresAI" "AI-native PostgreSQL observability: monitoring, health checks, and root cause analysis." {
2+
3+
!identifiers hierarchical
4+
5+
model {
6+
# ---------------------------------------------------------------
7+
# People / external actors
8+
# ---------------------------------------------------------------
9+
dba = person "Engineer / DBA" "Runs health checks, reviews dashboards, and resolves issues."
10+
aiAgent = person "AI Coding Agent" "Claude Code, Cursor, Windsurf, etc. Consumes structured reports and the MCP server." "AI"
11+
12+
# ---------------------------------------------------------------
13+
# External software systems
14+
# ---------------------------------------------------------------
15+
targetPg = softwareSystem "Target PostgreSQL" "The PostgreSQL database(s) being observed (self-hosted, RDS/Aurora, CloudSQL, Supabase, etc.)." "External, Database"
16+
console = softwareSystem "console.postgres.ai" "PostgresAI managed cloud: web UI, Issues API, report & file storage, auth (OAuth2/PKCE)." "External"
17+
supabaseApi = softwareSystem "Supabase Management API" "Executes SQL against Supabase-hosted databases." "External"
18+
awsApm = softwareSystem "AWS / Amazon Managed Prometheus" "RDS, Aurora and Amazon Managed Prometheus metrics sources." "External"
19+
llm = softwareSystem "LLM Provider" "Anthropic Claude / OpenAI GPT used to analyze JSON reports." "External, AI"
20+
21+
# ---------------------------------------------------------------
22+
# The PostgresAI system and its containers
23+
# ---------------------------------------------------------------
24+
pgai = softwareSystem "PostgresAI" "AI-native PostgreSQL observability platform." {
25+
26+
cli = container "CLI" "Express checkup, stack install, target & DB prep, auth. Binaries: postgresai / pgai." "Node.js / TypeScript" "EntryPoint" {
27+
cmd = component "Command Dispatch" "Parses commands and flags, orchestrates workflows." "postgres-ai.ts"
28+
checkup = component "Checkup Engine" "Runs 45+ health checks and builds structured JSON reports." "checkup.ts"
29+
checkupApi = component "Checkup API Client" "Uploads/fetches reports to/from console.postgres.ai." "checkup-api.ts"
30+
init = component "DB Init / prepare-db" "Creates monitoring role, schema and permissions on targets." "init.ts"
31+
instances = component "Targets / Instances" "Manages monitored targets (instances.yml)." "instances.ts"
32+
issues = component "Issues Client" "CRUD against the console Issues API." "issues.ts"
33+
auth = component "Auth (OAuth2/PKCE)" "Browser-based login and API-key handling." "auth-server.ts, pkce.ts"
34+
storage = component "Storage Client" "Uploads/downloads files to PostgresAI storage." "storage.ts"
35+
supabase = component "Supabase Client" "Runs queries via the Supabase Management API." "supabase.ts"
36+
config = component "Local Config" "Reads/writes ~/.postgres-ai settings." "config.ts, util.ts"
37+
}
38+
39+
mcp = container "MCP Server" "Model Context Protocol server exposing Issues tools to AI coding agents." "Node.js / TypeScript" "EntryPoint" {
40+
mcpTools = component "MCP Tools" "list_issues, view_issue, create_issue, update_issue, ..." "mcp-server.ts"
41+
}
42+
43+
pgwatchPg = container "pgwatch (Postgres sink)" "Collects pg_stat_statements, wait events, table/index stats; writes to the Postgres sink." "Go (patched v3.7.0)"
44+
pgwatchProm = container "pgwatch (Prometheus sink)" "Same collector, writes metrics to VictoriaMetrics." "Go (patched v3.7.0)"
45+
46+
reporter = container "Reporter" "Generates 45+ structured health-check reports (A/D/F/H/I/K/M/N series) from metrics & history." "Python" {
47+
reportGen = component "Report Generators" "Per-check report logic." "postgres_reports.py"
48+
schemas = component "Report Schemas" "29 JSON Schemas validating report output." "schemas/*.schema.json"
49+
}
50+
51+
flask = container "Monitoring Backend" "API for pg_stat_statements query history and PromQL proxying." "Python / Flask + Gunicorn" {
52+
api = component "API Endpoints" "Query history, query-id mapping, PromQL proxy." "app.py"
53+
promql = component "PromQL Utils" "Escaping/formatting PromQL." "promql_utils.py"
54+
}
55+
56+
grafana = container "Grafana" "13+ dashboards (Four Golden Signals, wait events, indexes, replication, ...)." "Grafana 12.x" "WebUI"
57+
vmetrics = container "VictoriaMetrics" "Prometheus-compatible time-series store for metrics." "VictoriaMetrics" "Database"
58+
sinkPg = container "Postgres Sink" "Stores historical metrics and check data." "PostgreSQL 17" "Database"
59+
60+
telemetry = container "Telemetry Agent" "Hourly system metrics (OOM, free RAM/disk) posted to the platform." "Node.js / TypeScript"
61+
indexPilot = container "pg_index_pilot" "Autonomous index lifecycle: bloat estimation, recommendations, reindexing." "SQL / PL-pgSQL + Bash"
62+
}
63+
64+
# ---------------------------------------------------------------
65+
# Relationships — Context level
66+
# ---------------------------------------------------------------
67+
dba -> pgai "Runs checkups, views dashboards, manages issues"
68+
aiAgent -> pgai "Reads reports, calls MCP tools"
69+
pgai -> targetPg "Connects to and queries"
70+
pgai -> console "Uploads reports, syncs issues, authenticates" "HTTPS"
71+
pgai -> supabaseApi "Executes SQL" "HTTPS"
72+
pgai -> awsApm "Reads metrics" "HTTPS"
73+
aiAgent -> llm "Analyzes JSON reports"
74+
75+
# ---------------------------------------------------------------
76+
# Relationships — Container level
77+
# ---------------------------------------------------------------
78+
dba -> pgai.cli "Runs commands"
79+
dba -> pgai.grafana "Views dashboards" "HTTPS"
80+
aiAgent -> pgai.mcp "Calls tools" "MCP/stdio"
81+
82+
pgai.cli -> targetPg "Runs health-check & prepare-db SQL" "SQL/TCP"
83+
pgai.cli -> console "Uploads reports, syncs issues, auth" "HTTPS"
84+
pgai.cli -> supabaseApi "Runs queries" "HTTPS"
85+
pgai.cli -> pgai.reporter "Triggers report generation"
86+
87+
pgai.mcp -> console "Reads/writes issues" "HTTPS"
88+
89+
pgai.pgwatchPg -> targetPg "Scrapes metrics" "SQL/TCP"
90+
pgai.pgwatchProm -> targetPg "Scrapes metrics" "SQL/TCP"
91+
pgai.pgwatchPg -> pgai.sinkPg "Writes metrics" "SQL/TCP"
92+
pgai.pgwatchProm -> pgai.vmetrics "Writes metrics" "HTTP"
93+
94+
pgai.reporter -> pgai.vmetrics "Reads metrics" "PromQL/HTTP"
95+
pgai.reporter -> pgai.sinkPg "Reads history" "SQL/TCP"
96+
pgai.reporter -> awsApm "Reads metrics" "HTTPS"
97+
pgai.reporter -> console "Uploads reports" "HTTPS"
98+
99+
pgai.flask -> pgai.vmetrics "Proxies PromQL" "HTTP"
100+
pgai.flask -> pgai.sinkPg "Reads query history" "SQL/TCP"
101+
pgai.grafana -> pgai.vmetrics "Queries metrics" "PromQL/HTTP"
102+
pgai.grafana -> pgai.sinkPg "Queries history" "SQL/TCP"
103+
pgai.grafana -> pgai.flask "Calls backend API" "HTTP"
104+
105+
pgai.telemetry -> console "Posts system telemetry" "HTTPS"
106+
pgai.indexPilot -> targetPg "Manages indexes" "SQL/TCP"
107+
108+
# ---------------------------------------------------------------
109+
# Relationships — Component level (CLI)
110+
# ---------------------------------------------------------------
111+
pgai.cli.cmd -> pgai.cli.checkup "Invokes"
112+
pgai.cli.cmd -> pgai.cli.init "Invokes"
113+
pgai.cli.cmd -> pgai.cli.instances "Invokes"
114+
pgai.cli.cmd -> pgai.cli.issues "Invokes"
115+
pgai.cli.cmd -> pgai.cli.auth "Invokes"
116+
pgai.cli.checkup -> targetPg "Runs SQL" "SQL/TCP"
117+
pgai.cli.checkup -> pgai.cli.checkupApi "Uploads report"
118+
pgai.cli.checkupApi -> console "HTTPS"
119+
pgai.cli.init -> targetPg "Creates role/schema" "SQL/TCP"
120+
pgai.cli.issues -> console "HTTPS"
121+
pgai.cli.auth -> console "OAuth2/PKCE" "HTTPS"
122+
pgai.cli.storage -> console "Uploads files" "HTTPS"
123+
pgai.cli.supabase -> supabaseApi "HTTPS"
124+
pgai.cli.checkup -> pgai.cli.config "Reads settings"
125+
}
126+
127+
views {
128+
systemContext pgai "SystemContext" {
129+
include *
130+
autolayout lr
131+
description "C4 Level 1 — PostgresAI in context."
132+
}
133+
134+
container pgai "Containers" {
135+
include *
136+
autolayout lr
137+
description "C4 Level 2 — containers inside PostgresAI."
138+
}
139+
140+
component pgai.cli "CliComponents" {
141+
include *
142+
autolayout lr
143+
description "C4 Level 3 — components inside the CLI."
144+
}
145+
146+
component pgai.reporter "ReporterComponents" {
147+
include *
148+
autolayout lr
149+
description "C4 Level 3 — components inside the Reporter."
150+
}
151+
152+
styles {
153+
element "Person" { shape person background #08427b color #ffffff }
154+
element "AI" { background #6b3fa0 color #ffffff }
155+
element "Software System" { background #1168bd color #ffffff }
156+
element "External" { background #999999 color #ffffff }
157+
element "Container" { background #438dd5 color #ffffff }
158+
element "Component" { background #85bbf0 color #000000 }
159+
element "Database" { shape cylinder }
160+
element "WebUI" { shape webBrowser }
161+
element "EntryPoint" { shape roundedBox }
162+
}
163+
164+
theme default
165+
}
166+
}

0 commit comments

Comments
 (0)