Schedule icon
Query icon
OutputValues icon
Log icon
If icon
SlackIncomingWebhook icon
Pause icon
Loop icon
Fail icon

PostgreSQL Missing Partition Provision Guard

Detect RANGE partitioned PostgreSQL tables missing future partitions, create them behind an approval gate, then re-read the partition tree to confirm.

Categories
DataInfrastructure

A RANGE partitioned table splits its rows across child tables by a time range. Someone has to create next week's partitions before next week arrives. When nobody does, every new row either falls into the DEFAULT partition, which then grows without bound and cannot be split later without an exclusive lock or, with no DEFAULT, the insert fails with "no partition of relation found for row". Either way it is a day-2 outage waiting on the calendar. This guard reads the live partition tree, works out which of the next N windows have no covering partition, creates the missing ones under a gate and proves they landed.

This blueprint was created by zkasuran.

Safety

  • Read-only by default. dry_run is true and auto_remediate is false, so a plain run reports and never creates a partition.
  • A create happens only when auto_remediate is true and dry_run is false. It then waits for a human to resume the Pause with approved set true. An unanswered gate reads as not approved.
  • Creating a partition is additive. It adds an empty child table and changes no existing row. It is idempotent. A window that already has a covering partition is skipped and no create ever overlaps an existing partition.
  • A DEFAULT partition is respected. The guard provisions forward windows so new rows stop landing in DEFAULT. It never detaches or splits a DEFAULT. When the DEFAULT already holds rows for a window being created, PostgreSQL rejects that one create, the re-read reports the window still uncovered and the run fails rather than reporting a false success.
  • The re-read of the partition tree is the real gate, so a create the connecting role is not allowed to run surfaces as a failed run.
  • Scope is RANGE partitioning on a single date or timestamp key, the common time-based case. LIST and HASH partitioning, multi-column range keys and non-temporal range keys are ignored.

How it works

  1. detect (io.kestra.plugin.jdbc.postgresql.Query) reads pg_partitioned_table for RANGE parents keyed on one date or timestamp column, enumerates each parent's children through pg_inherits, parses the FROM and TO bounds from pg_get_expr, builds the next windows_ahead windows from date_trunc(window_size, now()) and returns one row per window no partition covers. Each row carries the deterministic child name and the exact CREATE TABLE PARTITION OF statement, built with format('%I') and format('%L') so identifiers and bounds are quoted by the database.
  2. summarize (io.kestra.plugin.core.output.OutputValues) builds the human readable target list and the JSON the loop iterates, so the log, the alert and the loop read the same set.
  3. log_detect (io.kestra.plugin.core.log.Log) logs the count, the targets and the mode.
  4. guard (io.kestra.plugin.core.flow.If) branches on the count. With none found, clean logs and the run ends. Otherwise findings_alert posts the finding on every detection.
  5. mode (io.kestra.plugin.core.flow.If) enters the remediation branch only when auto_remediate is true and dry_run is false. Otherwise report_only lists what would be created and stops.
  6. approval_gate (io.kestra.plugin.core.flow.Pause) holds the run until a human resumes it. create_partitions (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) then runs one CREATE TABLE PARTITION OF per target.
  7. reverify re-runs the detection and verify_result fails the run through io.kestra.plugin.core.execution.Fail if any targeted window is still uncovered. A window that rolled in during the run is not a target, so it does not fail the run.

What you get

  • A check that reads the real partition tree, so it catches the tables that are about to overflow their last bound.
  • A deterministic child name per window (<parent>_YYYYMMDD) and a CREATE built inside PostgreSQL, correct for date, timestamp and timestamptz keys.
  • Idempotent provisioning. Run it as often as you like. It only creates what is genuinely missing and never overlaps an existing partition.
  • A real verification step. The run passes only when the second read of the partition tree shows every target covered.
  • Slack alerts on the findings and on any failure.

Who it's for

  • DBAs and platform engineers who run time-partitioned tables and need next week's partitions in place before the inserts arrive.
  • Teams who adopted native partitioning without pg_partman or a cron job and meet the gap the hard way.
  • Teams evaluating the PostgreSQL plugin who want an example that reads the catalog and drives gated, verified DDL.

