Sub topic 15
Direct-path inserts with the APPEND hint
This chapter assumes you can write INSERT ... SELECT, understand COMMIT and ROLLBACK, and know what a table segment and an index are. It uses the emp_demo and emp_archive tables from earlier chapters.
Conventional and direct-path inserts
A normal INSERT is a conventional-path insert. Oracle looks for free space in blocks the table already owns. It reads those blocks through the buffer cache, places the new rows, and generates undo and redo for every change.
A direct-path insert takes a different route. Oracle formats new blocks and writes them above the table's high-water mark (HWM). The HWM is the boundary between blocks that have ever held data and blocks that have never been used. Oracle skips the search for free space and does not reuse space freed by earlier deletes. The data goes to the datafiles with little undo, and the HWM moves up when you commit.
Requesting a direct-path insert
Add the APPEND hint to an INSERT ... SELECT:
INSERT /*+ APPEND */ INTO emp_archive
SELECT * FROM emp_demo WHERE status = 'INACTIVE';
COMMIT;
SELECT COUNT(*) FROM emp_archive;The COMMIT is required before the SELECT. Until you commit, the session cannot read or modify emp_archive again. A query or DML on the table in the same transaction raises this error:
ORA-12838: cannot read/modify an object after modifying it in parallelThe message mentions "parallel" even when you did not ask for parallel execution. Treat ORA-12838 as a sign that you touched a table after a direct-path insert and before the commit.
A ROLLBACK is also allowed. Oracle discards the new blocks above the HWM, so the table returns to its earlier state.
Confirming that Oracle used direct path
A hint is a request, not a command. If Oracle cannot honor APPEND, it silently performs a conventional insert and raises no error. To check, look at the execution plan:
EXPLAIN PLAN FOR
INSERT /*+ APPEND */ INTO emp_archive
SELECT * FROM emp_demo WHERE status = 'INACTIVE';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);Look at the operation under the INSERT STATEMENT line:
| Plan operation | Meaning |
|---|---|
| `LOAD AS SELECT` | Direct-path insert |
| `LOAD TABLE CONVENTIONAL` | Conventional insert |
Another check is to run the insert and then query the table before committing. If you get ORA-12838, the insert was direct path. If the query works, Oracle quietly used the conventional path.
When Oracle ignores the hint
Oracle falls back to a conventional insert when it cannot meet the restrictions of direct path. Common causes:
- The target table has enabled triggers.
- The target table is the child side of an enabled foreign key, or the parent side of one.
- The table is in a cluster, or is an object table with certain features.
- The statement is a single-row
INSERT ... VALUES. In that caseAPPENDhas no effect. UseAPPEND_VALUESinstead (see below).
The foreign key restriction matters because the first chapters of this tutorial created emp_demo with a foreign key to dept_demo. Check the constraint rules before you assume a hint works. Because emp_archive was built with CREATE TABLE ... AS SELECT, it carries only the NOT NULL constraints and none of the keys. That is why the archive examples can use direct path.
The restrictions can change between releases, so test your own version with EXPLAIN PLAN. Do not rely on a list from memory.
Locking effects
A direct-path insert takes an exclusive table-level lock (TM lock, mode 6) on the target table. Other sessions can still run queries against the table, and they see the data as of before your insert. They cannot run INSERT, UPDATE, DELETE, or MERGE against it until you commit or roll back. This is different from the row-level locks of ordinary DML, where many sessions can change different rows at once.
For this reason, a direct-path insert is a poor fit for a table that many online users write to. It suits staging tables, archive tables, and batch loads where one process owns the table.
Undo and redo
Direct path avoids undo for the table data, because rolling back only needs to discard the blocks above the HWM. This is the main speed gain, along with writing full blocks instead of single rows.
Redo is more complicated:
- If the table is in
LOGGINGmode (the default) and the database is inARCHIVELOGmode, Oracle still generates full redo. The load is faster than a conventional insert, but redo is not reduced. - If the table is set to
NOLOGGING, a direct-path insert generates minimal redo. The change is not in the redo stream, so you cannot recover it from archived logs. Take a backup after the load. FORCE LOGGINGat the database or tablespace level overridesNOLOGGING. Data Guard standby databases normally require it.
ALTER TABLE emp_archive NOLOGGING;
INSERT /*+ APPEND */ INTO emp_archive
SELECT * FROM emp_demo WHERE status = 'INACTIVE';
COMMIT;
ALTER TABLE emp_archive LOGGING;Indexes on the target table are still maintained, and that work generates undo and redo as usual. For very large loads, a common pattern is to make the indexes unusable, load the data, and then rebuild the indexes. Do this only if your team's rules allow it, because a unique index that is unusable stops enforcing uniqueness while it is unusable.
Single-row loads: APPEND_VALUES
APPEND works with INSERT ... SELECT. For INSERT ... VALUES, use APPEND_VALUES, available since Oracle 11g Release 2:
INSERT /*+ APPEND_VALUES */ INTO emp_archive
(emp_id, emp_name, dept_id, salary, status)
VALUES (3, 'Linus', 20, 8000, 'INACTIVE');
COMMIT;A single row inserted this way would waste space, because Oracle formats a whole new block for it. The hint is useful in PL/SQL with FORALL, where many rows are sent in one batch. It has the same rules as APPEND: commit before you read the table again.
Space and storage costs
Because direct path never reuses free space below the HWM, repeated small direct-path loads grow the table without bound, even if you delete rows between loads. Each load starts a new set of blocks above the mark. The same table can hold many half-empty blocks.
If you load and purge a table cyclically, TRUNCATE resets the HWM. A conventional insert is better for small, frequent changes. Reserve APPEND for large batches.
You can use NOAPPEND to turn off direct-path behavior when something else, such as a parallel hint, would otherwise request it:
INSERT /*+ NOAPPEND */ INTO emp_archive
SELECT * FROM emp_demo WHERE status = 'INACTIVE';Parallel direct-path inserts
Parallel DML always uses direct path for the insert. You must enable it at the session level and ask for parallelism:
ALTER SESSION ENABLE PARALLEL DML;
INSERT /*+ PARALLEL(emp_archive, 4) */ INTO emp_archive
SELECT /*+ PARALLEL(emp_demo, 4) */ * FROM emp_demo;
COMMIT;The same ORA-12838 rule applies. Parallel DML adds more restrictions and needs enough parallel server processes, so test it on a realistic data volume before you rely on it.
Decision guide
| Situation | Choice |
|---|---|
| Loading many thousands of rows or more into a staging or archive table | `APPEND` |
| Frequent small inserts into an online table | Conventional insert |
| Table has triggers or foreign keys | Conventional insert, or plan around the restriction |
| Many sessions need to write to the table at the same time | Conventional insert |
| Table is purged and refilled in full each cycle | `TRUNCATE`, then `APPEND` |
Summary
INSERT /*+ APPEND */ ... SELECTwrites new blocks above the HWM and uses little undo for table data.- Oracle takes an exclusive table lock until you commit or roll back.
- You cannot read or modify the table again in the same transaction. Doing so raises ORA-12838.
- The hint can be ignored without any error. Check the plan for
LOAD AS SELECT. - Redo is reduced only for
NOLOGGINGtables in databases without forced logging. - Direct path does not reuse free space, so use it for large batches only.
Exercise
You run INSERT /+ APPEND / INTO emparchive SELECT * FROM empdemo WHERE status = 'INACTIVE'; and then immediately run SELECT COUNT(*) FROM emp_archive; in the same session. You get ORA-12838. What went wrong, and how do you fix it?
Show answer
The insert used the direct-path method, and a session cannot read or modify the table again until the transaction ends. Run COMMIT (or ROLLBACK) after the INSERT and before the SELECT.
Exercise
You add the APPEND hint to an insert, and no error appears. A colleague says the insert is still conventional. How can you check, and name two likely causes?
Show answer
Run EXPLAIN PLAN for the statement and display it with DBMS_XPLAN.DISPLAY. LOAD AS SELECT means direct path, and LOAD TABLE CONVENTIONAL means the hint was ignored. Likely causes are an enabled trigger on the target table or an enabled foreign key constraint on it.
Exercise
A nightly job deletes old rows from a table and then loads new rows with APPEND. After a few weeks the table is much larger than its row count suggests. Why, and what would fix it?
Show answer
Direct-path inserts write above the high-water mark and never reuse the free space left by deletes below it. Each load adds new blocks. If the job replaces the whole table content, use TRUNCATE to reset the high-water mark before the load.