DB2 SQLCODE -803 in a Batch Insert: Distinguishing Duplicate Source Data from a Duplicate Replay
An `SQLCODE -803` on an `INSERT` tells you that DB2 rejected a duplicate value for a unique index or constraint. It does not tell you whether the source contains a genuine duplicate, an earlier attempt already committed the row, or the program built the key incorrectly. That distinction determines whether a restart is safe.
An SQLCODE -803 on an INSERT tells you that DB2 rejected a duplicate value for a unique index or constraint. It does not tell you whether the source contains a genuine duplicate, an earlier attempt already committed the row, or the program built the key incorrectly. That distinction determines whether a restart is safe.
This incident guide uses a fictional order-load process. Names and data are illustrative; the application’s table definitions, commit design, and local recovery procedure are the authority for a real incident.
Symptom: one duplicate error, several possible stories
A nightly loader reads accepted orders from a sequential input file and inserts one row per order into ORDER_EVENT. A job log shows an -803 after 18,000 rows have been processed. The immediate temptation is to remove the input record or start the job again.
Do neither until the team knows what DB2 has committed. The same return code can represent materially different conditions:
| What happened | Typical evidence | Safe direction |
|---|---|---|
| The input contains the same business event twice | Two input records resolve to the same unique key | Correct or reject the duplicate under business rules |
| A prior job attempt committed the row | The row has the same run/control identifier as the failed attempt | Use the approved restart or idempotency path; do not blindly replay |
| A different process inserted the key | Existing row has a different source or timestamp | Investigate ownership and business sequencing |
| The program derived the wrong key | Many failures share a default, truncated, or malformed key | Stop the load and correct the mapping/design |
| The unique index protects a different rule than assumed | Index columns differ from the program’s expected key | Reassess the transaction and the actual uniqueness rule |
-803 is a data-integrity control doing its job. Turning it into “ignore and continue” without a defined reconciliation design can conceal a missing or incorrectly sourced event.
Evidence to capture before changing anything
Capture evidence while it still identifies the attempted run. Record the job name and ID, failed step and program, DB2 subsystem, DB2 SQLCA values available in the application log, input file identity, cycle date, restart parameter or checkpoint reference, and the program/load-module version.
For the failed record, preserve only the approved diagnostic fields: its input sequence number, business identifier, derived DB2 key, and source control identifier. If business data is sensitive, use masked values in tickets and extracts. Also capture:
- The exact unique index or constraint reported by DB2 messages, including its column list from approved catalog access.
- The existing row’s key and non-sensitive lineage fields, such as load run ID, source code, creation timestamp, or status.
- The application’s last successful commit boundary and its committed-row count, not just its read count.
- Whether another job, online transaction, or interface can insert the same entity during this window.
- The disposition of any downstream output, notifications, or external calls made before the failure.
A database query against a production table must follow local access and approval rules. In particular, do not issue DELETE or UPDATE merely to make an insert succeed.
Diagnosis path: start with the enforced rule
First identify the unique object. A table can have a generated primary key plus one or more unique business indexes. An insert may fail because of an alternate uniqueness rule, not the field a programmer expects.
For example, assume this fictional table treats an event as unique by source system and source event number:
CREATE TABLE ORDER_EVENT
( ORDER_ID CHAR(12) NOT NULL,
SOURCE_SYSTEM CHAR(4) NOT NULL,
SOURCE_EVENT_NO CHAR(16) NOT NULL,
LOAD_RUN_ID CHAR(10) NOT NULL,
EVENT_STATUS CHAR(1) NOT NULL );
CREATE UNIQUE INDEX UX_ORDER_EVENT_SOURCE
ON ORDER_EVENT (SOURCE_SYSTEM, SOURCE_EVENT_NO);
```
The COBOL program might perform the insert after deriving the two source fields:
EXEC SQL
INSERT INTO ORDER_EVENT
(ORDER_ID, SOURCE_SYSTEM, SOURCE_EVENT_NO,
LOAD_RUN_ID, EVENT_STATUS)
VALUES
(:WS-ORDER-ID, :WS-SOURCE-SYSTEM,
:WS-SOURCE-EVENT-NO, :WS-LOAD-RUN-ID, 'N')
END-EXEC
IF SQLCODE = 0
ADD 1 TO WS-INSERTED-COUNT
ELSE
IF SQLCODE = -803
PERFORM CAPTURE-DUPLICATE-EVIDENCE
ELSE
PERFORM DB2-ERROR-HANDLING
END-IF
END-IF
```
The code must not assume that ORDER_ID is the duplicate key. Compare the failed host-variable values with the actual unique index columns. A common defect is a blank, zero, or truncated SOURCE-EVENT-NO caused by an input-layout change. If multiple failures have the same derived key, this points to a mapping problem rather than random duplicate source data.
Next, look up the existing row using the *unique-key columns*, selecting only fields authorized for support use. The decision is then evidence-based:
Does a row already exist for SOURCE_SYSTEM + SOURCE_EVENT_NO?
no -> Recheck index/constraint identity and DB2 diagnostic messages.
yes -> Does its LOAD_RUN_ID match the failed run or prior restart attempt?
yes -> Determine whether prior work committed and use restart design.
no -> Is it the same business event under the interface contract?
yes -> Reconcile source duplication or late/replayed delivery.
no -> Investigate key derivation or an ownership conflict.
A matching load-run ID is strong evidence of replay, but it is not proof that all prior work completed. Checkpoint and commit evidence still matters. Conversely, a different run ID does not automatically make the row invalid; it may be a legitimate earlier event that the input should not repeat.
Safe correction options
The correction belongs in the layer that owns the cause. If the source delivered a duplicate event, the interface owner may correct the feed or designate a reject according to the contract. If a prior commit created the row, use the job’s documented restart point, checkpoint table, or idempotent processing rule. If a key field was built incorrectly, stop further loading and correct the transformation after impact analysis and testing.
Avoid the superficially easy options:
- Do not delete the existing row to let the current run pass. It may be a valid committed business event with dependent rows or audit history.
- Do not change a unique index to non-unique as an incident workaround. That weakens an integrity rule for every writer.
- Do not treat
-803as success unless the application explicitly proves the existing row is semantically identical and records that reconciliation. - Do not rerun from the beginning until commit boundaries and downstream effects are understood.
For a deliberately idempotent feed, the program may use a controlled “insert or confirm existing event” pattern. That is a design choice, not generic error handling. It needs comparison of meaningful attributes, audit output, race-condition handling, and tests for a partially committed rerun. MERGE or an existence check can be appropriate in some designs, but only when the table’s concurrency and business semantics have been reviewed.
Before rerun: support checklist
- Confirm the exact unique index or constraint and all of its columns.
- Match failed input values to the host variables and the existing row’s unique key.
- Establish the last successful DB2 commit and whether the failed unit of work rolled back.
- Reconcile input read count, insert count, commit count, rejects, and any checkpoint record.
- Check whether another writer can create the same key during the rerun window.
- Identify later steps, reports, files, or external messages that may already reflect committed rows.
- Obtain the application owner’s decision for duplicate-source handling, replay handling, or code correction.
- Record the evidence and restart scope; do not substitute a full rerun for that decision.
Local DB2 isolation levels, triggers, generated columns, package behavior, and restart standards vary. Validate any query, recovery step, or program change in the approved environment before applying it to production.
Practical takeaway
An SQLCODE -803 is not a cleanup instruction. Treat it as a question about the unique rule and the state of prior work: identify the protected key, compare the attempted and existing rows, establish the commit boundary, then choose the recovery path supported by that evidence.