Skip to content

Designing subscriptions

Choices made on each subscription: who owns the destination table, apply options, keys, criteria, derived columns, mapping and LOBs.

Decide who owns the destination table

Do:

  • Let the Replicator Service create the table (Create every time subscription is Saved, the default) unless you need something it doesn't create.
  • Use Use existing table for baseline when you manage the table yourself: extra columns, partitions, storage options, permissions granted to other users, or foreign keys.

Why: a table the Replicator Service creates always matches the subscription's columns and keys, and ALTER processing can change it later. But with Create every time subscription is Saved, saving the subscription drops and re-creates the table, with its data, grants and any indexes you added (you're warned when the table exists). An existing table is never dropped, but you keep it in step with the source: ALTER processing doesn't change it, the incremental apply options aren't available, and mapped columns it doesn't have are removed from the subscription. See Destination DDL Options.

With Use existing table for baseline the table is yours and the Replicator Service never changes it; with Create if does not exist, else use existing (or the Create options) it's the service's, and a save may add the columns you add to the subscription. Either way the subscription takes the column types the table actually has, and a save tells you where they differ from the mapping in a way that matters. See Who owns the destination table.

Plan how changes to an incremental subscription reach the destination

Do: when a save shows the Baseline Snapshot Required dialog, prefer Alter Destination on Next Run for column changes, and choose Recompute existing rows or New changes only for each added column. Use Resume Without Baseline only when you'll make the existing destination data match the new definition yourself. Choose Require Baseline when the table must be reloaded anyway, and save when a baseline is acceptable (or with Automatic Snapshot off). For subscriptions whose columns you expect to change, consider Create if does not exist, else use existing, which keeps the table on save.

Why: a save needs a baseline only when it changes the columns, the criteria, the row identifier, the capture or apply options, or when the table is re-created on save. With Create every time subscription is Saved, every save re-creates the table and needs a baseline, and the other two choices aren't offered. Recomputing existing rows makes the group's change data wait while the run alters the table. See Saving an incremental subscription.

Choose the apply options for what the destination is for

Do:

  • Strict Apply Rules (the default) for an exact copy of the source.
  • Allow UPSERT when the destination may already hold some of the rows, or changes may be applied twice (after a replay, or when you load the table yourself).
  • Soft Delete when deleted rows must stay: add an @TYPE derived column (which becomes D) and a timestamp such as @TS, both nullable. Baseline such tables with Delete existing data before replication, which keeps the rows marked as deleted.
  • Archive all DML Activity for a history of changes, with @TS and @TYPE columns.

Why: Strict Apply Rules flags any change that doesn't affect exactly the expected row, which tells you when the destination has drifted; Allow UPSERT tolerates that drift but hides it. A soft delete updates only the derived columns, so without @TYPE a deleted row looks live; capture metadata is NULL until the row changes, so a non-nullable column makes inserts fail. An archive table only grows (every change is an insert, and no index is created), so plan its size and how you purge it. See Incremental apply options and Capture metadata.

Give every table a reliable key

Replicate tables that have a primary key or a unique index; for Db2 for i and Oracle tables without one, use the relative record number or ROWID (Force hidden column). Keep destination unique keys the same as the replication key, and keep key values that differ at the source different at the destination (collation, timestamp precision). Every update and delete is found by its key, so a wrong or colliding key makes changes hit the wrong row. See Keys, RRN/ROWID and indexes, in particular its decision guide.

Use constant criteria

Do: limit rows with a condition on constant values of the table's own columns, such as WHERE REGION = 'WEST'. Don't refer to other tables, the current date, or LOB columns. For incremental subscriptions, use only SQL that is valid both in the source database and in Db2 for LUW: no functions that only the source has. Run a new baseline after changing the criteria. Don't add GROUP BY, HAVING or ORDER BY (a save refuses them); to replicate grouped rows, subscribe to a source view.

Why: for an incremental subscription, the Capture Agent evaluates the criteria on the captured changes in the staging database, when the group requests them. An update that moves a row into the criteria is applied as an insert, and one that moves it out as a delete. A change to another table, or the passing of time, produces no change on this table, so rows that start or stop matching that way are never added or removed. LOB values aren't staged, so they can't be evaluated. Because the criteria run in the Db2 for LUW staging database (like derived columns), a function only the source has fails the save of the subscription: for example, Oracle's DBMS_ROWID.ROWID_BLOCK_NUMBER(rowid) gives DB2 SQL Error: SQLCODE=-440, SQLSTATE=42884, SQLERRMC=ROWID_BLOCK_NUMBER;FUNCTION, also in the agent's Diagnostics. Remove the criteria, or write one valid in both databases. See Criteria. On Db2 for i, criteria need tables journaled with before and after images (see Before and after images).

A criteria also makes the before image of updates available to change data logging for subscriptions that don't archive: 1=1 is enough (see Change Data Logging).

Write derived columns for the staging database too

Do: for incremental subscriptions, check that each derived column expression is also valid Db2 for LUW SQL, or enter a Staging Expression. Test with Resolve & test. Guard conversions (for example a CAST of a column that may hold invalid values) so that they can't fail on a single row.

Why: the expression runs in the source database for the baseline, and in the Capture Agent's Db2 for LUW staging database for every change (see Expressions for incremental subscriptions). One that fails there fails the poll for the whole group, not just the row.

Set mapping options before you create subscriptions

Do: when source and destination use different character encodings (for example single-byte EBCDIC to UTF-8), choose inflation, Unicode types or unlimited VARCHAR in Service Settings › Subscription Wizard before creating the subscriptions. See Character data.

Why: mapping options apply when a subscription's columns are built. Changing them later means Rebuild Column List (which discards your changes on the Columns tab) and a new baseline. A character column too short at the destination makes rows fail with truncation errors. The Inflation percentage is service-wide: the Replicator Service uses it for every subscription, whatever the subscription's Mapping Options show.

Size the LOB limit for your largest values

Do: set the Indeterminable-size column limit (KB) (service-wide, or for one group in Group Replication Settings) above the largest LOB value you replicate, and isolate tables with large LOBs in their own group (see Designing groups).

Why: values of indeterminable size are held in memory up to this limit, during baselines and incremental apply; a larger value is truncated or fails the replication, depending on the destination column. See LOB and long columns and Error: Data truncated for a LOB column.

Replicate the base tables of a materialized view

Do: replicate the tables a materialized view is built on incrementally, and build the materialized view on the destination tables. If you need the view's data itself, copy it with a snapshot subscription.

Why: when a materialized view is refreshed, the database usually regenerates all its rows, which produces a lot of log activity: an incremental subscription would have to apply every regenerated row one at a time, at least ten times slower than a snapshot, which loads the rows in bulk. The base tables change only by what the applications change.