Here's a made-up example. On a Wednesday afternoon, customer support starts hearing that paid orders still show "awaiting payment." The on-call engineer checks the dashboards: the payment service is under 30% CPU, there are zero 5xx responses, and the message queue has no backlog. It takes 15 minutes to find the cause. An upstream payment provider renamed a callback field, the gateway's signature check failed, and the gateway still returned 200 OK with a plain log line. More than 400 orders are sitting in the payment_order table with status PROCESSING.
Healthy hosts and healthy APIs don't mean the business is healthy. Stuck orders, reconciliation mismatches, a sudden drop in orders for a key category, and batch jobs that didn't run leave traces only in your business or analytics database. Writing read-only SQL alert rules directly against those tables costs less than maintaining a cron script or exporter per check, and the alerts go through the same routing and on-call process as your infrastructure alerts.
Where cron scripts and custom exporters fall short
Teams that want to watch business state in a database usually start with one of two approaches.
A cron script that posts to a chat group. The first script takes ten minutes. By the twentieth, they're hard to maintain:
- The script only does "found something, send a message." There's no multi-cycle check. While the problem persists it posts every 5 minutes, and when it's fixed there's no recovery notice.
- Messages only reach a chat group. They can't trigger phone calls, escalation, or a shift swap. An alert in a group chat isn't the same as an owner; see Alerts in a group chat don't mean anyone owns the incident.
- If the host running the script fills its disk, or the database password rotates, the script fails silently. Often nobody notices it stopped two weeks ago until the business breaks again.
A custom Prometheus exporter that turns SQL results into metrics. This works for plain counts but gets awkward with business detail. An on-call engineer paged for stuck orders wants to know which payment channel is stuck, when the oldest stuck order started, and one example order number. Put the order number and timestamp in labels, and every query creates a new batch of time series, so cardinality keeps growing. Leave them out, and the engineer sees only stuck_orders = 47, then has to connect to the VPN, log in to a bastion host, and rerun the SQL before investigating.
How a SQL alert rule splits query results
For the stuck-order case, one aggregate query gives you both per-channel severity and a troubleshooting sample (MySQL syntax):
SELECT channel,
COUNT(*) AS stuck_cnt,
MIN(order_no) AS sample_order,
MIN(updated_at) AS oldest_at
FROM payment_order
WHERE status = 'PROCESSING'
AND updated_at < now() - INTERVAL 5 MINUTE
AND updated_at > now() - INTERVAL 2 HOUR
GROUP BY channel
The four columns serve three purposes:
| Purpose | Columns here | What it does |
|---|---|---|
| Label | channel | Identifies the alert object. Each channel fires and recovers on its own. Pick stable, low-cardinality dimensions only |
| Value | stuck_cnt | Compared against thresholds, for example over 10 sends a message and over 30 places a phone call |
| Additional information | sample_order, oldest_at | Sent to the on-call engineer with the alert, but not used to identify the alert |
Keep values that change every time, such as order numbers, timestamps, and request IDs, in additional information. As labels, each row becomes a separate alert, which is the same cardinality problem the exporter had.
Which check mode fits which business case
Comparing a number to a threshold is one of three check modes. The other two cover common business checks as well:
| Business check | Check mode | Query shape and trigger |
|---|---|---|
| Order backlog, conversion drop, rising error transactions | Threshold evaluation | Aggregate with GROUP BY into a numeric column; use different thresholds for Warning and Critical |
| End-of-day reconciliation mismatch, negative inventory, invalid order state | Data exists | Put the abnormal condition in WHERE or HAVING; any returned row fires the alert |
| Store checkout offline, probe disconnected, hourly settlement not run | Missing data | Query records that should keep arriving; a label seen before that's missing for N consecutive cycles fires the alert |
Reconciliation is a typical data-exists check: the mismatch table should have no rows. In PostgreSQL:
SELECT merchant_id, bill_date, diff_amount
FROM daily_reconciliation
WHERE checked_at > NOW() - INTERVAL '10 minutes'
AND diff_amount <> 0
Normally the query returns nothing. When one merchant is off by a single cent, the query returns that row, and the alert carries merchant_id and diff_amount. Data exists has no severity levels; if you need Warning and Critical, use threshold evaluation.
Connecting production databases: read-only access and load
When you connect a business database to an alerting system, DBAs usually ask two questions: can it write bad data, and can it slow down production?
The read-only boundary is the database account. Create a dedicated read-only account for alert rules and point it at a read replica or an analytics store such as ClickHouse, away from the primary that serves transactions. Statement checks in the alerting system stop only obvious writes. They don't replace account permissions.
Every check query needs a time window. At one run per minute, a rule runs 1,440 times a day. The time condition in WHERE must hit an index, for example now() - INTERVAL 10 MINUTE in MySQL or SYSDATE - INTERVAL '5' MINUTE in Oracle. ClickHouse is sensitive to time partitions, so filter on the partition time column.
Use timeouts and connection limits to cap the impact of one rule. When a query misses its index, the timeout ends it, and the pool limit caps how many connections it can hold.
Setting it up in Flashduty Monitors
Flashduty Monitors supports SQL alert rules for MySQL, PostgreSQL, Oracle, and ClickHouse, and sends alert events to Flashduty On-call for routing and notification. Here's how each point above maps to the product.
Field mapping. Query result field mapping has two settings: value fields and label fields. You don't configure additional information. Once you select label fields explicitly, every column that's neither a label nor a value becomes additional information. If you leave label fields empty, all non-value columns become labels and there's no additional information, so the stuck-order rule must select channel explicitly. Monitors lowercases returned column names, so type field names in lowercase.
Threshold expressions. The query name is the variable prefix. For the stuck-order rule, set Warning: $A.stuck_cnt > 10 and Critical: $A.stuck_cnt > 30. Rules check every 15 seconds by default (@every 15s) and fire after 3 consecutive matches by default, so one noisy reading doesn't page anyone.
Recovery. By default, threshold evaluation recovers when no threshold matches. Once a channel has no stuck orders, the GROUP BY query returns no row for it. The option Also count a check as normal when the alert object returns no data handles that case: new rules have it on by default, and it needs alert engine v0.77.0 or later. You can also write a separate recovery query that uses label variables to re-check one object. For example, recover only when callbacks succeed again:
SELECT channel
FROM channel_callback_status
WHERE channel = '${channel}'
AND last_success_at > now() - INTERVAL 5 MINUTE
With the recovery mode Recovery when expression finds data, the alert for that channel recovers as soon as this query returns a row. The main query can't use variables. MySQL rules that use variables in recovery queries need alert engine v0.76.0 or later.
Watch the default for data-exists rules like the reconciliation check: Recover if no data. The query above looks back only 10 minutes, so once the mismatch row ages out of the window, the alert recovers even if nobody fixed the mismatch. If someone must confirm it, set the recovery mode to Keep the alert active until it is closed manually.
Read-only checks. MySQL rules accept only a single read-only statement. It must start with SELECT, WITH, SHOW, DESC, DESCRIBE, or EXPLAIN. It can't contain INTO, INSERT, UPDATE, or DELETE, multiple statements, or executable comments. Rules that break this are rejected at save time. The docs state that this is a lexical policy and that a read-only data source account is the real boundary. The PostgreSQL, Oracle, and ClickHouse rule docs don't describe a similar check, so for those databases the read-only account matters even more.
Timeouts and connection pools. MySQL, Oracle, and PostgreSQL data sources default to a 5000 ms query timeout and 32 max connections. ClickHouse data sources also have Maximum execution time (seconds), default 60, which is sent to the ClickHouse server with the query.

Network and credentials. The alert engine, monitedge, runs in your private network. It reads from local data sources, evaluates rules, and pushes only alert events to Flashduty, so the database needs no public endpoint. For more on this deployment model, see Keep monitoring data local and manage alerting in one place. Data source usernames and passwords accept references such as ${env:DB_PASS}, which each alert engine resolves in its own process. The resolved value isn't uploaded to Flashduty. This needs alert engine v0.46.0 or later; see Reference credentials locally in the alert engine.
Team access. A data source can have a Managing team and Query-allowed teams. Once set, only members of those teams (plus the creator, the main account, and Admins) can query the data source or reference it in alert rules and dashboards; see Data source permissions.
After the alert fires. In On-call, the event follows the channel's escalation rule to notify the on-call engineer and escalates to the next level if nobody responds. If your account has AI SRE enabled, AI SRE can query data sources already connected and enabled in Monitors directly, without an extra MCP server or Runner. AI SRE is available on every plan, is enabled separately in the console, and is billed in credits.
You can try Flashduty for free in the Flashduty console.



