Append hint with autotrace
-
29-09-2020 - |
Question
I'm doing a direct-path insert which executes successfully. When I try to autotrace the execution, I get the ORA-12838: cannot read/modify an object after modifying it in parallel
.
SQL> insert into emp_big select * from emp_big_temp;
411843 rows created.
Elapsed: 00:00:00.89
Execution Plan
----------------------------------------------------------
Plan hash value: 3203427748
-----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------
| 0 | INSERT STATEMENT | | 357K| 45M| 1164 (1)| 00:00:14 |
| 1 | LOAD TABLE CONVENTIONAL | EMP_BIG | | | | |
| 2 | TABLE ACCESS FULL | EMP_BIG_TEMP | 357K| 45M| 1164 (1)| 00:00:14 |
-----------------------------------------------------------------------------------------
Note
-----
- dynamic sampling used for this statement (level=2)
Statistics
----------------------------------------------------------
15 recursive calls
31909 db block gets
12829 consistent gets
0 physical reads
34685216 redo size
845 bytes sent via SQL*Net to client
802 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
411843 rows processed
SQL> rollback;
Rollback complete.
Elapsed: 00:00:00.18
SQL>
SQL> insert /*+ append */ into emp_big select * from emp_big_temp;
411843 rows created.
Elapsed: 00:00:00.18
Execution Plan
----------------------------------------------------------
ERROR:
ORA-12838: cannot read/modify an object after modifying it in parallel
SP2-0612: Error generating AUTOTRACE EXPLAIN report
Statistics
----------------------------------------------------------
0 recursive calls
4438 db block gets
4283 consistent gets
0 physical reads
7648 redo size
829 bytes sent via SQL*Net to client
816 bytes received via SQL*Net from client
3 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
411843 rows processed
Questions:
- Table
emp_big
has degree of parallelism set to 1, according toselect table_name, degree from user_tables
. Why does Oracle force parallel insert? - Why does using
autotrace trace
causeORA-12838
?
Solution
Answer to the original output:
This is not caused by autotrace. You will get the same error even without autotrace, just by repeating the same direct path insert. Issue a commit after the first insert.
This is a documented restriction: ORA-12700 to ORA-19400
ORA-12838: cannot read/modify an object after modifying it in parallel
Cause: Within the same transaction, an attempt was made to add read or modification statements on a table after it had been modified in parallel or with direct load. This is not permitted.
Action: Rewrite the transaction, or break it up into two transactions: one containing the initial modification and the second containing the parallel modification operation.
Answer to the new output, after edit:
Ok, so you have a rollback, and you started a new transaction. In this case, autotrace really is the culprit, indirectly. The above rule still applies, and as the above description says, even a read reference will cause the above error. If autotrace is enabled, it implicitly runs something like this (after your original statement was finished):
EXPLAIN PLAN SET STATEMENT_ID='PLUSxxxxxx' FOR insert /*+ append */ into emp_big select * from emp_big_temp;
You can easily confirm this by enabling the errorstack trace for the error 12838 (it is fine in a sandbox environment for this short demo, but this is not how I would do this on a real database):
alter system set events '12838 trace name errorstack level 3';
If I run this now:
SQL> create table t1 as select * from dba_objects where 1=2;
Table created.
SQL> alter system set events '12838 trace name errorstack level 3';
System altered.
SQL> set autotrace on
SQL> insert /*+ append */ into t1 select * from dba_objects;
20079 rows created.
Execution Plan
----------------------------------------------------------
ERROR:
ORA-12838: cannot read/modify an object after modifying it in parallel
I will find the error and a trace file in the alert log. The trace file contains this:
dbkedDefDump(): Starting a non-incident diagnostic dump (flags=0x0, level=3, mask=0x0)
----- Error Stack Dump -----
ORA-12838: cannot read/modify an object after modifying it in parallel
----- Current SQL Statement for this session (sql_id=5x12x8czsd3t9) -----
EXPLAIN PLAN SET STATEMENT_ID='PLUS730007' FOR insert /*+ append */ into t1 select * from dba_objects
And that is what causes the error, because it is still the same transaction. You can test it even without autotrace:
SQL> rollback;
Rollback complete.
SQL> set autotrace off
SQL> insert /*+ append */ into t1 select * from dba_objects;
20079 rows created.
SQL> explain plan for insert /*+ append */ into t1 select * from dba_objects;
explain plan for insert /*+ append */ into t1 select * from dba_objects
*
ERROR at line 1:
ORA-12838: cannot read/modify an object after modifying it in parallel
SQL> select count(*) from t1;
select count(*) from t1
*
ERROR at line 1:
ORA-12838: cannot read/modify an object after modifying it in parallel