Skip to content

Keys, RRN/ROWID and indexes

Every captured update and delete is applied to exactly one destination row, found by its key. This page explains where that key comes from, what the Replicator Service does when a table has none, which keys and indexes it creates at the destination, and how to keep your own indexes without breaking replication.

Why keys matter

For an incremental subscription, the key is used at every step after the baseline:

  • Applying updates and deletes. With Batch and Sequence, each change becomes an UPDATE ... WHERE <key> or DELETE ... WHERE <key> that must touch exactly one row, otherwise the subscription is flagged (see How Batch works). With Merge, the MERGE matches the merge table to the destination table on the key columns and keeps only the last change of each key (see The MERGE statement).
  • Catching up after a baseline. Changes made while the baseline was copying may already be in the copy. The Replicator Service applies them again without duplicates: it deletes the row by its key before inserting it, or, when the destination table has a unique key, turns an insert of an existing row into an update.
  • Soft Delete and LOB values: a soft delete updates the row with that key (see Soft Delete), and for sources other than SQL Server, LOB values are read from the source table by key.
  • Resuming a baseline. An interrupted baseline resumes from its last commit only when the rows can be read in key order; otherwise it starts over (see Checkpoint/Restart).

A snapshot subscription needs a key only for resuming an interrupted run.

How the key is chosen

The Capture Agent decides which key columns it stages with every change; the Replicator Service decides which of them it uses to find the destination row.

At the source: the Capture Agent

When a subscription is registered, the Capture Agent looks up the source table's primary key or, without one, the columns of its unique indexes, and stages their values (before the change) with every change. For Db2 for i, Oracle and Db2 for LUW sources, it also stages the row's identifier, whatever the key: the relative record number (RRN), the ROWID or the RID.

Source Key the Capture Agent reports Notes
Db2 for i Primary key (including the unique keys of DDS-described physical files), else unique indexes SQ_IXLIST replaces the primary key definition for a table. The RRN is always staged.
Oracle Primary key, else unique indexes The ROWID is always staged.
Db2 for LUW Primary key, else unique indexes The RID is staged by Stelo Capture 7.0 and later.
SQL Server Primary key, else unique indexes SQL Server allows Change Tracking only on tables with a primary key, so with Change Tracking every table needs one.
MySQL, PostgreSQL, Informix Primary key, else unique indexes, as reported by the database driver

Two Capture Agent properties change this (see Properties by source database):

Property What it does
alwaysUseRowId Db2 for i and Oracle: the agent reports no primary key or unique index for any table, so every incremental subscription is keyed by the RRN or ROWID. On Db2 for i, it also ignores SQ_IXLIST.
requireUniqueIndex A table without a primary key or unique index can't be registered: the agent rejects it with "Table/File cannot be replicated, has no unique index".

In the Replicator Service

When you create an incremental subscription, the Replicator Service records the same source key with it (marked in the Primary Key column of the Columns tab). When the group starts applying changes to the member, it picks the replication key:

  1. The RRN or ROWID, when the subscription has the RRN/ROWID column (see below). It is then the only key Apply searches by: primary key and unique index columns are ignored.
  2. Otherwise, the staged key columns of the source's primary key recorded with the subscription, or, without one, of its unique indexes.
  3. A derived column marked as a partition key is added, so that each subscription writing to a shared table only touches its own rows.

Ticking Primary Key on a source column doesn't change the replication key or create a destination primary key (on a derived column, the same tick makes a Partition Key). To see the key actually used, look at the baseline's messages (see Check what SQDR created).

The RRN or ROWID column

For Db2 for i, Oracle and Db2 for LUW sources, the Replicator Service can add the source's row identifier (the RRN, ROWID or RID) as a destination column and use it as the replication key. It's added:

  • automatically, for an incremental subscription whose source table has no primary key or unique index;
  • when Force hidden column is ticked on the Columns tab;
  • for new members of a group with Force hidden column: the group's setting applies when the column list is built, in the wizard or with Rebuild Column List. Existing members aren't changed.

