8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » 10g » Here

DML Error Logging in Oracle 10g Database Release 2

In some situations the most obvious solution to a problem is a multi-row DML statement (INSERT ... SELECT, UPDATE, DELETE), but you may choose to avoid multi-row DML because of the way it reacts to exceptions. By default, when a DML statement fails the whole statement is rolled back, regardless of how many rows were processed successfully before the error was detected. In the past, the only way around this problem was to process each row individually, preferably with a bulk operation using FORALL and the SAVE EXCEPTIONS clause. In Oracle 10g Database Release 2, the DML error logging feature has been introduced to solve this problem. Adding the appropriate LOG ERRORS clause on to most INSERT, UPDATE, MERGE and DELETE statements enables the operations to complete, regardless of errors. This article presents an overview of the DML error logging functionality, with examples of each type of DML statement.

Related articles.

Syntax

The syntax for the error logging clause is the same for INSERT, UPDATE, MERGE and DELETE statements.

LOG ERRORS [INTO [schema.]table] [('simple_expression')] [REJECT LIMIT integer|UNLIMITED]

The optional INTO clause allows you to specify the name of the error logging table. If you omit this clause, the the first 25 characters of the base table name are used along with the "ERR$_" prefix.

The simple_expression is used to specify a tag that makes the errors easier to identify. This might be a string or any function whose result is converted to a string.

The REJECT LIMIT is used to specify the maximum number of errors before the statement fails. The default value is 0 and the maximum values is the keyword UNLIMITED. For parallel DML operations, the reject limit is applied to each parallel server.

Restrictions

The DML error logging functionality is not invoked when:

In addition, the tracking of errors in LONG, LOB and object types is not supported, although a table containing these columns can be the target of error logging.

Sample Schema

This following code creates and populates the tables necessary to run the example code in this article.

-- Create and populate a source table.
create table source (
  id           number(10)    not null,
  code         varchar2(10),
  description  varchar2(50),
  constraint source_pk primary key (id)
);

insert into source
select level,
       to_char(level),
       'Description for ' || to_char(level)
from   dual
connect by level <= 100000;
commit;

-- For possible error conditions.
update source set code = null where id in (1000, 10000);
commit;

exec dbms_stats.gather_table_stats(user, 'source', cascade => true);

-- Create a destination table.
create table dest (
  id           number(10)    not null,
  code         varchar2(10)  not null,
  description  varchar2(50),
  constraint dest_pk primary key (id)
);

-- Create a dependant of the destination table.
create table dest_child (
  id       number,
  dest_id  number,
  constraint child_pk primary key (id),
  constraint dest_child_dest_fk foreign key (dest_id) references dest(id)
);

Notice that the CODE column is optional in the SOURCE table and mandatory in the DEST table.

Once the basic tables are in place we can create a table to hold the DML error logs for the DEST. A log table must be created for every base table that requires the DML error logging functionality. This can be done manually or with the CREATE_ERROR_LOG procedure in the DBMS_ERRLOG package, as shown below.

-- Create error logging table. Default name.
begin
  dbms_errlog.create_error_log (dml_table_name => 'dest');
end;
/

-- Create error logging table. Custom name.
begin
  dbms_errlog.create_error_log (dml_table_name     => 'dest',
                                err_log_table_name => 'dest_err_log');
end;
/

The owner, name and tablespace of the log table can be specified, but by default it is created in the current schema, in the default tablespace with a name that matches the first 25 characters of the base table with the "ERR$_" prefix.

column table_name format a20
column tablespace_name format a20

SELECT table_name, tablespace_name
FROM   user_tables;

TABLE_NAME           TABLESPACE_NAME
-------------------- --------------------
SOURCE               USERS
DEST                 USERS
DEST_CHILD           USERS
ERR$_DEST            USERS
DEST_ERR_LOG         USERS

SQL>

The structure of the log table includes maximum length and datatype independent versions of all available columns from the base table, as seen below.

