Schedule icon
Webhook icon
Query icon
Script icon
If icon
SlackIncomingWebhook icon
Log icon
OutputValues icon

Guard PostgreSQL Connection Saturation with Slack Alerts

Guard PostgreSQL connections with Kestra. Track usage vs max_connections, break down states, and alert Slack early.

Categories
DataInfrastructure

Stop connection exhaustion before it cascades into 504s. This blueprint polls pg_stat_activity every 5 minutes, compares total connections against max_connections, breaks usage down by state (active, idle in transaction, idle, lock waits) and top application, and posts a Slack alert when usage crosses warn (default 70%) or critical (default 90%) thresholds.

How it works

  1. every_5_minutes (Schedule, */5 * * * *) launches the check; on_demand_webhook allows incident-response runs.
  2. query_connection_stats (io.kestra.plugin.jdbc.postgresql.Query) aggregates pg_stat_activity states and reads max_connections from pg_settings.
  3. query_top_apps groups connections by application_name (top 10) for instant blame attribution.
  4. evaluate_saturation (Python Script) computes usage percent and warn / critical / ok severity.
  5. route_by_severity (If) posts a Slack webhook alert on breach or logs a quiet all-clear.
  6. emit_saturation_metrics (OutputValues) exports usage_pct and severity for history.

What you get

  • Early warning at 70% with escalation at 90% (both tunable).
  • State breakdown surfacing idle in transaction leaks fast.
  • Top-application attribution so you know which service to throttle.
  • Copy-paste pg_terminate_backend triage guidance in the alert.

Who it's for

  • DBAs and SREs guarding Postgres-backed web tiers.
  • Backend teams debugging pool exhaustion and transaction leaks.
  • Platform teams sizing PgBouncer or RDS max_connections.

Prerequisites

  • Reachable Postgres 12+ with SELECT on pg_stat_activity and pg_settings.
  • Slack Incoming Webhook URL.

Secrets

  • POSTGRES_USER: DB user with catalog read access.
  • POSTGRES_PASSWORD: DB password.
  • SLACK_WEBHOOK_URL: Slack alert destination.
  • WEBHOOK_KEY: webhook trigger auth key.

Quick start

  1. Set the four secrets above.
  2. Point postgres_host, postgres_port, database_name at your instance.
  3. Tune warn_pct / critical_pct to your pool headroom.
  4. Run manually, then enable the 5-minute schedule.

How to extend

  • Terminate idle in transaction sessions older than N minutes automatically.
  • Ship usage_pct to Prometheus or BigQuery for burn-rate dashboards.
  • Add PagerDuty escalation on critical persisting for 3 consecutive runs via KV.

Links

Inputs

  • postgres_host, postgres_port, database_name: connection target.
  • warn_pct (INT, default 70), critical_pct (INT, default 90): alert thresholds.

Outputs

  • emit_saturation_metrics.values.usage_pct: connection usage percent.
  • emit_saturation_metrics.values.severity: ok, warn, or critical.
See How

New to Kestra?

Use blueprints to kickstart your first workflows.