Queries icon
WorkingDirectory icon
Clone icon
DbtCLI icon
Query icon
If icon
SlackIncomingWebhook icon
Log icon
Schedule icon
Webhook icon

Zero-Downtime Blue-Green dbt Schema Swap on PostgreSQL

Run dbt transformations in staging, verify models, and atomically swap schemas on PostgreSQL with Kestra for zero-downtime, safe warehouse ELT.

Categories
DataInfrastructure

Running dbt transformations directly against production database schemas introduces substantial operational risk. If an upstream batch contains malformed records or a downstream model fails midway through dbt build, production marts are left in a half-mutated, corrupted state that immediately impacts live BI dashboards and user queries.

This blueprint orchestrates a zero-downtime blue-green schema deployment pattern for dbt Core on PostgreSQL. All models and tests are compiled and executed strictly inside an isolated staging schema. Once model materialization and assertions pass verification, Kestra executes an atomic schema swap in a single transaction block. If any step fails, the staging schema is dropped and live production tables remain completely untouched.

How it works

  1. prepare_staging_schema (io.kestra.plugin.jdbc.postgresql.Queries) drops any previous staging schema and creates a clean, isolated schema environment.
  2. run_dbt_staging (io.kestra.plugin.core.flow.WorkingDirectory) clones the Git repository and executes dbt deps and dbt build targeting the staging schema inside an isolated container.
  3. verify_staging_models (io.kestra.plugin.jdbc.postgresql.Query) queries PostgreSQL catalog metadata to verify that tables were successfully materialized in staging.
  4. evaluate_swap_gate (io.kestra.plugin.core.flow.If) checks that materialized tables exist and dry_run is false. When approved, atomic_schema_swap performs an instant transactional rename:
    • Renames current live schema to a backup schema for rollback safety.
    • Renames verified staging schema to live schema.
    • Dispatches an audit notification via notify_swap_success.
  5. The errors block intercepts any failure during staging execution, purges the incomplete staging schema via cleanup_staging_on_error, and notifies Slack that production was protected.

What you get

  • Zero downtime for downstream BI queries, Looker dashboards, and analytics consumers during long-running dbt builds.
  • Full immunity against partial model build failures corrupting production data marts.
  • Instant rollback capability with the previous live state retained in the backup schema.
  • Dry-run audit mode enabled by default to preview staging materialization without touching live tables.

Who it's for

  • Analytics Engineers running dbt Core on PostgreSQL, Amazon RDS, or Cloud SQL.
  • Data Platform Engineers designing reliable CI/CD and deployment gates for analytics.
  • Data Architects requiring zero-downtime guarantees for business-critical reporting.

Why orchestrate this with Kestra

Coordinating blue-green warehouse deployments across Git clones, containerized dbt CLI runs, multi-statement database DDL transactions, and conditional alerting requires brittle bash glue when built manually. Kestra provides declarative task sequencing, native container runners, transactional SQL execution, and dedicated failure recovery handlers in a single reproducible workflow.

Pitfalls

  • Active Lock Contention: ALTER SCHEMA ... RENAME requires an AccessExclusiveLock on the schema. If long-running analytical queries are executing in the live schema, the rename will queue behind them. Set statement_timeout or lock_timeout in PostgreSQL if strict cutover windows are required.
  • Cross-Schema References: Ensure dbt models reference other models via {{ ref(...) }} rather than hardcoding static database schema names, so models resolve dynamically to the staging target.

Prerequisites

  • PostgreSQL 12+ database accessible by the Kestra worker.
  • Database user with permissions to CREATE, DROP, and RENAME schemas.
  • Slack incoming webhook endpoint for promotion notifications.

Secrets

  • POSTGRES_PASSWORD: Password for the PostgreSQL connection user.
  • SLACK_WEBHOOK_URL: Webhook URL for dispatching deployment notifications.
  • DBT_WEBHOOK_KEY: Secret authentication key for external CI trigger dispatch.

Quick start

  1. Configure POSTGRES_PASSWORD and SLACK_WEBHOOK_URL in your Kestra namespace secrets.
  2. Adjust postgres_host, postgres_db, and schema names in the flow inputs.
  3. Execute the flow with dry_run: true to verify dbt model building in staging without mutating live tables.
  4. Run with dry_run: false to execute the full blue-green promotion.

How to extend

  • Add automated schema diff checks comparing row counts between backup and new live schemas.
  • Add automated rollback triggers that swap the backup schema back if downstream dashboard alerts fire.
  • Extend the dbt CLI command to run dbt snapshot or custom analytical seed steps before promotion.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.