Files
windmill/backend/migrations/20260909052532_add_missing_primary_keys.up.sql
Ruben FiszelandClaude Opus 5 e62bfdcd8c fix: give every table a primary key so the db can be logically replicated (#11036)
* fix: give every table a primary key so the db can be logically replicated

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017WS9iNfYQiLJNuBnxysBzi

* fix: tighten replicability guard and trim migration comments

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017WS9iNfYQiLJNuBnxysBzi

* fix: split deployment_metadata into its own primary-key migration

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017WS9iNfYQiLJNuBnxysBzi

* docs: correct the partial-index predicate note after the deployment_metadata split

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017WS9iNfYQiLJNuBnxysBzi

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-10 18:42:24 +00:00

32 lines
1.6 KiB
SQL

-- These five had neither a PRIMARY KEY nor an explicit REPLICA IDENTITY, which makes
-- PostgreSQL reject UPDATE and DELETE on them under logical replication.
-- `deployment_metadata` and `metrics` are the other two, one migration each after this.
--
-- The surrogate cannot be swapped for a natural key: every unique index on all five is
-- PARTIAL, split on `script_hash IS NULL`, and a partial index cannot back a primary
-- key. The partial uniques stay; they are what the ON CONFLICT clauses infer.
--
-- Each ALTER rewrites its table under ACCESS EXCLUSIVE and holds it unavailable for
-- the rewrite. These five share a transaction because each is bounded by what a
-- workspace holds rather than by how long it has run, so none can grow into the one
-- that locks the rest; a transaction holds all its locks until it commits.
--
-- An instance that cannot afford that lock at startup can set REPLICA IDENTITY FULL
-- on these tables instead, which unblocks replication by itself, and run these
-- idempotent ALTERs in a maintenance window first.
ALTER TABLE workspace_runnable_dependencies
ADD COLUMN IF NOT EXISTS id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY;
ALTER TABLE dbt_node
ADD COLUMN IF NOT EXISTS id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY;
ALTER TABLE dbt_edge
ADD COLUMN IF NOT EXISTS id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY;
ALTER TABLE dbt_column_edge
ADD COLUMN IF NOT EXISTS id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY;
ALTER TABLE dbt_graph_snapshot
ADD COLUMN IF NOT EXISTS id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY;