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

Database administration

Replicate PostgreSQL tables without primary keys

Replicate a PostgreSQL table that has no primary key by setting REPLICA IDENTITY FULL and pointing Artie's primary key override at a unique index.

Artie merges each change into the destination by the row’s key. For a table with no primary key, Artie uses the columns of a unique index that you select.

Prerequisites

  • A PostgreSQL table with no primary key
  • A unique index that identifies each row, or unique columns to build one on
  • Permission to alter the source table’s replica identity
  • An Artie pipeline that replicates the table

Artie merges updates and deletes in the destination on the columns you select. Prefer columns whose values never change: Artie replicates a changed key as a delete followed by an insert.

Configure the table

The SQL examples use the table public.orders and the column external_id. Replace them with your own table and columns.

  1. Create or identify a unique index

    The columns you select in Artie must exactly match the columns of one unique index on the source table. If the table has no unique index, create one.

    To turn an existing column into a unique constraint, first check for duplicates. Run this query on the source database. It must return no rows:

    SQL
    -- Find duplicate values before adding the constraint.
    SELECT external_id, count(*) AS duplicate_count
    FROM public.orders
    WHERE external_id IS NOT NULL
    GROUP BY external_id
    HAVING count(*) > 1;

    Then build the index and add the constraint. Run these statements outside a transaction block:

    SQL
    -- Build the backing unique index without blocking normal writes.
    CREATE UNIQUE INDEX CONCURRENTLY orders_external_id_key
    ON public.orders (external_id);
    
    -- Fail quickly instead of waiting on a conflicting lock.
    SET lock_timeout = '5s';
    
    -- Convert the index into an actual unique constraint.
    ALTER TABLE public.orders
    ADD CONSTRAINT orders_external_id_unique
    UNIQUE USING INDEX orders_external_id_key;

    The index build does not block inserts, updates, or deletes. The final ALTER TABLE is brief and fails after five seconds rather than waiting on a conflicting lock.

    For a composite identifier, create one unique index containing all of the columns you select in Artie.

  2. Set the table replica identity to FULL

    Run this statement as the table owner, a role that inherits the owning role’s privileges, or a superuser. On Amazon RDS, Aurora and Cloud SQL, the admin user isn’t a superuser, so it can run this only if it owns the table or inherits the owning role’s privileges. The statement briefly locks the table:

    SQL
    ALTER TABLE public.orders REPLICA IDENTITY FULL;

    REPLICA IDENTITY FULL makes PostgreSQL include the full previous row in update and delete events. Artie needs those old values to key updates and deletes on the override columns.

    Confirm the setting with this query:

    SQL
    SELECT relreplident FROM pg_class WHERE oid = 'public.orders'::regclass;

    The query returns f.

  3. Add the table and set the primary key override in Artie

    The Advanced settings icon appears only on rows for tables that the pipeline replicates, so add the table first.

    1. In the Artie dashboard, open your pipeline and select Edit pipeline.
    2. Select the Tables tab.
    3. On the table’s row, select the Advanced settings icon.
    4. Under Column configuration, select Override primary keys.
    5. Select every column in the unique index. For the example index, select external_id.
    6. Select Close.

    Keep the selected columns included in replication: excluded columns can’t be selected. Artie uses the selected columns as the table’s merge key in the destination, and creates the destination table without a primary key constraint.

    Pipeline setup calls this setting Merge key. If the Choose tables step shows 1 table needs a merge key, select Set merge key and choose the same columns.

  4. Save and deploy

    Select Save changes, then select Deploy. The override applies after the pipeline deploys.

    If the selected columns don’t match a unique index, the pipeline fails with an error that starts with this text:

    Text
    failed to find unique index for primary keys: no unique index found for column(s): (external_id)

Verify replication

Insert, update, and delete a row in the source table. Confirm that each change is reflected in the destination.

When to add a database primary key instead

Use a real PostgreSQL primary key when you control the schema and can add one safely. A primary key is the simplest long-term choice. Use the override workflow when you cannot change the table’s primary-key constraint but it already has a stable unique identifier.

Next steps

Was this page helpful?