SQL> desc err$_dest
 Name                              Null?    Type
 --------------------------------- -------- --------------
 ORA_ERR_NUMBER$                            NUMBER
 ORA_ERR_MESG$                              VARCHAR2(2000)
 ORA_ERR_ROWID$                             ROWID
 ORA_ERR_OPTYP$                             VARCHAR2(2)
 ORA_ERR_TAG$                               VARCHAR2(2000)
 ID                                         VARCHAR2(4000)
 CODE                                       VARCHAR2(4000)
 DESCRIPTION                                VARCHAR2(4000)

SQL>

Insert

When we built the sample schema we noted that the CODE column is optional in the SOURCE table, but mandatory in th DEST table. When we populated the SOURCE table we set the code to NULL for two of the rows. If we try to copy the data from the SOURCE table to the DEST table we get the following result.

insert into dest
select *
from   source;

SELECT *
       *
ERROR at line 2:
ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


SQL>

The failure causes the whole insert to roll back, regardless of how many rows were inserted successfully. Adding the DML error logging clause allows us to complete the insert of the valid rows.

insert into dest
select *
from   source
log errors into err$_dest ('INSERT') reject limit unlimited;

99998 rows created.

SQL>

The rows that failed during the insert are stored in the ERR$_DEST table, along with the reason for the failure.

column ora_err_mesg$ format a70

select ora_err_number$, ora_err_mesg$
from   err$_dest
where  ora_err_tag$ = 'INSERT';

ORA_ERR_NUMBER$ ORA_ERR_MESG$
--------------- ---------------------------------------------------------
           1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")
           1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")

2 rows selected.

SQL>

Update

The following code attempts to update the CODE column for 10 rows, setting it to itself for 8 rows and to the value NULL for 2 rows.

update dest
set    code = decode(id, 9, null, 10, null, code)
where  id between 1 and 10;
       *
ERROR at line 2:
ORA-01407: cannot update ("TEST"."DEST"."CODE") to NULL


SQL>

As expected, the statement fails because the CODE column is mandatory. Adding the DML error logging clause allows us to complete the update of the valid rows.

update dest
set    code = decode(id, 9, null, 10, null, code)
where  id between 1 and 10
log errors into err$_dest ('UPDATE') reject limit unlimited;

8 rows updated.

SQL>

The rows that failed during the update are stored in the ERR$_DEST table, along with the reason for the failure.

column ora_err_mesg$ format a70

select ora_err_number$, ora_err_mesg$
from   err$_dest
where  ora_err_tag$ = 'UPDATE';

ORA_ERR_NUMBER$ ORA_ERR_MESG$
--------------- ---------------------------------------------------------
           1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")
           1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")

2 rows selected.

SQL>

Merge

The following code deletes some of the rows from the DEST table, then attempts to merge the data from the SOURCE table into the DEST table.

delete from dest
where  id > 50000;

merge into dest a
    using source b
    on (a.id = b.id)
  when matched then
    update set a.code        = b.code,
               a.description = b.description
  when not matched then
    insert (id, code, description)
    values (b.id, b.code, b.description);
                  *
ERROR at line 9:
ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")


SQL>

As expected, the merge operation fails and rolls back. Adding the DML error logging clause allows the merge operation to complete.

merge into dest a
    using source b
    on (a.id = b.id)
  when matched then
    update set a.code        = b.code,
               a.description = b.description
  when not matched then
    insert (id, code, description)
    values (b.id, b.code, b.description)
  log errors into err$_dest ('MERGE') reject limit unlimited;

99998 rows merged.

SQL>

The rows that failed during the update are stored in the ERR$_DEST table, along with the reason for the failure.

column ora_err_mesg$ format a70

select ora_err_number$, ora_err_mesg$
from   err$_dest
where  ora_err_tag$ = 'MERGE';

ORA_ERR_NUMBER$ ORA_ERR_MESG$
--------------- ---------------------------------------------------------
           1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")
           1400 ORA-01400: cannot insert NULL into ("TEST"."DEST"."CODE")

