Query icon
If icon
Query icon
Loop icon
Get icon
Request icon
OutputValues icon
Queries icon
Parallel icon
SlackIncomingWebhook icon
Set icon
Log icon
Schedule icon

PostgreSQL Slow Query AI Index Healer with DuckDB and HypoPG

Detect slow PostgreSQL queries, rank severity in DuckDB, verify non-locking AI index DDL with EXPLAIN, and dispatch GitHub issues and Slack alerts.

Categories
AIData

What you get

  • Automated extraction of slow SELECT queries from PostgreSQL pg_stat_statements filtered by execution latency and call volume.
  • In-memory composite bottleneck scoring via DuckDB combining cumulative database time, shared buffer cache-miss ratios, and latency jitter.
  • Guardrailed LLM synthesis that enforces non-locking CREATE INDEX CONCURRENTLY IF NOT EXISTS and DROP INDEX CONCURRENTLY statements.
  • Virtual execution plan verification using PostgreSQL EXPLAIN (GENERIC_PLAN) and hypopg, plus 7-day alert deduplication via Kestra KV Store.

Who it's for

  • Database Reliability Engineers (DBREs), Backend Engineers, and Platform Teams operating production PostgreSQL databases who want proactive, non-locking index recommendations before slow queries degrade application SLAs.

Why orchestrate this with Kestra

Kestra unifies JDBC database introspection, embedded DuckDB analytical scoring, LLM API calls, conditional safety gates, parallel webhook/GitHub fan-out, and stateful KV deduplication inside a single declarative YAML workflow with built-in retries and execution lineage.

How it works

  1. hourly_schedule (io.kestra.plugin.core.trigger.Schedule) triggers the pipeline every hour.
  2. extract_slow_queries (io.kestra.plugin.jdbc.postgresql.Query) queries pg_stat_statements for slow SELECT queries exceeding min_mean_exec_time_ms and min_calls.
  3. check_slow_queries_exist (io.kestra.plugin.core.flow.If) branches only when bottlenecks are detected, invoking fetch_index_catalog to retrieve existing pg_indexes definitions.
  4. rank_bottlenecks (io.kestra.plugin.jdbc.duckdb.Query) ingests query metrics into DuckDB and computes a composite severity_score to isolate the top bottlenecks.
  5. heal_each_bottleneck (io.kestra.plugin.core.flow.Loop) iterates over each bottleneck, checking check_already_tracked (io.kestra.plugin.core.kv.Get) to skip queries already reported within the last 7 days.
  6. ai_synthesis (io.kestra.plugin.core.http.Request) sends query telemetry and existing indexes to an OpenAI-compatible LLM endpoint, and parse_ai_response enforces CONCURRENTLY DDL safety guardrails.
  7. verify_with_explain and score_verification test the proposed index virtually via hypopg and compare EXPLAIN (GENERIC_PLAN) planner costs in DuckDB.
  8. fan_out_remediation (io.kestra.plugin.core.flow.Parallel) opens a detailed GitHub issue and posts a Slack notification simultaneously, followed by remember_query (io.kestra.plugin.core.kv.Set) setting a P7D deduplication key.

Prerequisites

  • PostgreSQL 16+ instance with pg_stat_statements enabled (shared_preload_libraries=pg_stat_statements) and optional hypopg extension installed.
  • Outbound HTTPS access to an OpenAI-compatible LLM endpoint (e.g., Groq), GitHub REST API, and Slack Incoming Webhooks.

Secrets

  • POSTGRES_PASSWORD: Password for the PostgreSQL monitoring role.
  • LLM_API_KEY: API key for the OpenAI-compatible LLM provider (e.g., Groq API key).
  • GITHUB_TOKEN: GitHub Personal Access Token with issues:write permission on the target repository.
  • SLACK_WEBHOOK_URL: Incoming Webhook URL for Slack notifications.

Quick start

  1. Configure POSTGRES_PASSWORD, LLM_API_KEY, GITHUB_TOKEN, and SLACK_WEBHOOK_URL as namespace secrets in Kestra.
  2. Import this blueprint into the company.data namespace and update pg_url and github_repository inputs.
  3. Click Execute to run an immediate diagnostic pass, verify the generated GitHub issue and Slack alert, and keep hourly_schedule enabled.

How to extend

  • Add a GitOps pull request task using io.kestra.plugin.git.PushFlows or GitHub API to automatically commit verified .sql migration files into a Flyway or Liquibase directory.
  • Route NOT_CONFIRMED virtual index plans to PagerDuty or Jira for manual DBA inspection.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.