Schedule icon
Query icon
If icon
Fail icon
Loop icon
SlackIncomingWebhook icon
Pause icon
CopyOut icon
FileCompress icon
Upload icon
List icon
Set icon
Log icon

PostgreSQL Monthly Partition Roller with Verified S3 Archive

Create future monthly PostgreSQL partitions, then seal, COPY to S3, verify and drop expired ones after approval, with a KV archive index.

Categories
Data

A monthly RANGE partitioned table needs two jobs every month. Someone must create next month before rows arrive. Someone must also remove the month that left the retention window, without loss of data. This flow does both jobs on one table. It removes old data with DETACH and DROP, not with DELETE, so the table gets no bloat and no long VACUUM.

How it works

  1. A nightly Schedule starts the run. concurrency.limit: 1 stops two runs on the same table.
  2. inspect_parent reads the partition key. The run fails when the table is not RANGE partitioned on one date or timestamp column.
  3. plan_months finds each month from now to months_ahead that no partition overlaps. A Loop creates them one at a time with lock_timeout.
  4. plan_retention lists attached partitions older than retention_months, oldest first, up to max_partitions_per_run.
  5. When require_approval is true, Slack gets the list and a Pause waits for approved=true.
  6. A second Loop handles each expired partition. It adds a CHECK (false) NOT VALID seal, exports with COPY to gzip CSV and uploads to S3 with row count metadata. It reads the object size back from S3.
  7. One transaction checks that the partition is still attached and sealed and that the live row count equals the exported count. Then it detaches and drops the partition.
  8. A KV entry records the S3 object for each dropped partition. verify_layout reads the tree again and fails the run when a month is uncovered or a planned partition still exists.

What you get

  • Future months exist before rows arrive.
  • No expired partition is dropped before S3 holds a verified copy with the same row count.
  • A failed check leaves the partition attached and sealed. The next run repeats it.
  • A KV index from partition name to S3 object for restores.
  • Slack messages for approval, changes and failures.

Who it's for

  • Database administrators and platform teams that own large time series or event tables in PostgreSQL.

Why orchestrate this with Kestra

  • Retries, timeouts, the approval Pause, KV state and the execution history replace a cron script and a runbook.

Prerequisites

  • PostgreSQL 13 or later. The role must own the parent table.
  • An S3 bucket and an IAM user with s3:PutObject and s3:ListBucket.

Secrets

  • POSTGRES_USERNAME, POSTGRES_PASSWORD: credentials of the table owner role.
  • AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY: S3 write and list access.
  • SLACK_WEBHOOK_URL: incoming webhook for approval and alerts.

Quick start

  1. Add the secrets to the namespace.
  2. Set jdbc_url, table_schema, parent_table and s3_bucket.
  3. Execute once and approve. Check the new partitions and the S3 objects.

How to extend

  • Set endpointOverride, forcePathStyle and compatibilityMode on the two S3 tasks to use MinIO or another S3 API.
  • Add a Subflow per table to roll many tables.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.