Skip to contentManaged Kafka is live: Apache Kafka on AWS for half the cost of MSK

Database administration

Change a PostgreSQL table's primary key

Change the primary key of a replicated PostgreSQL table without a backfill, and avoid long locks on large tables.

You can change the primary key of a replicated table without a backfill or a write pause:

  • Artie reads the new key from PostgreSQL and merges later changes into the destination on it. Rows already in the destination match because they hold the new key’s values.
  • Before buffering the first change with the new key, Artie flushes the changes it holds under the old key.
  • Artie doesn’t read the destination’s primary key constraint, so replication keeps working before you update it.

Before you start

  • The new key must be unique, never NULL and included in replication.
  • Its values must already be in the destination. If you just added or filled in a key column, wait for those changes to replicate.
  • A primary key override takes precedence over the new key. Update or clear it.
  • Dynamic tables keep the key they were created with. Contact Artie first.

Change the key

The examples change public.orders from id to (tenant_id, id).

  1. Check the new key for duplicates

    This query must return no rows:

    SQL
    SELECT tenant_id, id, count(*)
    FROM public.orders
    GROUP BY tenant_id, id
    HAVING count(*) > 1;
  2. Make the new key columns NOT NULL

    On PostgreSQL 12 and later, validate a CHECK constraint first, so SET NOT NULL skips its full-table scan while writes are blocked:

    SQL
    ALTER TABLE public.orders
    ADD CONSTRAINT orders_tenant_id_not_null CHECK (tenant_id IS NOT NULL) NOT VALID;
    ALTER TABLE public.orders VALIDATE CONSTRAINT orders_tenant_id_not_null;
    ALTER TABLE public.orders ALTER COLUMN tenant_id SET NOT NULL;
    ALTER TABLE public.orders DROP CONSTRAINT orders_tenant_id_not_null;
  3. Build the new unique index

    Run this outside a transaction. It doesn’t block writes:

    SQL
    CREATE UNIQUE INDEX CONCURRENTLY orders_tenant_id_id_key
    ON public.orders (tenant_id, id);
  4. Swap the primary key in one transaction

    A published table without a primary key rejects updates and deletes, so don’t split these statements:

    SQL
    BEGIN;
    SET LOCAL lock_timeout = '5s';
    ALTER TABLE public.orders DROP CONSTRAINT orders_pkey;
    ALTER TABLE public.orders
    ADD CONSTRAINT orders_pkey PRIMARY KEY USING INDEX orders_tenant_id_id_key;
    COMMIT;

    In the same transaction, recreate any foreign keys that reference the old key. If the table uses REPLICA IDENTITY USING INDEX, point it at the new index too.

  5. Update the primary key in Snowflake

    Artie created the destination table with the old primary key, and Snowflake doesn’t update it for you. Replace it so tools that read the table’s constraints, such as BI tools, catalogs and dbt, see the new key:

    SQL
    ALTER TABLE orders DROP PRIMARY KEY;
    ALTER TABLE orders ADD PRIMARY KEY (tenant_id, id);

    Snowflake doesn’t enforce primary keys, so this changes only the table’s metadata.

Verify the destination

Run the duplicate check from step 1 on the destination table, then compare row counts with the source. If either is off, backfill the table.

Was this page helpful?