Queries icon
AIAgent icon
GoogleGemini icon
If icon
SlackIncomingWebhook icon
Log icon
Schedule icon
Webhook icon

Autonomous Database Incident Investigation Agent with DuckDB and AI

Investigate database connection spikes and lock contention with DuckDB diagnostics and Kestra AI agent to synthesize copy-paste remediation commands.

Categories
AIInfrastructure

Production database incidents (connection pool exhaustion, lock tree contention, hung transaction spikes) frequently trigger high-severity alerts outside standard business hours. When an incident occurs, on-call Site Reliability Engineers (SREs) are awakened and lose critical minutes manually running routine diagnostic queries (pg_stat_activity, lock dependency trees, active transactions) to identify the offending session.

This blueprint implements an autonomous Tier-1 AI SRE investigation agent. When a connection pool or latency alert fires, the workflow ingests process session telemetry and runs relational lock tree diagnostics using in-memory DuckDB. Next, Kestra's AI plugin evaluates the lock graph against production database failure archetypes with strict JSON Schema constraints. It isolates the exact root-cause blocker PID and offending query, checks whether downstream checkout queries are hung, and delivers a copy-paste remediation SQL command (pg_terminate_backend) with architectural prevention guidance directly to Slack.

How it works

  1. collect_diagnostic_telemetry (io.kestra.plugin.jdbc.duckdb.Queries): Ingests the active database session snapshot and computes lock dependency trees, isolating the longest-running unblocked root session.
  2. investigate_incident (io.kestra.plugin.ai.agent.AIAgent): Evaluates session telemetry and alert symptoms using Google Gemini (gemini-2.5-flash) with structured JSON Schema enforcement (CRITICAL_LOCK_CONTENTION, LONG_RUNNING_IDLE_TRANSACTION, HIGH_VOLUME_TRAFFIC_SPIKE), isolating the culprit PID and drafting the remediation SQL command.
  3. evaluate_severity_gate (io.kestra.plugin.core.flow.If):
    • If severity is CRITICAL: Posts an actionable investigation summary card to #sre-incidents in Slack containing the culprit PID, offending SQL, and copy-paste kill command.
    • Otherwise: Logs a nominal diagnostic report without generating unnecessary alert channel fatigue.
  4. alert_investigation_failure (errors block): Catches downstream connectivity failures or unhandled exceptions and notifies the on-call channel.

What you get

  • Sub-second automated root-cause diagnosis for database lock contention and connection spikes.
  • Zero-setup demo execution out of the box via built-in diagnostic lock tree telemetry.
  • Deterministic relational dependency analysis combining DuckDB with foundational AI models.
  • Actionable copy-paste remediation commands ready for on-call engineers.
  • Centralized pluginDefaults keeping Slack webhook URLs out of individual task declarations.

Who it's for

  • Site Reliability Engineers, Database Administrators, and DevOps Platform teams automating Tier-1 incident response and database health governance.

Why orchestrate this with Kestra

Scripting database incident triage with ad-hoc cron jobs or standalone Python scripts creates brittle infrastructure that lacks centralized secret management, observability, and structured AI response guarantees. Orchestrating with Kestra provides declarative scheduling, authenticated webhooks for APM alarms (Datadog, Prometheus), in-memory DuckDB analytical parsing, structured JSON Schema model reasoning, and an immutable execution audit trail in a single version-controlled YAML workflow.

Pitfalls

  • Terminating Critical Background Workers: When executing remediation commands (pg_terminate_backend), ensure the target PID is a client worker session rather than an internal engine worker (such as the autovacuum launcher or logical replication worker), which can trigger crash recovery.
  • Exclusive DDL Table Locks in Migrations: Schema migrations that execute unindexed DDL (ALTER TABLE ... ADD COLUMN) hold an AccessExclusiveLock, blocking all read and write traffic. Production migrations should enforce strict session lock timeouts (SET lock_timeout = '3s';).
  • Reconnection Storms Following Backend Cancellation: Abruptly terminating high numbers of pooled client sessions can induce a thundering herd reconnection storm against database connection proxies (such as PgBouncer).
  • Security Exposure in SQL Snippet Notifications: Alert notifications sent to team channels should ensure raw SQL strings do not expose sensitive customer credentials or unmasked personal identifiers.

Prerequisites

  • A Google Gemini API key with access to gemini-2.5-flash.
  • A Slack incoming webhook URL for on-call SRE notifications.

Secrets

  • GEMINI_API_KEY: API key for Gemini foundational model execution.
  • SLACK_WEBHOOK_URL: Slack webhook endpoint for alert delivery.
  • WEBHOOK_KEY: Authentication secret for event-driven webhook execution.

Quick start

  1. Execute the flow with default inputs to inspect the automated diagnosis of the sample DDL lock contention incident.
  2. Add GEMINI_API_KEY and SLACK_WEBHOOK_URL to your Kestra namespace secrets.
  3. Configure your monitoring alert (Datadog, Grafana, CloudWatch) to post incident webhooks to alert_webhook_trigger.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.