Skip to main content
PostgreSQL databases have a hard limit on concurrent connections. When reached, your application stops accepting new requests. This happens more often than you’d think: a deployment introduces a connection leak, a background job goes haywire, or legitimate traffic simply exceeds your capacity. The frustrating part is that connection pool issues require manual detective work. You need to identify which queries are stuck, which services are hogging connections, and whether it’s safe to kill certain processes. Every minute spent investigating is another minute of downtime. This investigation follows the same pattern every time: check connection counts, find long-running queries, analyze service logs, identify the culprit. It’s repetitive work that’s perfect for automation.

Example Alert

Here is an example database connection pool alert our Agent will investigate:

Creating A Database Connection Pool Investigation Agent

Let’s create an Agent that runs every time we get a database connection pool exhaustion alert. Our Agent will extract the database host from the alert, analyze current connections and pool statistics, identify long-running queries and locks, and correlate logs across connected services to pinpoint which service is causing the issue. After installing Unpage, create the agent by running:
A yaml file will open in your $EDITOR. Paste the following Agent definition into the file:
Let’s dig in to what each section of the yaml file does:

Description: When the agent should run

The description of an Agent is used by the Router to decide which Agent to run for a given input. In this example we want the Agent to run only when the alert is about database connection pool exhaustion.

Prompt: What the agent should do

The prompt is where you give the Agent instructions, written in a runbook format. Make sure any instructions you give are achievable using the tools you have allowed the Agent to use (see below).

Tools: What the agent is allowed to use

The tools section explicitly grants permission to use specific tools. You can list individual tools, or use wildcards and regex patterns to limit what the Agent can use. To see all of the available tools your Unpage installation has access to, run:
In our example we added several custom shell commands for database diagnostics:
  • shell_check_db_connections
  • shell_check_db_pool_stats
  • shell_check_long_queries
  • shell_check_db_locks.
These are custom shell commands that query the internal tables of a PostgreSQL database to help diagnose connection pool errors. Custom shell commands allow you to extend the functionality of Unpage without having to write a new plugin.

Defining Custom Tools

To add our custom database analysis tools, edit ~/.unpage/profiles/default/config.yaml and add the following:
Shell commands have full access to your environment and can run custom scripts or call internal tools. See shell commands for more details.

Running Your Agent

With your Agent configured and the custom database analysis tools added, we are ready to test it on a real PagerDuty alert.

Testing on an existing alert

To test your Agent locally on a specific PagerDuty alert, run:

Listening for webhooks

To have your Agent listen for new PagerDuty alerts as they happen, run unpage agent serve and add the webhook URL to your PagerDuty account:

Example Output

Your Agent will update the PagerDuty alert with:
  • Current active connections vs maximum connection limit
  • Breakdown of connections by database and user
  • Long-running queries with their duration and wait events
  • Database locks that are blocking connection cleanup
  • Connected services with connection error patterns from logs
  • Timeline correlation showing which service started having issues first
  • Actionable recommendations for immediate connection pool recovery
The Agent transforms a frantic 3am investigation into a structured analysis, giving you the exact information needed to quickly identify and resolve the connection pool exhaustion.