Why orchestrate this with Kestra

PostgreSQL has no scheduler, no dry run, no approval step and no way to prove a partition was created. pg_partman solves the scheduling with an extension you have to install and trust. A hand-rolled cron creates blindly and says nothing when a create is rejected. Kestra adds the missing pieces. It schedules the sweep, computes the exact missing windows from the live partition tree, gates the change behind dry-run and approval, builds each CREATE inside the database so identifiers and bounds are correct, re-reads the tree to turn a hopeful create into a verified one and posts a Slack alert the moment a step fails.

Tested end to end

Run on Kestra 2.0.5 against a PostgreSQL 16 server, with a local HTTP sink standing in for Slack. One schema held four tables: events_default partitioned by day with a DEFAULT partition and days behind through tomorrow provisioned, events_strict partitioned by day with no DEFAULT and only today and tomorrow provisioned, metrics_full partitioned by day and already provisioned for the next seven days, plus a plain non-partitioned table. A LIST partitioned table was also present. Every count was confirmed with psql against pg_partition_tree.

  • Detector can fail. At windows_ahead 7 the detector returned exactly 10 uncovered windows (five on each of the two under-provisioned tables), never metrics_full and never the non-partitioned or LIST table. At windows_ahead 1 it returned 0, because today is already covered everywhere.
  • Dry run detected the 10, alerted and left every table at its original child count.
  • Remediate with approval created all 10 and the re-read reported 0 of them still uncovered. events_default went from 4 children to 9 and events_strict from 2 to 7, metrics_full stayed at 7 and the LIST table was untouched. An insert for a previously uncovered day then landed in its own new child rather than in DEFAULT or an error, confirmed with the row's tableoid.
  • A second dry run against the now provisioned tables returned 0 and took the clean branch, so the guard is idempotent.
  • A DEFAULT partition holding a row for one target window blocked that single create. The other 9 were created, the re-read reported that one window still uncovered and the run failed and alerted, which is the honest outcome because splitting a DEFAULT is out of scope.
  • An unreachable host failed the first query and fired the Slack alert.

Prerequisites

A PostgreSQL server reachable over JDBC with one or more RANGE partitioned tables keyed on a date or timestamp column. The connecting user must be able to read the catalog and to create partitions on the parent tables.

Secrets

  • POSTGRES_USERNAME: PostgreSQL user, read when use_secret_auth is true.
  • POSTGRES_PASSWORD: password for that user.
  • SLACK_WEBHOOK_URL: used by findings_alert and the errors block.
  • In Kestra OSS, provide each secret as a base64-encoded environment variable prefixed SECRET_.

Inputs

Name Type Default Description
postgres_url STRING jdbc:postgresql://localhost:5432/postgres PostgreSQL JDBC URL for the target database.
use_secret_auth BOOL true Read credentials from secrets. Set false only for inline test credentials.
schema STRING "" Optional single schema filter. Empty scans every user schema.
window_size SELECT day Partition window size: day, week or month. Match how the tables are partitioned.
windows_ahead INT 7 How many future windows to keep provisioned, counted from the current window.
dry_run BOOL true Report without creating anything.
auto_remediate BOOL false Create behind the approval gate when dry_run is false.

Quick start

  1. Set POSTGRES_USERNAME, POSTGRES_PASSWORD and SLACK_WEBHOOK_URL as namespace secrets.
  2. Import this flow.
  3. Run it with postgres_url pointed at a database that has RANGE partitioned tables. The default dry run reports the missing windows.
  4. To provision them, run again with dry_run false and auto_remediate true, then resume the paused execution with approved true.

How to extend

  • Set window_size to week or month to match a weekly or monthly partitioned table.
  • Raise windows_ahead to keep a longer runway of future partitions ready.
  • Set schema to restrict the sweep to one schema on a large database.
  • Fail the run as soon as detection finds anything, for a read-only drift check with no remediation.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.