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

PostgreSQL Dead Tuple Bloat and Autovacuum Sentinel

Monitor Postgres dead tuples and autovacuum lag with Kestra. Query pg_stat_user_tables, identify table bloat, and alert DBAs via Slack.

Categories
DataInfrastructure

In PostgreSQL, updates and deletes generate dead tuples that consume disk space and degrade sequential scans until autovacuum reclaims them. Under heavy write workloads, long-running transactions, or conservative autovacuum configurations, dead tuples accumulate rapidly—bloating tables and indexes, exhausting disk capacity, and polluting the database buffer cache.

This blueprint queries PostgreSQL's native pg_stat_user_tables catalog to detect tables where dead tuple counts and ratios breach operational thresholds. It exports structured metrics into execution outputs, evaluates severity, and alerts DBAs and on-call engineers on Slack with targeted remediation guidance.

How it works

  1. inspect_table_bloat (io.kestra.plugin.jdbc.postgresql.Query) connects to the target PostgreSQL database and inspects pg_stat_user_tables with fetchType: FETCH. It filters for tables where dead tuple counts and bloat ratios exceed inputs.dead_tuple_threshold and inputs.dead_ratio_threshold_percent.
  2. emit_audit_metrics (io.kestra.plugin.core.output.OutputValues) exports diagnostic variables (bloated_table_count, database_scanned, host_scanned, scanned_at) into execution outputs for auditability.
  3. evaluate_bloat_severity (io.kestra.plugin.core.flow.If) checks whether any bloated tables were found:
    • If true, notify_dba_slack (io.kestra.plugin.slack.notifications.SlackIncomingWebhook) dispatches a formatted Slack notification listing the offending tables, dead tuple counts, bloat percentages, and last autovacuum timestamps.
    • If false, log_healthy_status (io.kestra.plugin.core.log.Log) logs that all user tables are within acceptable thresholds.
  4. alert_on_failure in the errors block dispatches a Slack alert if database connection fails or credentials expire.

What you get

  • Automated, non-invasive health auditing of all user tables without third-party APM agents.
  • Early warning before dead tuples cause disk exhaustion, query latency spikes, or transaction ID wraparound risk.
  • Direct visibility into autovacuum lag and tables requiring manual vacuuming or parameter tuning.
  • Centralized execution history and audit logs across all database clusters.

Who it's for

  • Database Administrators (DBAs) maintaining production PostgreSQL instances.
  • Site Reliability Engineers (SREs) monitoring infrastructure health and database storage headroom.
  • Data Platform Engineers running high-velocity ETL pipelines with frequent table mutations.

Why orchestrate this with Kestra

Monitoring PostgreSQL health through ad-hoc bash scripts or unmonitored crontabs is fragile: crons lack execution logs, failure retries, and centralized secret management. Kestra provides native JDBC integration with declarative configuration, built-in credential isolation via secrets, dual schedule and webhook triggers, and automated notification routing when thresholds are crossed.

Prerequisites

  • Reachable PostgreSQL database (version 12+) with user table statistics enabled.
  • Slack incoming webhook URL for alerting.

Secrets

  • POSTGRES_USER: Database username with read access to pg_stat_user_tables.
  • 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. Add POSTGRES_USER, POSTGRES_PASSWORD, SLACK_WEBHOOK_URL, and WEBHOOK_KEY to your Kestra namespace secrets.
  2. Adjust postgres_host, postgres_port, and database_name inputs to match your database environment.
  3. Run a manual execution via the Kestra UI to verify database connectivity and baseline statistics.
  4. Enable the scheduled_sentinel trigger to run audits automatically during off-peak hours.

How to extend

  • Add an automated VACUUM ANALYZE task for low-risk tables during scheduled maintenance windows.
  • Route critical alerts to PagerDuty or Opsgenie in addition to Slack.
  • Store historical bloat trends in an audit table for long-term capacity planning.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.