Query icon
IonToCsv icon
If icon
Log icon
SlackIncomingWebhook icon
Set icon
Schedule icon

PostgreSQL Unused Index Bloat and Storage Reclamation Guard

Audit PostgreSQL unused indexes using pg_stat_user_indexes, export CSV reports, and safely reclaim storage in Kestra.

Categories
Data

Secondary indexes accelerate read queries but impose a hidden tax on database performance: every index must be updated on every INSERT, UPDATE, and DELETE, consuming buffer pool memory and NVMe disk space. Over months of application schema evolution, feature deprecations leave behind abandoned indexes that never receive scans. This flow audits PostgreSQL's pg_stat_user_indexes and pg_stat_database on a schedule, confirms statistics maturity, isolates un-scanned non-constraint indexes exceeding size thresholds, exports a downloadable CSV diagnostic report, persists historical metrics in Kestra's Key-Value store, and delivers actionable Slack storage advisories to database administrators.

How it works

  1. The Schedule trigger initiates the workflow weekly, or operators can trigger runs manually via the UI.
  2. verify_stats_maturity queries pg_stat_database to ensure stats_reset was at least min_stats_age_days ago, preventing false positives on freshly created or reset databases.
  3. evaluate_unused_indexes aggregates the count and cumulative disk storage in megabytes consumed by indexes with idx_scan = 0, explicitly excluding primary keys and unique constraints.
  4. fetch_unused_indexes_telemetry streams detailed metadata (schema, table, index name, sizes, definitions, and safe concurrent drop statements) into internal storage (STORE fetch type).
  5. export_diagnostic_csv transforms the raw query telemetry into a downloadable CSV artifact using IonToCsv.
  6. check_health_and_alert (If condition) verifies whether bloated unused indexes exist and statistics are mature.
  7. If storage opportunities exist, notify_slack_alert posts an advisory with wasted gigabytes and the direct CSV download link.
  8. update_audit_watermark updates the postgres_unused_index_audit_<database> key in Kestra's KV store for trend analysis.
  9. The errors block intercepts connection or query failures and immediately alerts the on-call channel.

What you get

  • Automated discovery of abandoned and zero-scan database indexes consuming expensive cloud storage.
  • Safe validation that preserves all primary keys, unique constraints, and system catalog objects.
  • False-positive prevention through configurable statistics maturity verification.
  • Automated export of candidate index definitions and generated DROP INDEX CONCURRENTLY commands into CSV.
  • Persistent state tracking via Kestra KV store without third-party monitoring infrastructure.
  • Automated incident alerts delivered directly to Slack with report links.

Who it's for

  • Database Administrators (DBAs) managing high-write PostgreSQL clusters.
  • Site Reliability Engineers (SREs) optimizing cloud infrastructure costs and disk capacity.
  • Backend Engineers seeking proactive database performance optimization.

Why orchestrate this with Kestra

Monitoring index usage directly through declarative Kestra workflows eliminates the need for proprietary database monitoring agents. Kestra natively handles database retries, credential isolation via secrets, conditional branching, artifact generation, and key-value state persistence in a single audited YAML pipeline.

Prerequisites

  • PostgreSQL 12 or newer.
  • Database user with SELECT permissions on pg_stat_user_indexes, pg_stat_database, pg_index, and pg_class.
  • Incoming Slack Webhook URL for alerting notifications.

Secrets

  • POSTGRES_JDBC_URL: JDBC connection string (e.g., jdbc:postgresql://postgres.company.internal:5432/postgres).
  • POSTGRES_USERNAME: Database username with read permissions.
  • POSTGRES_PASSWORD: Database user password.
  • SLACK_WEBHOOK_URL: Slack Incoming Webhook endpoint for alerting.

Quick start

  1. Configure the POSTGRES_JDBC_URL, POSTGRES_USERNAME, POSTGRES_PASSWORD, and SLACK_WEBHOOK_URL secrets in your Kestra namespace.
  2. Import this blueprint into your Kestra workspace.
  3. Click Execute with default inputs to perform an initial audit.
  4. Verify the generated output logs, download the generated CSV report from the Outputs tab, and inspect the stored record in the KV Store tab.
  5. Enable the weekly schedule trigger for continuous monitoring.

How to extend

  • Add an automated review-gated step to execute the generated DROP INDEX CONCURRENTLY statements during maintenance windows.
  • Archive weekly unused index CSV audit logs to Amazon S3 or Google Cloud Storage using io.kestra.plugin.aws.s3.Upload.
  • Connect to Jira or GitHub using Kestra plugins to automatically open tracking tickets for bloated indexes.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.