Architecture
Deep-dive into PostgresAI monitoring system components and data flow.
System overviewโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PostgresAI Monitoring โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ
โ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโ โ
โ โ PostgreSQL โ โ PostgreSQL โ โ PostgreSQL โ โ
โ โ Cluster A โ โ Cluster B โ โ Cluster C โ โ
โ โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ โโโโโโโโฌโโโโโโโ โ
โ โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โ โโโโโโโโผโโโโโโโ โ
โ โ pgwatch โ โ
โ โ (collector)โ โ
โ โโโโโโโโฌโโโโโโโ โ
โ โ Prometheus format โ
โ โโโโโโโโผโโโโโโโโโ โ
โ โVictoriaMetricsโ โ
โ โ (storage) โ โ
โ โโโโโโโโฌโโโโโโโโโ โ
โ โ PromQL โ
โ โโโโโโโโผโโโโโโโโโ โ
โ โ Grafana โ โ
โ โ(visualization)โ โ
โ โโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Componentsโ
pgwatch โ Metrics collectorโ
Purpose: Collect PostgreSQL metrics and expose in Prometheus format
Key functions:
- Execute SQL queries against PostgreSQL
- Transform results to Prometheus metrics
- Expose the
/pgwatchmetrics endpoint on:9091(Prometheus sink)
Configuration:
| Setting | Default | Description |
|---|---|---|
| Scrape interval | 30s | VictoriaMetrics scrapes the pgwatch-prometheus job every 30s (the 15s global default is overridden for this job; the separate query-info job โ metrics_path: /query_info_metrics โ runs every 300s) |
| Collection interval | per-metric | Each metric group has its own interval in metrics.yml (most 30s; pg_stat_activity/wait_events 15s) |
Collected data sources:
| View | Metrics |
|---|---|
| pg_stat_statements | Query performance |
| pg_stat_activity | Session state |
| wait_events (from pg_stat_activity) | Wait event sampling |
| pg_stat_all_tables / table_stats | Table access patterns |
| pg_stat_all_indexes | Index usage |
| db_stats (from pg_stat_database) | Database-level stats |
| bgwriter | Checkpoint behavior |
VictoriaMetrics โ time-series databaseโ
Purpose: Store and query metrics
Key functions:
- Ingest metrics from pgwatch
- Compress and store time-series data
- Execute PromQL queries
Performance characteristics:
| Aspect | VictoriaMetrics | Prometheus |
|---|---|---|
| Compression | 10x better | Baseline |
| Query speed | 2-5x faster | Baseline |
| Memory usage | 3-5x lower | Baseline |
| High availability | Built-in clustering | Federation |
Storage model:
In this deployment VictoriaMetrics is started with -storageDataPath=/victoria-metrics-data, and
the victoria_metrics_data Docker volume is mounted at /victoria-metrics-data. The on-disk
layout under that path follows VictoriaMetrics' standard structure (recent vs. historical data
parts, a label index, and optional snapshots).
Grafana โ Visualizationโ
Purpose: Dashboard and alerting UI
Key functions:
- Render time-series charts
- Dashboard templating with variables
- Unified alerting
Dashboard structure:
PostgresAI dashboards:
โโโ 01. Node overview (cluster-level)
โโโ 02. Query analysis (top-N queries)
โโโ 03. Single query (query deep-dive)
โโโ 04. Wait events (session analysis)
โโโ 05. Backups (WAL archiving)
โโโ 06. Replication (lag monitoring)
โโโ 07. Autovacuum (vacuum status)
โโโ 08. Table stats (table analysis)
โโโ 09. Single table (table deep-dive)
โโโ 10. Index health (index analysis)
โโโ 11. Single index (index deep-dive)
โโโ 12. SLRU (cache stats)
โโโ 13. Lock contention (lock waits)
โโโ 14. I/O statistics (pg_stat_io, PG16+)
โโโ Self-monitoring (stack health)
Data flowโ
Collection flowโ
1. pgwatch connects to PostgreSQL
โโโ Uses monitoring user credentials
โโโ Executes metric collection queries
2. Query results transformed to metrics
โโโ Column values โ metric values
โโโ Column names โ labels
3. Metrics exposed on the `/pgwatch` endpoint (`:9091`)
โโโ Prometheus exposition format
โโโ Timestamp attached
4. VictoriaMetrics scrapes the pgwatch-prometheus sink
โโโ HTTP GET pgwatch-prometheus:9091/pgwatch
โโโ `pgwatch-prometheus` job scrape_interval: 30s (scrape_timeout 25s)
5. Metrics stored in VictoriaMetrics
โโโ Compressed time-series storage
โโโ Indexed by labels
Query flowโ
1. User opens Grafana dashboard
โโโ Dashboard loads panel queries
2. Grafana sends PromQL to VictoriaMetrics
โโโ Variables substituted
โโโ Time range applied
3. VictoriaMetrics executes query
โโโ Index lookup by labels
โโโ Data retrieval from storage
โโโ Aggregation/calculation
4. Results returned to Grafana
โโโ Time series data
โโโ Rendered as charts
Metric namingโ
Conventionโ
pgwatch exports series as pgwatch_<metric-group>_<column>. The Prometheus metric type is
driven by each metric group's gauges: list in config/pgwatch-prometheus/metrics.yml: a column
is emitted as a Prometheus gauge only if its group lists it (or uses gauges: ['*']); otherwise it
is emitted as a counter. Note this is the exported type, not the PostgreSQL semantics โ the
db_stats and pg_stat_statements groups use gauges: ['*'] / explicit gauge lists, so their
cumulative columns (e.g. xact_commit, exec_time_total) are exported as gauges even though
they only ever increase. Cumulative columns in the pg_stat_database family are also not
_total-suffixed.
pgwatch_<metric-group>_<column>
Examples (Type = the exporter's emitted Prometheus type):
pgwatch_db_stats_xact_commit # Gauge (transactions committed; db_stats uses gauges: ['*'])
pgwatch_db_stats_numbackends # Gauge (current backends)
pgwatch_pg_stat_statements_exec_time_total # Gauge (total exec time, ms; listed in pg_stat_statements gauges)
Labelsโ
The cluster label is cluster (set from custom_tags.cluster). cluster_name is only the
Grafana template variable; dashboard filters select with cluster="$cluster_name".
pgwatch_<metric-group>_<column>{
cluster="production",
node_name="primary",
datname="myapp",
schemaname="public",
relname="users"
}
Storage requirementsโ
Calculationโ
Storage = metrics_per_second ร bytes_per_sample ร retention_seconds
Typical values:
- metrics_per_second: 50-200 per database
- bytes_per_sample: 3-5 bytes (VictoriaMetrics compressed)
- retention: 1,209,600 seconds (14 days)
Example: 5 databases, 14-day retention
= 5 ร 100 ร 1.5 ร 1,209,600
= 907,200,000 bytes โ 907 MB (โ 865 MiB)
Scaling factorsโ
| Factor | Impact |
|---|---|
| More databases | Linear increase |
| More tables/indexes | Sublinear (only active tracked) |
| Longer retention | Linear increase |
| Shorter scrape interval | Linear increase |
High availabilityโ
HA architectureโ
โโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Load Balancer โ
โโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ โ โ
โโโโโโผโโโโโ โโโโโโผโโโโโ โโโโโโผโโโโโ
โpgwatch-1โ โpgwatch-2โ โpgwatch-3โ
โโโโโโฌโโโโโ โโโโโโฌโโโโโ โโโโโโฌโโโโโ
โ โ โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโ
โ remote_write
โ
โโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโ
โ VictoriaMetrics Cluster โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โvmstorage1โ โvmstorage2โ โvmstorage3โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโผโโโโโ
โ Grafana โ
โ (HA) โ
โโโโโโโโโโโ
Failure modesโ
| Component failure | Impact | Recovery |
|---|---|---|
| Single pgwatch | Partial data loss | Automatic failover |
| Single vmstorage | No data loss (replication) | Automatic |
| All pgwatch | Collection stops | Manual restart |
| All vmstorage | Query unavailable | Restore from backup |
| Grafana | UI unavailable | Load balancer failover |
Data privacy โ metadata onlyโ
PostgresAI monitoring collects only database metadata โ no actual data or query parameters are ever accessed.
Collected data typesโ
| Data type | Example | Storage location |
|---|---|---|
| Database statistics | Connections, transactions, cache hit ratio | Prometheus (VictoriaMetrics) |
| Normalized queries | select * from users where id = $1 | PostgreSQL sink |
| Wait events | CPU, IO, Lock, LWLock | Prometheus (VictoriaMetrics) |
| Table statistics | Row count, dead tuples, last vacuum | Prometheus (VictoriaMetrics) |
| Index statistics | Size, scans, tuples read | Prometheus (VictoriaMetrics) |
| Column statistics | From pg_statistic for bloat estimates | Prometheus (VictoriaMetrics) |
NOT collectedโ
- Actual table data (row contents)
- Query parameter values (
$1,$2remain as placeholders) - Application secrets or credentials
- Connection passwords
Metric definitionsโ
Review exactly what is collected:
- Prometheus metrics: pgwatch-prometheus/metrics.yml
- PostgreSQL metrics (with query texts): pgwatch-postgres/metrics.yml
Verify monitoring database role and its permissionsโ
# See exact SQL for creating monitoring role
npx postgresai@latest prepare-db --print-sql
Security architectureโ
Networkโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ DMZ / Public โ
โ โ
โ โโโโโโโโโโโ โ
โ โ Grafana โ โ HTTPS (443) โ
โ โโโโโโฌโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Internal Network โ
โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ VictoriaMetrics (sink-prometheus) โ โ
โ โ (port 9090 internal, host 59090) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ scrapes pgwatch-prometheus:9091/pgwatch โ
โ โโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ pgwatch (pgwatch-postgres, pgwatch-prometheus)โ โ
โ โ metrics scraped on pgwatch-prometheus:9091 โ โ
โ โ (web/health ports 8080/8089 are internal) โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Database Network โ
โ โ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โPostgreSQLโ โPostgreSQLโ โPostgreSQLโ โ
โ โโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Credentialsโ
| Component | Credential type | Storage |
|---|---|---|
| PostgreSQL | Password | Environment variable / secret |
| VictoriaMetrics | Basic auth | Config file |
| Grafana | OAuth/LDAP | Database |
Performance characteristicsโ
Collection overheadโ
Frequencies below are the full preset intervals from config/pgwatch-prometheus/metrics.yml
(see the per-metric note above):
| Metric type | Query cost | Frequency |
|---|---|---|
pg_stat_database (db_stats) | Low | 30s |
| pg_stat_statements | Medium | 30s |
pg_stat_all_tables / table_stats | Medium | 30s |
Bloat estimation (pg_table_bloat, pg_btree_bloat) | High | 7200s (2h) |
Query performanceโ
| Query type | Typical latency |
|---|---|
| Instant query | 10-100ms |
| Range query (1h) | 50-200ms |
| Range query (24h) | 200-500ms |
| Range query (7d) | 500ms-2s |
Resource usageโ
| Component | CPU | Memory | Disk I/O |
|---|---|---|---|
| pgwatch | Low | 256 MiB | Minimal |
| VictoriaMetrics | Medium | 2 GiB+ | Medium |
| Grafana | Low | 512 MiB | Low |