> ## Documentation Index
> Fetch the complete documentation index at: https://www.artie.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 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](/docs/pipelines/tables#override-primary-keys) takes precedence over the new key. Update or clear it.
* [Dynamic tables](/docs/guides/artie/dynamic-tables) keep the key they were created with. Contact Artie first.

> **Warning:**
>
> Tables with [multi-step merge](/docs/pipelines/tables#enable-multi-step-merge) keep their key until the merge cycle completes. Pause writes from just before the swap until then. Otherwise a merge can fail with `Duplicate row detected during DML action` or miss a delete.

## Change the key

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

**Step 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;
```

**Step 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;
```

**Step 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);
```

**Step 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.

**Step 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](/docs/pipelines/backfill).