Creating Alerts with pgX
pgX provides a streamlined workflow for creating alerts based on the panels in the app. This guide walks you through exporting an alert query from a pgX panel and setting it up in Grafana's alerting system.
Time to Complete
15-20 minutes
What You'll Accomplish
- Generate an alert-ready pgX query from any time-series or stat panel
- Add the query to a Grafana panel
- Create and configure a Grafana alert rule
Prerequisites
- Access to pgX and Grafana in Scout
- pgX configured and showing data for the database you want to alert on
- Permissions to create dashboards and alert rules
Overview
The alert creation process involves three main steps:
- Show Alert Query from pgX: Use the panel's Show alert query menu item to generate a ClickHouse query based on the panel's current filters and metric.
- Add Query to Dashboard: Create or add the query to a Grafana dashboard with the correct datasource configuration.
- Create Alert Rule: Configure the alert rule from the dashboard panel with thresholds and notification settings.
Step 1: Show Alert Query from pgX
Generate the Query
- Open the pgX application in Scout
- Configure your panel filters at the top of the page:
- Select the Environment and Cluster you want to monitor
- On the Queries tab, optionally narrow by Database, User, or Query Type
- Pick the panel you want to alert on. The Show alert query option is available on time-series and stat panels (the three-dot menu in the top-right corner of each panel)
- Click the panel's three-dot menu, then click Show alert query
Understanding the Generated Query
The exported query is a ClickHouse SQL statement that:
- Reads the same metric the panel reads
- Carries over the panel's environment, cluster, database, user, and query-type filters as literal values
- Uses Vertamedia macros for the time range, table, and bucket alignment so the alert engine can re-evaluate it against any time window
Example query structure:
SELECT
$timeSeries as t,
anyLast(Value)
FROM $table
WHERE $timeFilter
AND MetricName = 'pg_up'
AND ServiceName in ('pgdashex')
AND Attributes['cluster'] = 'prod-db'
GROUP BY t
ORDER BY t
The macros are expanded by the ClickHouse datasource at evaluation time:
| Macro | Expands to |
|---|---|
$timeFilter | TimeUnix >= toDateTime64(...) AND TimeUnix <= toDateTime64(...) |
$table | The configured database and table (e.g. acme-corp.otel_metrics_gauge) |
$timeSeries | A bucketed millisecond timestamp aligned to the evaluation interval |
Copy the Query
- In the Alert Query dialog, review the generated SQL query
- Click the "Copy Query" button to copy it to your clipboard
- Close the dialog
Step 2: Add Query to a Dashboard
Create or Open a Dashboard
- Navigate to Dashboards
- Either:
- Create a new dashboard by clicking "New Dashboard"
- Open an existing dashboard where you want to add the alert
Add a New Panel
- Click "Add" → "Visualization" to add a new panel
- Configure the panel settings:
Configure the Datasource
In the query editor:
- Datasource: Select
DS_ScoutAltCH(Scout Altinity ClickHouse datasource) - Click on the Query Options or Settings (gear icon)
- Configure the following settings:
- Database: Enter your organization name (e.g.,
acme-corp) - Table: Enter
otel_metrics_gauge - Timestamp Type: Select
DateTime64 - Timestamp Column: Enter
TimeUnix
- Database: Enter your organization name (e.g.,
Add Your Query
- Switch to SQL Editor mode (toggle button or "Edit SQL" option)
- Paste the alert query you copied from pgX
Configure the Visualization
- Set panel title (e.g., "PostgreSQL Up")
- Configure value options, thresholds, and colors as needed
- Click "Apply" to save the panel
Save the Dashboard
- Click "Save dashboard" (disk icon)
- Give your dashboard a meaningful name
- Optionally add it to a folder
- Click "Save"
Step 3: Create Alert Rule
Now that you have a dashboard panel with your pgX query, you're ready to create the alert rule.
- On your dashboard, locate the panel you just created
- Click the panel title or three dots (⋮) menu
- Select "More..." → "New alert rule"
That's it! Now you can follow the comprehensive Creating Alerts in Grafana guide to configure your alert rule, set thresholds, configure notifications, and test your alert.
The general alerting guide covers:
- Setting alert rule names and descriptions
- Defining query conditions and thresholds
- Configuring evaluation behavior and timing
- Adding alert details and templates
- Setting up notifications and contact points
- Testing and troubleshooting alerts
- Best practices for alerting
Verification
- Use Preview alerts to confirm the rule evaluates successfully
- Set a short evaluation interval temporarily to validate behavior
- Trigger a known database condition and verify the alert transitions to
Firing (e.g., stop the monitored database to test a
pg_upalert)
Best Practices
Query Scoping
- Pick a specific cluster: Always set the cluster filter in pgX before exporting — alerts on the implicit "all clusters" set are usually noisy
- Pick a specific database/user/query when alerting on per-query metrics (Queries tab drawer) so the alert tracks one concrete thing
Troubleshooting
If the alert doesn't fire as expected:
- Confirm the panel query returns a non-zero value in the chosen time range
- Verify the table is
otel_metrics_gaugeand the timestamp column isTimeUnixwith typeDateTime64 - Ensure the rule uses the same datasource and query as the panel
- Use an evaluation interval of at least 5 minutes — narrower windows can give zero data points
Next Steps
- Dashboards and Alerts - General dashboard and alerting overview
- Grafana Alerting Documentation - Official Grafana alerting guide