On Db2 for LUW, an incremental subscription is keyed by the RID only when the group's Capture Agent is Stelo Capture 7.0 or later. With an earlier agent, a table without a key is replicated as described in Tables without a usable key, and a subscription with the row identifier column can't be saved.

The column is an ordinary, named derived column with the expression @RRN, which the Replicator Service resolves for the subscription's own table each time it prepares a statement (for example RRN(<schema>.<table>) on Db2 for i). So a subscription exported from a test server and imported into production, or copied to another schema, keeps working. Existing subscriptions are converted when the service starts.

  • Row identifier use. In the column's properties, Index means Apply finds rows by the row identifier, which is indexed at the destination, while the destination primary key is the source's (else its unique index with the fewest columns, else none). Primary key also makes the row identifier the destination primary key. An incremental subscription with Allow UPSERT and no primary key or unique index at the source gets Primary key. Changing the use doesn't change an existing table's primary key: it applies when the table is created again.
  • Stable key. A subscription keyed by its row identifier always has a stable key.
  • Members that archive or only stream changes don't search the destination, so the row identifier stays an ordinary column for them, and a save warns about it.
  • Snapshot subscriptions can also have a row identifier column from Informix (ROWID) and PostgreSQL (ctid) sources. Index adds a non-unique index on it, Primary key makes it the primary key. Incremental subscriptions use the row identifier only on Db2 for i, Oracle and Db2 for LUW.

Use the RRN or ROWID when the table has no key, when its key values don't stay unique at the destination (see below), or when a Db2 for i table is journaled with after images only (see Before and after images).

Keep in mind:

  • A Db2 for i reorganization needs a new baseline. When a table is reorganized (the journal records a member reorganization, for example after RGZPFM), its rows can get new record numbers. Members keyed by the RRN are then marked as needing a baseline ("A reorganization event was detected for a subscription using a hidden column(i.e. RRN)"); other members ignore it. Schedule reorganizations of replicated tables, and let Automatic Snapshot or a scheduled group run take the baseline.
  • So does a Db2 for LUW REORG. A REORG TABLE gives rows new RID values. The Capture Agent finds a classic (offline) REORG in the log, and an INPLACE one in the database's recovery history, which it checks every 5 minutes (the agent property udbReorgPollInterval, in seconds; 0 turns the check off). Members keyed by the RID are then marked as needing a baseline, with the same message. Schedule reorganizations as on Db2 for i.
  • So does a Db2 for i ALTER. It copies the table without its deleted rows, which renumbers them too. After a reorganization or an ALTER, members keyed by the RRN need a baseline and apply no more changes until it runs; members that hold the RRN only in an ordinary column also need a baseline, but keep applying changes (they find their rows by the source key). Members that archive or only stream changes aren't affected.
  • Name and type. The column is named RRN or ROWID by default (the rrnName and rowidName service properties); ROWID is reserved in Oracle, so rename it for Oracle destinations. See RRN and ROWID columns and Errors caused by reserved column names on the destination (ROWID).

Tables without a usable key

On Db2 for i and Oracle, an incremental subscription always has a key: the RRN or ROWID is added when the table has none. So it is on Db2 for LUW with a Stelo Capture 7.0 or later Capture Agent (the RID). For other sources, a table without a primary key or unique index can still be replicated incrementally, with these limits:

  • Batch and Sequence apply inserts only. An update or delete can't be applied without a key, so the Replicator Service marks the member as needing a baseline instead ("SQDRSVC(Apply) determined a snapshot was required for source table ... Likely an update was attempted for a table lacking a suitable index."), and so does an insert that arrives while the member catches up after its baseline. With Automatic Snapshot on, every update or delete at the source then costs a full copy.
  • The baseline says so. Its messages end with "Table lacks unique index. Incremental replication restricted to insert only", and no index is created at the destination.
  • Db2 for LUW sources with an earlier Capture Agent: the agent itself turns an update of such a table, and a delete (unless the member uses Ignore Deletes), into a "baseline required" signal.
  • Merge needs a key. A member without one in a Merge group is applied row by row, as in a Batch group: inserts are applied, and an update or delete marks it as needing a baseline; the other members are merged as usual. Destinations with a Java apply connector (except Kafka) refuse a subscription without a key when it's saved.
  • With requireUniqueIndex, the Capture Agent doesn't accept such tables at all (see above).

