id: postgres-expand-contract-column-migration
namespace: company.team
description: |
Change a PostgreSQL column without downtime, using expand and contract. Expand adds the new column and a trigger
that keeps it in sync with every write. A batched backfill fills the existing rows and saves a checkpoint in the
KV store after every batch, so a killed run resumes where it stopped. Every row is then verified, and the old
column is dropped only after a person approves. Demo mode migrates `demo_orders.amount` (dollars, numeric) to
`amount_cents` (bigint).
triggers:
- id: from_ci
type: io.kestra.plugin.core.trigger.Webhook
description: Let a deploy pipeline start the migration once the app version that
writes both columns is live.
key: "{{ secret('EXPAND_CONTRACT_WEBHOOK_KEY') }}"
concurrency:
limit: 1
behavior: QUEUE
inputs:
- id: action
type: SELECT
values: [ migrate, rollback ]
defaults: migrate
description: migrate runs expand, backfill, verify, approval and contract, and
is safe to re-run. rollback removes the trigger and the new column and
keeps the old one.
- id: schema
type: STRING
defaults: public
validator: ^[a-z_][a-z0-9_]*$
- id: table
type: STRING
defaults: demo_orders
validator: ^[a-z_][a-z0-9_]*$
- id: key_column
type: STRING
defaults: id
validator: ^[a-z_][a-z0-9_]*$
description: An integer primary key. Batches walk it in order, and the
checkpoint is the last key done.
- id: old_column
type: STRING
defaults: amount
validator: ^[a-z_][a-z0-9_]*$
- id: new_column
type: STRING
defaults: amount_cents
validator: ^[a-z_][a-z0-9_]*$
- id: new_type
type: STRING
defaults: bigint
description: SQL type of the new column, e.g. bigint, text, timestamptz.
- id: transform
type: STRING
defaults: CAST(round(amount * 100) AS bigint)
description: SQL expression that computes the new value from the row's columns.
The trigger, the backfill and the verification all use it.
- id: batch_size
type: INT
defaults: 5000
min: 1
- id: batch_interval
type: DURATION
defaults: PT0.2S
description: Pause between batches, to leave room for production writes and replication.
- id: lock_timeout
type: STRING
defaults: 5s
description: DDL waits at most this long for its lock instead of queueing behind
a long transaction and blocking every query behind it. It tries 5 times,
10 s apart.
- id: approval_timeout
type: DURATION
defaults: PT24H
description: No decision within this time keeps the expanded state (nothing is dropped).
- id: seed_demo
type: BOOL
defaults: true
description: Create demo_orders with 100,000 rows if it does not exist. Switch
off for your own tables.
variables:
kv_key: "expand_contract_{{ inputs.schema }}_{{ inputs.table }}_{{
inputs.new_column }}"
target: "\"{{ inputs.schema }}\".\"{{ inputs.table }}\""
cfg: "{{ {'schema': inputs.schema, 'table': inputs.table, 'key':
inputs.key_column, 'old': inputs.old_column, 'new': inputs.new_column,
'new_type': inputs.new_type, 'transform': inputs.transform} | toJson }}"
approval_window: "{{ inputs.approval_timeout >= 3600 ? ((inputs.approval_timeout
/ 3600) | numberFormat('0.#')) ~ ' h' : ((inputs.approval_timeout / 60) |
numberFormat('0.#')) ~ ' min' }}"
mismatch: "\"{{ inputs.new_column }}\" IS DISTINCT FROM ({{ inputs.transform }})"
tasks:
- id: seed
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.seed_demo and inputs.action == 'migrate' }}"
then:
- id: create_demo_table
type: io.kestra.plugin.jdbc.postgresql.Queries
url: "{{ secret('POSTGRES_URL') }}"
username: "{{ secret('POSTGRES_USER') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
sql: |
CREATE TABLE IF NOT EXISTS public.demo_orders (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, customer text NOT NULL, amount numeric(10,2), created_at timestamptz NOT NULL DEFAULT now());
INSERT INTO public.demo_orders (customer, amount)
SELECT 'customer-' || (g % 997), round(CAST(random() * 500 AS numeric), 2)
FROM generate_series(1, 100000) AS g
WHERE NOT EXISTS (SELECT 1 FROM public.demo_orders);
- id: action_switch
type: io.kestra.plugin.core.flow.Switch
value: "{{ inputs.action }}"
cases:
migrate:
- id: expand
type: io.kestra.plugin.jdbc.postgresql.Queries
description: Idempotent. Adds the column and (re)creates the sync trigger. Only
a lock timeout is retried; any other error fails at once.
url: "{{ secret('POSTGRES_URL') }}"
username: "{{ secret('POSTGRES_USER') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
sql: |
SELECT set_config('lock_timeout', '{{ inputs.lock_timeout }}', false), set_config('expand_contract.cfg', $cfg${{ render(vars.cfg) }}$cfg$, false);
DO $do$
DECLARE
c json := CAST(current_setting('expand_contract.cfg') AS json);
t regclass := to_regclass(format('%I.%I', c->>'schema', c->>'table'));
key_type text;
attempt int;
fn text := (c->>'table') || '_' || (c->>'new') || '_sync';
BEGIN
IF t IS NULL THEN RAISE EXCEPTION 'table %.% does not exist', c->>'schema', c->>'table'; END IF;
SELECT format_type(atttypid, NULL) INTO key_type FROM pg_attribute WHERE attrelid = t AND attname = c->>'key' AND NOT attisdropped;
IF key_type IS NULL OR key_type NOT IN ('smallint', 'integer', 'bigint') THEN
RAISE EXCEPTION 'key column % must be an integer column (found %)', c->>'key', coalesce(key_type, 'no such column');
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid = t AND attname = c->>'old' AND NOT attisdropped) THEN
RAISE EXCEPTION 'old column % does not exist on % (already contracted?)', c->>'old', t;
END IF;
-- A transform that does not parse or cast would break every write once it is in the trigger, so try it on 1000 rows first.
EXECUTE format('SELECT count(CAST((%s) AS %s)) FROM (SELECT * FROM %s LIMIT 1000) AS sample', c->>'transform', c->>'new_type', t);
FOR attempt IN 1..5 LOOP
BEGIN
EXECUTE format('ALTER TABLE %s ADD COLUMN IF NOT EXISTS %I %s', t, c->>'new', c->>'new_type');
EXECUTE format($f$
CREATE OR REPLACE FUNCTION %I.%I() RETURNS trigger LANGUAGE plpgsql AS $body$
BEGIN
IF TG_OP = 'INSERT' THEN
IF NEW.%I IS NULL THEN SELECT (%s) INTO NEW.%I FROM (SELECT NEW.*) AS src; END IF;
ELSIF NEW.%I IS DISTINCT FROM OLD.%I THEN
SELECT (%s) INTO NEW.%I FROM (SELECT NEW.*) AS src;
END IF;
RETURN NEW;
END $body$
$f$, c->>'schema', fn, c->>'new', c->>'transform', c->>'new', c->>'old', c->>'old', c->>'transform', c->>'new');
EXECUTE format('DROP TRIGGER IF EXISTS %I ON %s', fn, t);
EXECUTE format('CREATE TRIGGER %I BEFORE INSERT OR UPDATE ON %s FOR EACH ROW EXECUTE FUNCTION %I.%I()', fn, t, c->>'schema', fn);
EXIT;
EXCEPTION WHEN lock_not_available THEN
IF attempt = 5 THEN RAISE EXCEPTION 'no lock on % after 5 tries of lock_timeout %; a long transaction holds it (see pg_stat_activity)', t, current_setting('lock_timeout'); END IF;
RAISE WARNING 'lock_timeout on %, try % of 5, waiting 10 s', t, attempt;
PERFORM pg_sleep(10);
END;
END LOOP;
END $do$;
- id: start_point
type: io.kestra.plugin.core.output.OutputValues
description: Where this run starts. Anything above 0 means an earlier run was
stopped and this one resumes.
values:
resume_from: "{{ kv(render(vars.kv_key), errorOnMissing=false) ?? 0 }}"
- id: backfill
type: io.kestra.plugin.core.flow.LoopUntil
description: One batch per iteration, until a batch comes back short. The
checkpoint survives a killed run.
condition: "{{ outputs.batch.row.batch_rows < inputs.batch_size }}"
failOnMaxReached: true
checkFrequency:
interval: "{{ inputs.batch_interval }}"
maxDuration: PT12H
tasks:
- id: batch
type: io.kestra.plugin.jdbc.postgresql.Query
url: "{{ secret('POSTGRES_URL') }}"
username: "{{ secret('POSTGRES_USER') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
fetchType: FETCH_ONE
sql: |
WITH b AS (
SELECT "{{ inputs.key_column }}" AS k FROM {{ render(vars.target) }}
WHERE "{{ inputs.key_column }}" > {{ kv(render(vars.kv_key), errorOnMissing=false) ?? 0 }}
ORDER BY "{{ inputs.key_column }}" LIMIT {{ inputs.batch_size }}
), u AS (
UPDATE {{ render(vars.target) }} SET "{{ inputs.new_column }}" = ({{ inputs.transform }})
WHERE "{{ inputs.key_column }}" IN (SELECT k FROM b)
RETURNING 1
)
SELECT (SELECT count(*) FROM b) AS batch_rows, (SELECT count(*) FROM u) AS updated,
COALESCE((SELECT max(k) FROM b), {{ kv(render(vars.kv_key), errorOnMissing=false) ?? 0 }}) AS last_key
- id: checkpoint
type: io.kestra.plugin.core.kv.Set
key: "{{ render(vars.kv_key) }}"
value: "{{ outputs.batch.row.last_key }}"
kvType: NUMBER
- id: verify
type: io.kestra.plugin.jdbc.postgresql.Query
description: Every row, old against new, through the same transform the trigger
uses.
url: "{{ secret('POSTGRES_URL') }}"
username: "{{ secret('POSTGRES_USER') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
fetchType: FETCH_ONE
sql: |
SELECT count(*) AS total_rows,
count(*) FILTER (WHERE {{ render(vars.mismatch) }}) AS mismatched,
(SELECT string_agg(CAST(k AS text), ', ') FROM (
SELECT "{{ inputs.key_column }}" AS k FROM {{ render(vars.target) }} WHERE {{ render(vars.mismatch) }}
ORDER BY 1 LIMIT 5) AS s) AS sample_keys
FROM {{ render(vars.target) }}
- id: verification_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.verify.row.mismatched > 0 }}"
then:
- id: verification_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "MIGRATION BLOCKED: {{ outputs.verify.row.mismatched }} of {{
outputs.verify.row.total_rows }} rows have {{ inputs.new_column
}} different from {{ inputs.transform }} (keys {{
outputs.verify.row.sample_keys }}). Nothing was dropped; fix the
rows or the transform and re-run."
- id: request_approval
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
Column migration {{ inputs.schema }}.{{ inputs.table }}: {{ inputs.old_column }} -> {{ inputs.new_column }} is backfilled and verified ({{ outputs.verify.row.total_rows }} rows, 0 mismatches; this run started at {{ inputs.key_column }} > {{ outputs.start_point.values.resume_from }} and ran {{ outputs.backfill.iterationCount }} batches).
Contract drops the trigger and the column {{ inputs.old_column }}. Approve only once every app reads {{ inputs.new_column }}: resume execution {{ execution.id }} in Kestra within {{ render(vars.approval_window) }}.
- id: approve_contract
type: io.kestra.plugin.core.flow.Pause
description: The old column is dropped only on an explicit yes. A timeout keeps
the expanded state.
pauseDuration: "{{ inputs.approval_timeout }}"
behavior: RESUME
onResume:
- id: drop_old_column
type: BOOL
displayName: Drop the old column now?
defaults: false
- id: note
type: STRING
required: false
- id: decision
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.approve_contract.onResume.drop_old_column ?? false }}"
then:
- id: reverify
type: io.kestra.plugin.jdbc.postgresql.Query
description: Rows changed while the run waited; check again right before
dropping anything.
url: "{{ secret('POSTGRES_URL') }}"
username: "{{ secret('POSTGRES_USER') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
fetchType: FETCH_ONE
sql: SELECT count(*) AS total_rows, count(*) FILTER (WHERE {{
render(vars.mismatch) }}) AS mismatched FROM {{
render(vars.target) }}
- id: reverification_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ outputs.reverify.row.mismatched > 0 }}"
then:
- id: reverification_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "MIGRATION BLOCKED: while the run waited for approval, {{
outputs.reverify.row.mismatched }} of {{
outputs.reverify.row.total_rows }} rows stopped matching.
Nothing was dropped; re-run migrate."
- id: contract
type: io.kestra.plugin.jdbc.postgresql.Queries
url: "{{ secret('POSTGRES_URL') }}"
username: "{{ secret('POSTGRES_USER') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
sql: |
SELECT set_config('lock_timeout', '{{ inputs.lock_timeout }}', false), set_config('expand_contract.cfg', $cfg${{ render(vars.cfg) }}$cfg$, false);
DO $do$
DECLARE
c json := CAST(current_setting('expand_contract.cfg') AS json);
t regclass := to_regclass(format('%I.%I', c->>'schema', c->>'table'));
fn text := (c->>'table') || '_' || (c->>'new') || '_sync';
attempt int;
BEGIN
FOR attempt IN 1..5 LOOP
BEGIN
EXECUTE format('DROP TRIGGER IF EXISTS %I ON %s', fn, t);
EXECUTE format('DROP FUNCTION IF EXISTS %I.%I()', c->>'schema', fn);
EXECUTE format('ALTER TABLE %s DROP COLUMN %I', t, c->>'old');
EXIT;
EXCEPTION WHEN lock_not_available THEN
IF attempt = 5 THEN RAISE EXCEPTION 'no lock on % after 5 tries of lock_timeout %; a long transaction holds it (see pg_stat_activity)', t, current_setting('lock_timeout'); END IF;
RAISE WARNING 'lock_timeout on %, try % of 5, waiting 10 s', t, attempt;
PERFORM pg_sleep(10);
END;
END LOOP;
END $do$;
- id: forget_checkpoint
type: io.kestra.plugin.core.kv.Delete
key: "{{ render(vars.kv_key) }}"
errorOnMissing: false
- id: report_done
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: "Column migration {{ inputs.schema }}.{{ inputs.table }} DONE: {{
inputs.old_column }} dropped, {{ inputs.new_column }} verified
on {{ outputs.reverify.row.total_rows }} rows. Approved{% if
(outputs.approve_contract.onResume.note ?? '') != '' %}: {{
outputs.approve_contract.onResume.note }}{% endif %}."
else:
- id: report_kept
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: "Column migration {{ inputs.schema }}.{{ inputs.table }} paused in
the expanded state: {{ inputs.new_column }} is backfilled and
kept in sync by the trigger, {{ inputs.old_column }} was not
dropped ({% if outputs.approve_contract.onResume.drop_old_column
is defined %}declined{% else %}no decision within {{
render(vars.approval_window) }}{% endif %}{% if
(outputs.approve_contract.onResume.note ?? '') != '' %}: {{
outputs.approve_contract.onResume.note }}{% endif %}). Re-run
migrate to contract later, or rollback."
rollback:
- id: undo_expand
type: io.kestra.plugin.jdbc.postgresql.Queries
url: "{{ secret('POSTGRES_URL') }}"
username: "{{ secret('POSTGRES_USER') }}"
password: "{{ secret('POSTGRES_PASSWORD') }}"
sql: |
SELECT set_config('lock_timeout', '{{ inputs.lock_timeout }}', false), set_config('expand_contract.cfg', $cfg${{ render(vars.cfg) }}$cfg$, false);
DO $do$
DECLARE
c json := CAST(current_setting('expand_contract.cfg') AS json);
t regclass := to_regclass(format('%I.%I', c->>'schema', c->>'table'));
fn text := (c->>'table') || '_' || (c->>'new') || '_sync';
attempt int;
BEGIN
IF t IS NULL THEN RAISE EXCEPTION 'table %.% does not exist', c->>'schema', c->>'table'; END IF;
IF NOT EXISTS (SELECT 1 FROM pg_attribute WHERE attrelid = t AND attname = c->>'old' AND NOT attisdropped) THEN
RAISE EXCEPTION 'refusing to roll back: old column % is already dropped, % holds the only copy of the data', c->>'old', c->>'new';
END IF;
FOR attempt IN 1..5 LOOP
BEGIN
EXECUTE format('DROP TRIGGER IF EXISTS %I ON %s', fn, t);
EXECUTE format('DROP FUNCTION IF EXISTS %I.%I()', c->>'schema', fn);
EXECUTE format('ALTER TABLE %s DROP COLUMN IF EXISTS %I', t, c->>'new');
EXIT;
EXCEPTION WHEN lock_not_available THEN
IF attempt = 5 THEN RAISE EXCEPTION 'no lock on % after 5 tries of lock_timeout %; a long transaction holds it (see pg_stat_activity)', t, current_setting('lock_timeout'); END IF;
RAISE WARNING 'lock_timeout on %, try % of 5, waiting 10 s', t, attempt;
PERFORM pg_sleep(10);
END;
END LOOP;
END $do$;
- id: forget_checkpoint_after_rollback
type: io.kestra.plugin.core.kv.Delete
key: "{{ render(vars.kv_key) }}"
errorOnMissing: false
- id: report_rollback
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: "Column migration {{ inputs.schema }}.{{ inputs.table }} ROLLED
BACK: trigger and {{ inputs.new_column }} removed, {{
inputs.old_column }} untouched, checkpoint cleared."
errors:
- id: alert_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
messageText: |
Column migration {{ inputs.schema }}.{{ inputs.table }} ({{ inputs.action }}) FAILED in execution {{ execution.id }}{% if errorLogs() | length > 0 %} at `{{ errorLogs()[0]['taskId'] }}`: {{ errorLogs() | jq('([.[] | select(.message | test("MIGRATION BLOCKED"))] + [.[] | select(.message | test("ERROR:"))] + .)[0].message | sub("^.*?ERROR: "; "") | split("\n")[0]') | first }}{% endif %}
{% if inputs.action == 'migrate' %}{% if kv(render(vars.kv_key), errorOnMissing=false) is not null %}Backfill checkpoint: {{ inputs.key_column }} {{ kv(render(vars.kv_key), errorOnMissing=false) }}; a re-run resumes from there. {% endif %}The old column {{ inputs.old_column }} is only dropped after an approval.{% endif %}
outputs:
- id: verification
type: JSON
value: "{{ outputs.verify.row ?? {} }}"