---
title: "Database Monitoring — Depfloy docs"
description: "Charts, the slowest statements, live sessions and alerts for PostgreSQL, MySQL and MariaDB servers, sampled every 60 seconds."
url: "https://depfloy.com/docs/database-monitoring"
---

# Database Monitoring

Database monitoring reads PostgreSQL, MySQL and MariaDB from the inside: connections, throughput, cache hit ratio, the longest open transaction, lock waits and replication lag every 60 seconds, the 50 statements that took the most time, and the sessions running right now. A database server's **Database monitoring** section is where you turn it on and read it.

Database monitoring is available on the Scale plan. [Compare plans →](https://depfloy.com/pricing)

On another plan, the section says that database monitoring is part of the Scale plan and offers the upgrade.

## Turn it on

Open a server that runs PostgreSQL, MySQL or MariaDB and pick **Database monitoring** from the **Overview** group of its sections. Click **Enable**.

Enabling needs permission to update the server and to see its monitoring: Owner, Admin, Manager or Developer. If the server has no monitoring agent yet, Depfloy installs it first.

Depfloy then creates a database user named `depfloy` that signs in over the local socket, without a password:

| Engine | How it signs in | What it is granted |
| --- | --- | --- |
| PostgreSQL | peer authentication | `pg_monitor` and `pg_signal_backend` |
| MySQL | `auth_socket` | `PROCESS`, `REPLICATION CLIENT`, `CONNECTION_ADMIN`, `SYSTEM_USER` and read access to `performance_schema` |
| MariaDB | `unix_socket` | `PROCESS`, `BINLOG MONITOR`, `SLAVE MONITOR`, `CONNECTION ADMIN` |

The rights to signal a backend or administer connections are what let the **Activity** tab stop a query. **Disable** stops collecting and keeps this user and your alerts.

### PostgreSQL: Restart Required

Statement statistics on PostgreSQL come from `pg_stat_statements`, which PostgreSQL loads only when it starts. If the running server does not load it yet, enabling adds it to `shared_preload_libraries`, keeping any library already listed there, and the section shows **Restart Required** with a **Restart PostgreSQL** button. Every other metric is collected in the meantime; the **Queries** tab fills in after the restart.

Depfloy never restarts the database on its own. Choose a quiet moment and click the button. Servers Depfloy sets up today load `pg_stat_statements` from the start and need no restart.

On a PostgreSQL replica, enable monitoring on the primary first. The replica is read-only and receives the `depfloy` role and the extension from its primary.

### MariaDB: no statement list

The slowest-statements list needs `performance_schema`, which MariaDB ships turned off. Turning it on means editing `my.cnf` and restarting MariaDB, so Depfloy leaves it off. Connection, InnoDB and replication metrics are collected, and the section says why the **Queries** tab is empty.

## Overview

The **Overview** tab shows the latest values and a chart for each:

-   **Connections**, out of `max_connections`
-   **Throughput**: transactions per second on PostgreSQL, queries per second on MySQL and MariaDB
-   **Cache hit ratio**: reads served from memory as a percent of all reads
-   **Replication**: on a primary, how many replicas it has; on a replica, how far it is behind
-   **Longest open transaction**
-   **Lock waits**

Pick a range of 1 hour, 6 hours, 24 hours or 7 days. The 1-hour and 6-hour charts use the per-minute samples; 24 hours and 7 days use hourly averages. Depfloy keeps the per-minute samples for 14 days and the hourly summaries for 90 days.

## Queries

The **Queries** tab lists the 50 statements that took the most total time in the selected range, with calls, mean, total and maximum time, and rows. Sort by any column. Clicking a row opens the statement with its calls and mean time across the range.

Statements are stored normalized: `$1` or `?` stands where a value was, so the list never holds the values your application sent. Only statements that begin with `SELECT`, `INSERT`, `UPDATE`, `DELETE`, `WITH` or `MERGE` are collected; utility statements such as `ALTER ROLE … PASSWORD` or `SET` are not. The monitoring user’s own queries are left out.

### When a statement starts counting

-   **PostgreSQL 17 and later, and MySQL:** a statement is counted from its first run, including one that ran only once in a burst. On MariaDB the same applies when you have turned `performance_schema` on yourself. This needs version 1.2.0 of the metrics agent, which a server gets when database monitoring is enabled or its agent is upgraded.
-   **PostgreSQL 16:** a statement is counted from the sample after the one it first appears in, one minute later. Runs before that are not in the list.

## Activity

The **Activity** tab shows the database as it is now, read from the server when you open the tab and every 20 seconds while it stays open. Depfloy does not store it.

**Sessions** lists every client connection with its user, database, client address, state, wait event, how long its current statement has run, the age of its transaction, and the first 500 characters of the statement. That statement is the running SQL with its values, which is why viewing it needs the same access as the rest of the section.

**Lock waits** draws a tree from the session that holds a lock to the sessions waiting on it. PostgreSQL reports this through `pg_blocking_pids()`, MySQL through `performance_schema.data_lock_waits` and MariaDB through `information_schema.INNODB_LOCK_WAITS`.

### Stopping a query

Each session has two actions:

-   **Cancel** stops the running statement and keeps the connection open (`pg_cancel_backend` or `KILL QUERY`).
-   **Terminate** closes the connection (`pg_terminate_backend` or `KILL CONNECTION`).

Depfloy does not stop PostgreSQL superuser sessions, or MySQL and MariaDB sessions belonging to `root` and the engine’s own system accounts; those rows have no actions. Every stop is recorded in the [Activity log](https://depfloy.com/docs/activity) with the session id and the first 120 characters of the statement.

If **Cancel** or **Terminate** answers that the monitoring user cannot stop sessions, disable and enable database monitoring once to grant the rights listed above. Viewing keeps working until then.

Stopping needs permission to update the server as well as to see its monitoring: Owner, Admin, Manager or Developer.

Each server allows six requests a minute to each of these views and actions, shared by everyone looking at that server.

## Alerts

The **Alerts** tab holds alert rules for the database. **Add Alert** creates one; each row’s menu has **Activate** or **Deactivate**, and **Delete**. Creating, pausing and deleting needs permission to update the server.

| Metric | Unit | Engines | Form default |
| --- | --- | --- | --- |
| Database connections | % of `max_connections` | all three | above 85 |
| Replication lag | seconds, measured on replicas only | all three | above 30 |
| Deadlocks | per minute | all three | above 0 |
| Longest transaction | seconds | all three | above 300 |
| Cache hit ratio | % | all three | below 90 |
| Slow queries | statements over `long_query_time` per minute | MySQL, MariaDB | above 10 |
| Transaction ID age | oldest unfrozen transaction ID | PostgreSQL | above 1,000,000,000 |

PostgreSQL stops accepting writes as transaction ID age nears 2 billion, which is what the last metric watches for.

Enabling database monitoring adds two alerts: connections above 85% and a transaction open for more than 5 minutes. A replica also gets “more than 30 seconds behind” the first time it reports as one. All three can be edited or deleted.

### When an alert fires and resolves

Database samples arrive once a minute. An alert fires after two samples in a row cross its threshold and resolves after two samples in a row are back within it, so about two minutes each way.

While an alert is firing, another notification goes out when the value gets worse by more than a set amount since the last notification, and once an hour as a reminder:

| Metric | Notifies again after |
| --- | --- |
| Database connections, cache hit ratio | 5 points |
| Replication lag | 30 seconds |
| Longest transaction | 60 seconds |
| Slow queries | 5 per minute |
| Deadlocks | 1 per minute |
| Transaction ID age | 10,000,000 |

When several alerts on the same server fire on the same sample, they arrive as one email and one Slack message listing each alert with its current value. Alerts that resolve on the same sample are grouped the same way.

Notifications go to everyone in the organization by email, and to Slack for members who connected it under [Settings → Notifications](https://depfloy.com/docs/notifications).

## Every server on the Monitoring page

The **Monitoring** page in the sidebar gets a **Database** column when at least one server sends database metrics. It shows connection use, and replica lag for replicas. See [Server Monitoring](https://depfloy.com/docs/server-monitoring#every-server-on-one-page).

## From an AI assistant

Four MCP tools read the same data and stop queries under the same plan and role rules: `get_database_metrics`, `list_slow_queries`, `get_database_activity` and `stop_database_query`. See [MCP → Database monitoring](https://depfloy.com/docs/mcp#database-monitoring).

## The metrics agent token

The monitoring agent on each server signs its reports with a token of its own. Depfloy writes it to `/etc/system-metrics.conf` as `API_KEY` and never shows it. Do not edit that line by hand: the agent stops reporting until the token is rotated.

`POST /api/v1/servers/{server}/metrics-token/rotate` replaces the token. It needs `server:update`. The old token is refused at once; Depfloy then writes the new one to the server and restarts the agent, and samples sent in between are not recorded. On a server with monitoring off, the agent receives the token when monitoring is installed.

## API

Every endpoint below needs the Scale plan and the same permissions as the section.

| Endpoint | What it does |
| --- | --- |
| `POST /api/v1/servers/{server}/database-monitoring` | Enable |
| `DELETE /api/v1/servers/{server}/database-monitoring` | Disable |
| `GET /api/v1/servers/{server}/database-monitoring/metrics?range=1h|6h|24h|7d` | Overview values, charts and the statement list. `409` while monitoring is off |
| `GET /api/v1/servers/{server}/database-monitoring/queries/{digest}?range=…` | One statement over the range |
| `GET /api/v1/servers/{server}/database-monitoring/activity` | Live sessions |
| `GET /api/v1/servers/{server}/database-monitoring/locks` | The lock tree |
| `POST /api/v1/servers/{server}/database-monitoring/activity/{pid}/cancel` | Cancel the statement |
| `POST /api/v1/servers/{server}/database-monitoring/activity/{pid}/terminate` | Close the connection |

The live endpoints answer `429` beyond six requests a minute per server.

Database alerts use the existing `POST`, `PUT` and `DELETE /api/v1/servers/{server}/alerts` with these `metric_type` values: `db_connections_pct`, `db_replication_lag_seconds`, `db_deadlocks_per_min`, `db_longest_transaction_seconds`, `db_cache_hit_ratio`, `db_slow_queries_per_min`, `db_xid_age`. A database metric answers `422` when the plan, the monitoring state or the engine does not support it.
