New to Kestra?
Use blueprints to kickstart your first workflows.
Automated database reliability workflow to monitor PostgreSQL connection pool limits, idle transactions, deadlocks, and alert Slack.
Diagram unavailable
We could not build the topology for this blueprint. The flow itself is valid, use the YAML on the left to run it.
In PostgreSQL production environments, connection pool exhaustion and lingering idle-in-transaction states represent catastrophic failure vectors. When application workloads leak connections or experience sudden traffic spikes, active sessions quickly consume the server's max_connections allocation. Once saturated, PostgreSQL rejects incoming requests with FATAL: remaining connection slots are reserved for non-replication superuser connections, abruptly taking dependent services offline.
Furthermore, connections left in idle in transaction state prevent the autovacuum daemon from cleaning dead row versions (tuples). This results in severe table bloat, disk exhaustion, and degraded query cache hit ratios.
This blueprint establishes a continuous reliability watchdog for PostgreSQL. Executing on a 5-minute schedule or on-demand, it inspects pg_stat_activity and pg_stat_database to evaluate pool utilization percentages, flags aged idle transactions, records cumulative deadlocks, and delivers actionable Slack alerts before connection starvation causes production outages.
scheduled_health_check trigger (io.kestra.plugin.core.trigger.Schedule) executes every 5 minutes or on-demand via the Kestra UI.audit_postgres_health task (io.kestra.plugin.jdbc.postgres.Query) queries pg_stat_activity and pg_settings using Common Table Expressions (CTEs) to calculate connection utilization percentage against max_connections and identify transactions left idle for longer than max_idle_transaction_age_seconds.evaluate_health_thresholds flowable task (io.kestra.plugin.core.flow.If) evaluates whether the computed health_status is non-healthy (CONNECTION_POOL_SATURATION or IDLE_IN_TRANSACTION_STARVATION).notify_dba_slack (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) dispatches a diagnostic card detailing connection counts, aged idle transaction duration, and remediation commands.log_healthy_status records current pool capacity in execution logs.export_audit_manifest task records telemetry metrics for reliability reporting and observability platforms.max_connections and rejects application queries.idle in transaction sessions that cause table bloat and block autovacuum.Monitoring PostgreSQL connection pool health usually requires complex external metric agents or custom cron daemons. Kestra provides declarative, serverless orchestration: it handles secure JDBC connectivity, schedules regular health checks, branches conditionally based on utilization thresholds, and alerts the engineering team directly in Slack.
| Name | Type | Default | Description |
|---|---|---|---|
postgres_url |
STRING | jdbc:postgresql://localhost:5432/postgres |
JDBC connection string targeting the target PostgreSQL database. |
max_connection_utilization_percent |
FLOAT | 80.0 |
Threshold percentage of max_connections beyond which an alert is triggered. |
max_idle_transaction_age_seconds |
INT | 120 |
Duration in seconds an idle transaction can remain open before triggering an alert. |
slack_channel |
STRING | #dba-alerts |
Slack channel destination for database performance alerts. |
{{ outputs.audit_postgres_health.rows[0].connection_utilization_percent }}: Current percentage of allowed connections in use.{{ outputs.audit_postgres_health.rows[0].idle_in_transaction_connections }}: Count of active sessions currently holding an uncommitted transaction in idle state.{{ outputs.audit_postgres_health.rows[0].aged_idle_in_transaction_connections }}: Count of idle transactions older than the configured threshold.{{ outputs.audit_postgres_health.rows[0].health_status }}: Computed operational status (HEALTHY, CONNECTION_POOL_SATURATION, or IDLE_IN_TRANSACTION_STARVATION).{{ outputs.export_audit_manifest.value }}: Structured JSON telemetry manifest recording execution timestamp and pool metrics.pg_read_all_stats role or superuser privileges to inspect all client connections in pg_stat_activity.POSTGRES_USERNAME: Database username authorized to inspect server activity views.POSTGRES_PASSWORD: Password for the PostgreSQL monitoring user.SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint URL used for sending alert notifications.POSTGRES_USERNAME, POSTGRES_PASSWORD, and SLACK_WEBHOOK_URL in your Kestra namespace secrets.pg_read_all_stats predefined role. Grant GRANT pg_read_all_stats TO <monitoring_user>; for full visibility.max_idle_transaction_age_seconds to reflect your workload characteristics.deadlocks column in pg_stat_database is cumulative since the last stats reset. Track the change over time rather than absolute historical counts.io.kestra.plugin.notifications.pagerduty.PagerDutyAlert inside the then: block for critical saturation escalations.SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE ....pg_replication_slots into the diagnostic query.