Sub topic 15
Direct-path inserts with the APPEND hint
Prerequisites and the basic idea
This chapter assumes that you can run INSERT ... SELECT, understand transactions, and have the emp_demo and emp_archive tables from the earlier examples.
Oracle normally performs a conventional insert. It finds free space in existing blocks, places the new rows there, and uses the buffer cache. A direct-path insert takes a different route. Oracle places new rows above the table's current high-water mark, which is the point up to which the table has formatted storage.
The APPEND hint requests this direct-path behavior:
INSERT /*+ APPEND */ INTO emp_archive
SELECT *
FROM emp_demo
WHERE status = 'INACTIVE';The hint appears immediately after INSERT. It applies to this statement only. It does not permanently change the table or all future inserts.
What APPEND changes
Direct-path inserts can improve large batch loads because Oracle does not search through existing blocks for free space. The operation can also reduce some buffer-cache work. It is most useful when inserting many rows, such as loading a staging table into a warehouse table.
The trade-off is that direct-path inserts can use more space. They append above the high-water mark even when reusable space exists below it. The operation can also acquire stronger table-level locking behavior, which may block other sessions from modifying the same table.
| Insert method | Typical use | Main characteristic |
|---|---|---|
| Conventional insert | Small, interactive changes | Reuses available table blocks |
| Direct-path insert | Large batch loads | Appends rows above the high-water mark |
Oracle treats APPEND as a hint, not a command that guarantees a plan in every situation. Table features, constraints, triggers, and other statement conditions can prevent direct-path execution. Check the execution plan and test with the actual target table.
The transaction restriction
After a direct-path insert, the same transaction cannot read or modify that target table before it ends the transaction. If you try, Oracle can raise ORA-12838, which means that the table was modified by a parallel or direct-path operation and cannot be read or modified again in that transaction.
Commit before querying the target:
INSERT /*+ APPEND */ INTO emp_archive
(emp_id, emp_name, dept_id, salary, status)
SELECT emp_id, emp_name, dept_id, salary, status
FROM emp_demo
WHERE status = 'INACTIVE';
COMMIT;
SELECT COUNT(*) AS archived_rows
FROM emp_archive;The COMMIT makes the inserted rows visible to other sessions and ends the restriction for this transaction. A ROLLBACK also ends the restriction, but it removes the inserted rows.
Choosing the hint
Use direct-path insertion when the load is large, the target is designed for batch loading, and you can schedule the required commit. Prefer conventional inserts for individual transactions or workloads where other sessions must continue changing the target table concurrently.
Do not add APPEND merely because a statement contains INSERT ... SELECT. Measure the load, inspect space growth, and verify that the target table's constraints and triggers support the operation.
Exercise
Why must a session commit or roll back after a direct-path insert before querying the target table?
Show answer
Oracle prevents the same transaction from reading or modifying that target after a direct-path operation, so the transaction must end first.