Sub topic 13
Keeping batch loads going with LOG ERRORS
By default, one bad row makes a multi-row DML statement fail and roll back completely. That is a problem when you load 500,000 rows and 12 of them are bad. The LOG ERRORS clause tells Oracle to write each failing row to an error table and keep processing the rest. This chapter shows how to create the error table, use the clause, and review what it caught. It also covers the cases where the clause doesn't help.
This chapter assumes you know INSERT ... SELECT, constraint errors such as ORA-00001 and ORA-02291, and how COMMIT and ROLLBACK work. It continues with the dept_demo, emp_demo, and emp_stage tables from earlier chapters.
How statement failure works without LOG ERRORS
Oracle treats a single DML statement as atomic. If row 9,000 of an INSERT ... SELECT violates a constraint, Oracle undoes the 8,999 rows already inserted by that statement and raises the error. Your transaction stays open, but the statement did nothing.
The usual workaround is to pre-validate the data with extra queries, or to loop in PL/SQL and insert one row at a time. Both are slow or complicated. LOG ERRORS handles the problem inside the statement itself.
Step 1: Create the error log table
The error table is an ordinary table. DBMS_ERRLOG.CREATE_ERROR_LOG builds it for you.
EXEC DBMS_ERRLOG.CREATE_ERROR_LOG('EMP_DEMO');In tools that don't support EXEC, wrap the call in BEGIN ... END;. By default the new table is named ERR$_ followed by the first 25 characters of the target table name, so here it is ERR$_EMP_DEMO. You can pass a different name as the second argument.
The table has five control columns, followed by one column for each column of the target table:
| Column | Content |
|---|---|
| `ORA_ERR_NUMBER$` | The Oracle error number, such as 1 for ORA-00001 |
| `ORA_ERR_MESG$` | The full error message text |
| `ORA_ERR_ROWID$` | The rowid of the row, for UPDATE and DELETE |
| `ORA_ERR_OPTYP$` | The operation: `I` (insert), `U` (update), or `D` (delete) |
| `ORA_ERR_TAG$` | A label that you supply, up to 2000 characters |
The copied columns are all VARCHAR2(4000), whatever the original type. This lets the table store a value even when the failure was a bad data type or a value that was too large. It also means that you read the logged values as text.
You need the INSERT privilege on the error table, in addition to the privileges on the target table.
Step 2: Add LOG ERRORS to the statement
Put LOG ERRORS at the end of the DML statement. First, load some test rows into emp_stage. The table was created with CREATE TABLE ... AS SELECT, so it has the NOT NULL constraints and column sizes of emp_demo. It has no primary key or foreign key, so it accepts rows that emp_demo would reject.
INSERT INTO emp_stage (emp_id, emp_name, dept_id, salary, status)
VALUES (1, 'Ada Copy', 20, 9500, 'ACTIVE'); -- duplicate key
INSERT INTO emp_stage (emp_id, emp_name, dept_id, salary, status)
VALUES (3, 'Linus', 20, 8000, 'ACTIVE'); -- good
INSERT INTO emp_stage (emp_id, emp_name, dept_id, salary, status)
VALUES (4, 'Ken', 99, 6000, 'ACTIVE'); -- no department 99
INSERT INTO emp_stage (emp_id, emp_name, dept_id, salary, status)
VALUES (5, 'Barbara', 10, 8500, 'ACTIVE'); -- good
COMMIT;Now run the load.
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary, status)
SELECT emp_id, emp_name, dept_id, salary, status
FROM emp_stage
LOG ERRORS INTO err$_emp_demo ('load_1') REJECT LIMIT UNLIMITED;Oracle reports 2 rows created. Employees 3 and 5 are in emp_demo. The other two rows went to the error table. The statement did not raise an error, because every failure was logged.
The clause has three parts:
INTO err$_emp_demonames the error table. You can leave it out if you used the default name.('load_1')is the optional tag. It can be any expression that Oracle can convert to a string, for example'load_' || TO_CHAR(SYSDATE, 'YYYYMMDD').REJECT LIMITis the number of errors the statement tolerates.
Step 3: Read the errors
SELECT ora_err_number$, ora_err_mesg$, emp_id, dept_id
FROM err$_emp_demo
WHERE ora_err_tag$ = 'load_1'
ORDER BY emp_id;You should see error 1 (ORA-00001, unique constraint violated) for employee 1, and error 2291 (ORA-02291, parent key not found) for employee 4. The message text includes the constraint name. The tag lets you separate this load's errors from earlier ones in the same table.
After you fix the source data or add the missing department, you can rerun just the failed rows, because the error table keeps their values.
REJECT LIMIT
REJECT LIMIT is the setting that most often surprises people.
| Setting | Behavior |
|---|---|
| Omitted, or `REJECT LIMIT 0` | The first error is logged, then the statement fails and rolls back |
| `REJECT LIMIT 10` | The statement fails when the 11th error occurs |
| `REJECT LIMIT UNLIMITED` | The statement never fails because of row errors |
The default is 0. If you add LOG ERRORS and forget the limit, the statement still fails on the first bad row. The difference is that the failing row is now recorded in the error table. When the limit is exceeded, Oracle rolls back the statement's changes. The rows already written to the error table remain, because Oracle logs them in an autonomous transaction. A ROLLBACK of your own does not remove them either.
Because the log rows are committed independently, the error table grows until you clear it. A load job should delete the old rows, or use a fresh tag for each run and purge by tag.
Other statements
LOG ERRORS also works with UPDATE, DELETE, and MERGE. For MERGE, put it after the last WHEN clause. Here is an update that logs rows it cannot change:
UPDATE emp_demo
SET salary = salary * 1000
WHERE dept_id = 20
LOG ERRORS INTO err$_emp_demo ('raise_1') REJECT LIMIT UNLIMITED;A salary that exceeds the NUMBER(8,2) precision raises ORA-01438 for that row. Oracle logs the row with ORA_ERR_OPTYP$ = 'U' and its rowid, and the other rows stay updated. Be careful with this pattern. A statement that quietly skips some rows can leave the data half-changed, so always check the error table after the statement.
What is not logged
LOG ERRORS does not catch everything. Know these gaps before you rely on it:
- Deferred constraints. A violation of a deferred constraint is checked at
COMMIT, so the statement does not see it. The error is raised at commit time and the whole transaction fails. - Direct-path operations and unique keys. In a direct-path
INSERT(theAPPENDhint) or a parallel DML, unique or primary key violations are not logged. The statement fails instead. - Some column types. Columns of type
LONG,LOB, or object types cannot be logged. - Errors outside the row. Problems such as a missing table, a privilege error, or a syntax error are statement-level errors. They are raised normally.
If you see an unexpected ORA error from a statement with LOG ERRORS, check this list first.
Practical guidance
- Always set
REJECT LIMITon purpose. UseUNLIMITEDonly when you plan to review the error table afterwards. - Use a tag for each run so the errors are easy to find and purge.
- Treat a non-empty error table as a failed load, even though the statement succeeded. Have the job count the rows for its tag and report them.
- Commit the main load only after you have checked the errors. The error rows are already committed, but your inserted rows are not.
- Expect a slowdown when many rows fail. Each error costs extra work, so a load that logs 30% of its rows is a data-quality problem, not a job for
LOG ERRORS.
Exercise
You run an INSERT ... SELECT with LOG ERRORS INTO err$empdemo ('nightly') but no REJECT LIMIT clause. The 3rd row of 100 violates a foreign key. What happens to the other 99 rows, and where can you see why it failed?
Show answer
None of the rows are inserted. The default REJECT LIMIT is 0, so Oracle logs the first bad row, raises the error, and rolls back the statement. The failing row stays in err$empdemo, because the log rows are written in an autonomous transaction. Query it with WHERE oraerrtag$ = 'nightly' and read ORAERRMESG$, which shows ORA-02291 and the constraint name. To keep the 99 good rows, add REJECT LIMIT UNLIMITED (or a number of at least 1).
Exercise
A load uses INSERT /+ APPEND / ... LOG ERRORS REJECT LIMIT UNLIMITED. A row with a duplicate primary key arrives. Will the row be logged?
Show answer
No. Unique and primary key violations in a direct-path insert are not logged, so the statement fails with ORA-00001. Other errors in the same statement, such as a NOT NULL violation, are still logged. To log duplicate-key rows, remove the APPEND hint or remove the duplicates before the load.