aiagent/skill/embedded/builtin/create-alert-rule/datasources/pgsql.md
prod: "metric"cate: "pgsql"recover_config.judge_type: 1 (metric type)database: the name of the database the query runs againstkeys.valueKey: the alias of the numeric column in the SELECT statementdb.schema.tableJust like MySQL uses db.table, PostgreSQL also requires writing the database name into the SQL.
The PostgreSQL plugin requires the SQL to use the database.schema.table three-part naming format (e.g. testdb.public.events), and internally the plugin will:
"db"."schema"."table" before executingIf the SQL only writes FROM events or FROM public.events (missing the database name), it reports the error no valid table name in format database.schema.table found.
The OSS edition of n9e's PostgreSQL data source does not support time variables such as $from/$to/$__timeFilter; the variables are not substituted.
Correct approach: use PostgreSQL's native time functions:
WHERE created_at >= NOW() - INTERVAL '5 minutes'WHERE created_at >= NOW() - INTERVAL '1 hour'WHERE DATE(created_at) = CURRENT_DATEexp is required and is the only field the alert engine evaluates (a rule without exp will never fire once created, with no error whatsoever)$<ref>.<valueKey alias>, e.g. $A.value > 5; with only one valueKey you may omit the alias and write $A directly, but with multiple valueKeys you must include the alias (a bare $A has an undefined value)mode is fixed at 1 (expression mode; the frontend displays exp as-is); join multiple conditions with && / ||, e.g. "$A.value > 10 && $B.value < 5"{
"rule_config": {
"queries": [
{
"ref": "A",
"sql": "SELECT count(*) AS value FROM testdb.public.events WHERE created_at >= NOW() - INTERVAL '5 minutes' AND severity = 'critical'",
"keys": {
"valueKey": "value",
"labelKey": ""
},
"interval": 60
}
],
"triggers": [
{
"mode": 1,
"exp": "$A.value > 5",
"severity": 1,
"recover_config": {"judge_type": 1}
}
]
}
}
| Field | Required | Description |
|---|---|---|
ref | ✅ | Query reference name |
sql | ✅ | PostgreSQL SQL. Must use the FROM db.schema.table three-part naming (e.g. FROM testdb.public.events) |
keys.valueKey | ✅ | Required, the alias of the numeric column |
keys.labelKey | ❌ | Label column alias(es), multiple separated by spaces |
interval | ❌ | Query execution interval, unit: total seconds (60=1 minute, 300=5 minutes, 3600=1 hour). Do not write interval_unit |
PostgreSQL's default schema is public, but there may be other schemas:
{
"ref": "A",
"sql": "SELECT count(*) AS value FROM testdb.monitoring.events WHERE created_at >= NOW() - INTERVAL '5 minutes'",
"keys": {"valueKey": "value"},
"interval": 60
}
| Description | SQL syntax |
|---|---|
| Default public schema | FROM testdb.public.events |
| Other schema | FROM testdb.monitoring.events |
| Multi-table JOIN | FROM testdb.public.orders o JOIN testdb.public.items i ON o.id = i.order_id |