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

PostgreSQL Blocking Transaction and Lock Contention Monitor

Monitor PostgreSQL lock contention and blocking transactions with Kestra. Inspect pg_locks, detect cascading wait trees, and alert DBAs via Slack.

Categories
DataInfrastructure

In production PostgreSQL environments, slow queries rarely take down an application on their own, but cascading lock conflicts do. When an uncommitted transaction, slow migration, or unindexed update acquires an exclusive lock on a hot table, dozens of incoming requests queue up behind it. Within minutes, connection pools saturate, application threads starve, and the web tier throws 504 Gateway Timeouts.

This blueprint introspects PostgreSQL's native pg_locks and pg_stat_activity system catalogs to identify root blocking sessions, the queries they are running, their application origins, and how long downstream queries have been waiting. If lock wait times exceed your threshold, it sends an actionable Slack alert with the exact PID and copy-paste remediation commands.

How it works

  1. query_blocking_locks (io.kestra.plugin.jdbc.postgresql.Query) connects to the target database and runs a self-join query against pg_locks and pg_stat_activity with fetchType: FETCH. It isolates granted locks that are blocking ungranted lock requests where wait duration exceeds inputs.min_wait_seconds.
  2. emit_lock_metrics (io.kestra.plugin.core.output.OutputValues) exports diagnostic variables (blocked_transactions_count, database_scanned, host_scanned, audited_at) into execution outputs.
  3. evaluate_lock_contention (io.kestra.plugin.core.flow.If) evaluates whether any blocking conflicts were found:
    • If true, notify_dba_slack (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) dispatches a formatted Slack notification detailing blocker PIDs, application names, client IPs, wait durations, and SQL snippets.
    • If false, log_healthy_locks (io.kestra.plugin.core.log.Log) logs an all-clear confirmation.
  4. alert_on_failure in the errors block dispatches a Slack alert if database connectivity fails.

What you get

  • Early detection of cascading lock bottlenecks before connection pools max out.
  • Identification of the root blocker PID, including its current transaction state (such as idle in transaction).
  • Copy-paste remediation SQL (SELECT pg_cancel_backend(...)) delivered directly to Slack.
  • Complete execution history and audit logs of all lock contention events.

Who it's for

  • Database Administrators (DBAs) safeguarding mission-critical PostgreSQL clusters.
  • Site Reliability Engineers (SREs) maintaining web service availability and connection pool health.
  • Backend engineers diagnosing intermittent database timeouts and query stalls.

Why orchestrate this with Kestra

Ad-hoc manual inspections in psql only happen after an incident is already underway, and shell scripts on cron lack execution lineage, centralized secrets, and reliable error alerts. Kestra provides native JDBC connectivity, secure credential handling, dual schedule and webhook triggers, and automated notification routing without external agents or daemons.

Prerequisites

  • Reachable PostgreSQL database (version 12+) with permissions to read pg_stat_activity and pg_locks.
  • Slack incoming webhook URL for alerting.

Secrets

  • POSTGRES_USER: Database username with read access to system catalogs.
  • POSTGRES_PASSWORD: Database password for authentication.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook destination URL.
  • WEBHOOK_KEY: Secret authentication key for the manual webhook trigger.

Quick start

  1. Configure POSTGRES_USER, POSTGRES_PASSWORD, SLACK_WEBHOOK_URL, and WEBHOOK_KEY in your Kestra namespace secrets.
  2. Set postgres_host, postgres_port, and database_name inputs to match your database environment.
  3. Trigger a manual execution in the Kestra UI to verify database connectivity.
  4. Enable the scheduled_monitor trigger to run automated checks every 10 minutes.

How to extend

  • Add an automated cancellation task for low-criticality batch workloads exceeding max lock limits.
  • Route critical alerts to PagerDuty or Opsgenie in addition to Slack.
  • Log recurring blocker queries to an internal audit table for schema and index optimization.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.