2 rows selected.

SQL>

Delete

The DEST_CHILD table has a foreign key to the DEST table, so if we add some data to it would would expect an error if we tried to delete the parent rows from the DEST table.

insert into dest_child (id, dest_id) values (1, 100);
insert into dest_child (id, dest_id) values (2, 101);

With the child data in place we ca attempt to delete th data from the DEST table.

delete from dest;
*
ERROR at line 1:
ORA-02292: integrity constraint (TEST.DEST_CHILD_DEST_FK) violated - child record found


SQL>

As expected, the delete operation fails. Adding the DML error logging clause allows the delete operation to complete.

delete from dest
log errors into err$_dest ('DELETE') reject limit unlimited;

99996 rows deleted.

SQL>

The rows that failed during the delete operation are stored in the ERR$_DEST table, along with the reason for the failure.

column ora_err_mesg$ format a69

select ora_err_number$, ora_err_mesg$
from   err$_dest
where  ora_err_tag$ = 'DELETE';

ORA_ERR_NUMBER$ ORA_ERR_MESG$
--------------- ---------------------------------------------------------------------
           2292 ORA-02292: integrity constraint (TEST.DEST_CHILD_DEST_FK) violated -
                child record found

           2292 ORA-02292: integrity constraint (TEST.DEST_CHILD_DEST_FK) violated -
                child record found


2 rows selected.

SQL>

Performance

The performance of DML error logging depends on the way it is being used and what version of the database you use it against. Prior to Oracle 12c, you will probably only use DML error logging during direct path loads, since conventional path loads become very slow when using it. The following example displays this, but before we start we will need to remove the extra dependency table.

drop table dest_child purge;

Truncate the destination table and run a conventional path load using DML error logging, using SQL*Plus timing to measure the elapsed time.

set timing on

truncate table dest;

insert into dest
select *
from   source
log errors into err$_dest ('INSERT NO-APPEND') reject limit unlimited;

99998 rows created.

Elapsed: 00:00:08.61
SQL>

Next, repeat the test using a direct path load this time.

truncate table dest;

insert /*+ append */ into dest
select *
from   source
log errors into err$_dest ('INSERT APPEND') reject limit unlimited;

99998 rows created.

Elapsed: 00:00:00.38
SQL>

Finally, perform the same load using FORALL ... SAVE EXCEPTIONS method.

truncate table dest;

declare
  type t_tab is table of dest%rowtype;
  l_tab t_tab;

  l_start pls_integer;

  cursor c_source is
    select * from source;

  ex_dml_errors exception;
  pragma exception_init(ex_dml_errors, -24381);
begin

  open c_source;
  loop
    fetch c_source
    bulk collect into l_tab limit 1000;
    exit when l_tab.count = 0;

    begin
      forall i in l_tab.first .. l_tab.last save exceptions
        insert into dest values l_tab(i);
    exception
      when ex_dml_errors then
        null;
    end;
  end loop;
  close c_source;
end;
/

PL/SQL procedure successfully completed.

Elapsed: 00:00:01.01
SQL>

From this we can see that DML error logging is very fast for direct path loads, but does not perform well for conventional path loads. In fact, it performs significantly worse than the FORALL ... SAVE EXCEPTIONS method.

The relative performance of these methods depends on the database version. The following table shows the results of the previous tests against a number of database versions. They are run on different servers, so don't compare version-to-version. Look at the comparison between the methods within a version.

                               10.2.0.4   11.2.0.3   11.2.0.4   12.1.0.1
                               ========   ========   ========   ========
DML Error Logging          :      07.62      08.61      04.82      00.94
DML Error Logging (APPEND) :      00.86      00.38      00.85      01.07
FORALL ... SAVE EXCEPTIONS :      01.15      01.01      00.94      01.37

For more information see:

Hope this helps. Regards Tim...

Back to the Top.