Your options, from best to least:

  1. Add a primary key or unique index at the source, then re-create the subscription or run Rebuild Column List and a baseline.
  2. Db2 for i: define a key in SQ_IXLIST when a set of columns is unique in practice. A wrong definition causes errors when changes are applied.
  3. Db2 for i, Oracle and Db2 for LUW: use the row identifier (automatic for tables without a key; on Db2 for LUW, with Stelo Capture 7.0 or later).
  4. Replicate the table with a scheduled snapshot subscription instead.

The key at the destination

What the Replicator Service creates

At the end of each baseline of an incremental member, before it starts applying changes, the Replicator Service prepares an index on the replication key, depending on the member's apply option and Destination DDL Options:

Member What is created on the destination table
Strict Apply Rules, table created by the Replicator Service A non-unique index on the key columns, named after the table with the suffix IR (for example ORDERSIR). It's dropped before each baseline and created again afterwards.
Allow UPSERT, table created by the Replicator Service The same IR index; then, once the changes captured during the baseline are applied, a primary key (else a unique index), and the IR index is dropped when it has the same columns (except on Db2 for LUW and Oracle). If the key can't be created (for example duplicate values), an event reports "Unable to create unique constraint(s) on target for target table ...". The primary key is dropped before each baseline.
Archive all DML Activity No index: the table holds several rows per key.
Use existing table for baseline Nothing. The baseline's messages show the CREATE INDEX statement to run yourself ("Use existing specified - verify that a suitable index exists over destination columns").
Create if does not exist, else use existing The IR index, if the table doesn't have it yet.
Destinations without indexes (Snowflake, Databricks, Google BigQuery, Amazon Redshift, Exasol, text files) No index.
  • The primary key of an Allow UPSERT member is created on the source's primary key columns when the subscription recorded one, otherwise on the replication key columns. For SQL Server sources that use Change Tracking (or with the assumeStableKey service property) it's created right after the baseline. If the destination table already has a primary key or unique index after the baseline (for example one replicated with Replicate Primary Keys), no key is added.
  • If the index can't be created, the baseline ends with an error showing the statement and the database's message. Fix the cause (for example space or permissions) and run the baseline again.
  • Unique indexes of the source aren't replicated for incremental subscriptions: the baseline skips them ("Unique Index skipped"), and they can't be ticked on the Indexes tab. The apply options manage the destination's unique keys.
  • On SQL Server, indexes are created CLUSTERED only with the useCluster service property.

Stable key

Stable key says that a subscription's key values never change at the source: rows are inserted and deleted, but no update changes the key columns. Only you can tell, from what you know about the application; SQDR can't tell from the table definition. With a stable key:

  • updates leave the key columns (and partition key columns) out of their SET lists;
  • the destination primary key is built right after the table is prepared, before the data is loaded, and the load is ordered by it (a sliced baseline still builds its keys afterwards);
  • an update or delete is searched by whichever image of the key the Capture Agent staged, which helps for Db2 for i tables journaled with after images only.

If a key does change at the source, the destination row keeps its old key, and later changes to that row don't find it. Don't tick Stable key unless you're sure.

Where the Capture Agent stages the key's before image, Apply notices such a change: it flags the member and reports an error event that names the subscription, the destination table and the old and new key ("... has Stable key set, but the source changed the key of a row"), once per change set. Turn Stable key off for that subscription: no baseline is needed for later changes, but the rows already applied with it may need one to be corrected. Without a before image (for example a Db2 for i table journaled with after images only, or SQL Server Change Tracking), a key change can't be noticed.

Set it on the subscription, or for new subscriptions with the group's Stable key default (both kinds of group). A subscription keyed by its row identifier always has a stable key. The assumeStableKey service property sets the default of new groups, and of new subscriptions that aren't in a group.

Keep destination keys the same as the replication key

Every unique key at the destination must accept whatever the replication key accepts:

  • Don't add your own primary key or unique index on other columns. It can reject an insert that the replication key allows. Allow UPSERT doesn't help: its retry looks the row up by the replication key and updates nothing, so the member is flagged; with Automatic Snapshot, the baseline then empties the table and fails on the same duplicate, again and again. See Give every table a reliable key.
  • Values that differ at the source must stay different at the destination. A case-insensitive collation (First and first) or a timestamp with less precision (Db2 for i microseconds to a SQL Server type with milliseconds) makes two source keys equal at the destination, and changes hit the wrong row. Use a case-sensitive collation for SQL Server destinations, a destination type with the same precision, or, for Db2 for i, Oracle and Db2 for LUW, the row identifier.
  • Members keyed by the RRN or ROWID of a table that has a primary key: the destination primary key (from Allow UPSERT or Replicate Primary Keys) is on the source's key columns, not the RRN. Check that those columns are unique in the data, or use Strict Apply Rules.

Indexes

Replicating the source's indexes and keys

A baseline (or a snapshot run) can create the source's indexes, primary key and foreign keys on the destination table:

  • Which ones. Those ticked in Replicate on the subscription's Indexes tab. In a group run, the group's Replicate indexes, Replicate Primary Keys and Replicate Foreign Keys add all of that kind for every member.
  • When. They're dropped before the data is copied and created after it, which makes the copy faster. A subscription run on its own drops foreign keys, primary key and indexes, copies the data, then creates indexes, primary key and foreign keys. A group run drops all members' foreign keys first and creates them last, after every member is copied, so a foreign key never refers to a table that isn't loaded yet.
  • Not with Manual synchronization, which doesn't copy data or replicate indexes.
  • Not at destinations without indexes (non-unique indexes of Snowflake, Databricks, BigQuery and similar destinations can't be ticked).
  • Source changes. Indexes are read from the source when the subscription is created or when you click Refresh on the Indexes tab; a changed source index reaches the destination with the next baseline.

Disabling indexes during a baseline

With Allow disable/enable indexes on the group (see Group properties), a group run disables every non-unique index of the destination table before the copy and rebuilds it afterwards, including your own. It works for SQL Server and Oracle destinations, not for members that re-create the table on each run (Create every time baseline is Run). A subscription run on its own doesn't disable indexes. A clustered index is left alone. For SQL Server, rebuild options can be set with the msSQLIndexRebuildOptions service property.

Keeping your own destination indexes

You can add non-unique indexes to destination tables, for example for reporting. They stay as long as the table isn't dropped:

  • Create every time subscription is Saved drops the table, with your indexes, at each save; Create every time baseline is Run at every baseline. With Use existing table for baseline, the table is never dropped, and baselines only empty it (Truncate or Delete); create the index on the replication key yourself too (see above).
  • To disable and rebuild your indexes around each baseline of a SQL Server or Oracle table, use Allow disable/enable indexes (see Disabling indexes during a baseline).
  • Don't add unique indexes or primary keys on other columns than the replication key (see above). With Allow UPSERT, the destination table's primary key is dropped before each baseline anyway.

Merge groups

A Merge group joins each member's merge table to the destination table on the key columns, so keep the index the Replicator Service creates on them: without it, every MERGE scans the destination table. The merge table itself has no index. For MySQL destinations, which have no MERGE, the statements depend on whether the destination table has a primary key or unique index on the key columns. See How Merge works.

Check what SQDR created

  • Baseline messages (the member's replication events): "Index ... created over (...)", "Primary Key ... created over (...)", "Unique Index skipped", "Table lacks unique index. Incremental replication restricted to insert only", or the CREATE INDEX statement to run yourself for an existing table.
  • Service log (see Logs): when the Replicator Service creates a destination primary key or unique index for an incremental member, it logs the columns with the replication key, for example "Created destination PRIMARY KEY ... over (...) on ... (replication key: RRN)". Columns that differ from the replication key are the mismatch described above.

Duplicate keys and duplicate rows

Most duplicate key errors while applying changes, and most duplicate rows at the destination, come from a key that doesn't identify one row in the same way at the source and the destination. A duplicate key error stops the group's apply, unless Allow UPSERT turns the insert into an update (see Polling errors); an update or delete that finds no row, or several, flags the member (see Flagged members).

Situation What happens What to do
Db2 for i table keyed by the RRN, with REUSEDLT(*YES) The file reuses deleted rows' record numbers, so a new row can get an old row's RRN. If the destination still holds the old row (for example its delete wasn't applied), the insert collides with it. Prefer the table's primary key or a unique key to the RRN. Without one, run a new baseline when the destination is out of step.
Composite key with a column that can be NULL, in a Merge group The MERGE compares each key column with =, and NULL never equals NULL, so a change to a row with a NULL in its key never matches the destination row: it's inserted again. Key the subscription on columns that can't be NULL (for example the source's primary key alone), then run a new baseline.
Key columns created as nullable at the destination The destination can't get a primary key on them, and NULL key values behave as in the row above. Set forceNonNullPK so that key columns are created NOT NULL, then Rebuild Column List and run a new baseline.
Table without a primary key or unique index (Db2 for i, Oracle, or Db2 for LUW with Stelo Capture 7.0 or later) whose member asks for a baseline after every change ("Baseline snapshot is required, according to Capture Agent") The subscription was created without the RRN or ROWID column, so there's no key to apply updates and deletes with (see Tables without a usable key). Turn on Force hidden column in the group (see The RRN or ROWID column); it doesn't change existing members, so create the subscription again (or run Rebuild Column List on it) to get the RRN or ROWID column, then run a new baseline, which creates the index. The same applies to Db2 for i tables journaled with after images only.
Destination table you created yourself (Use existing table for baseline) No index is created on the replication key, and your primary key may include columns the source doesn't have or keep unique. Create the index on the replication key that the baseline's messages show, and no unique key on other columns (see Keep destination keys the same as the replication key).
Two source keys that are equal at the destination (case-insensitive collation, less timestamp precision) Changes to one row hit the other. See Keep destination keys the same as the replication key.
Two groups or jobs that write to the same destination table One loads or empties rows the other then applies changes to. See Never let two groups write to the same destination table.

For a step-by-step diagnosis, see Duplicate key errors when applying changes.

Decision guide

Situation What to do
The source table has a primary key Nothing. Keep the index the Replicator Service creates on it at the destination.
No primary key, but a unique index Nothing: its columns are used. Prefer unique indexes on NOT NULL columns.
No key, Db2 for i, Oracle or Db2 for LUW source The row identifier (RRN, ROWID or RID) is used automatically; on Db2 for LUW, with Stelo Capture 7.0 or later. On Db2 for i, SQ_IXLIST if some columns are unique in practice.
No key, Db2 for i, Oracle or Db2 for LUW, and the member asks for a baseline after every change Force hidden column in the group and re-create the subscription (see Duplicate keys).
Db2 for i file with REUSEDLT(*YES) Prefer its primary key or a unique key to the RRN.
Composite key with a nullable column in a Merge group Key on NOT NULL columns only.
No key, other sources Add a primary key or unique index at the source. Otherwise expect a baseline after every update or delete, keep the member in a Batch group, or use a snapshot subscription.
Key values collide at the destination (case-insensitive collation, less timestamp precision) Case-sensitive collation or a type with the same precision; for Db2 for i, Oracle and Db2 for LUW, the row identifier (Force hidden column, for a whole group).
Db2 for i table journaled with after images only Use the RRN.
Db2 for i or Db2 for LUW tables keyed by the row identifier that are reorganized Plan for a new baseline after each reorganization.
The destination needs a unique constraint Allow UPSERT (primary key on the key after the baseline) or Replicate Primary Keys.
You want a primary key or unique index on other columns at the destination Don't: it breaks the apply. Use a non-unique index.
You add your own indexes to destination tables Non-unique only; Use existing table for baseline so the table isn't dropped; Allow disable/enable indexes for large SQL Server and Oracle baselines.
You create the destination table yourself Also create an index on the replication key (the baseline's messages show the statement).
Destination foreign keys between members Replicate Foreign Keys in the group; they're created after every member is loaded. For applying changes in the right order, see Sequence.
Merge group Every member should have key columns; keep the key index. A member without one gets inserts only.
The key values never change at the source, and you know it from the application Stable key.