Sub topic 13
COMMIT WRITE options for redo durability trade-offs
This chapter assumes you know what COMMIT does, that Oracle records changes as redo, and that you can run a PL/SQL block. It covers the one place where Oracle lets you trade a small amount of durability for commit speed, and when that trade is acceptable.
What a commit waits for
When you issue COMMIT, Oracle writes a commit record into the redo log buffer in memory. The log writer process (LGWR) then writes the buffer to the online redo log files on disk. By default, your session does not get control back until LGWR confirms the write. While the session waits, it shows the wait event log file sync.
That wait is what makes a committed transaction survive an instance crash. It is also a cost you pay on every commit. Short transactions that commit constantly, such as row-by-row loads, can spend most of their time in log file sync.
The COMMIT statement has two optional choices that change this behavior:
COMMIT WRITE [IMMEDIATE | BATCH] [WAIT | NOWAIT];- IMMEDIATE or BATCH controls who decides when redo is written. IMMEDIATE tells LGWR to write the redo now. BATCH lets LGWR buffer the redo and write it when it chooses, which allows it to combine the redo of many commits into fewer writes.
- WAIT or NOWAIT controls whether your session waits for the write to be confirmed. WAIT blocks until the redo is on disk. NOWAIT returns immediately after the commit record reaches the log buffer.
Leaving out a choice selects the default for that choice. Plain COMMIT is the same as COMMIT WRITE IMMEDIATE WAIT.
The four combinations
| Statement | Redo write triggered | Session waits | Durable when COMMIT returns |
|---|---|---|---|
| `COMMIT WRITE IMMEDIATE WAIT` (default) | Immediately | Yes | Yes |
| `COMMIT WRITE IMMEDIATE NOWAIT` | Immediately | No | Not guaranteed |
| `COMMIT WRITE BATCH WAIT` | At LGWR's discretion | Yes | Yes, once the call returns |
| `COMMIT WRITE BATCH NOWAIT` | At LGWR's discretion | No | Not guaranteed |
Only the WAIT variants guarantee that the redo is on disk when COMMIT returns. With NOWAIT, your session continues while the write is still in flight, so there is a short window in which Oracle has reported success but the commit record exists only in memory.
The next diagram shows the difference in timing.
What you risk with NOWAIT
NOWAIT does not threaten the integrity of the database. If the instance crashes before LGWR writes the commit record, instance recovery rolls the transaction back as if it had never committed. You do not get half a transaction, and you do not get corruption. What you can lose is a transaction that your application was told had committed.
Two consequences follow from this.
- The application may act on a commit that later disappears. If a program commits a payment with NOWAIT and then sends a confirmation email, a crash in the gap leaves the customer with an email for a payment that no longer exists.
- Other sessions can see the data early. The change becomes visible to other sessions at commit time, not when the redo is written. A second session can read the new row, act on it, and then the first transaction is lost in a crash.
Where NOWAIT is acceptable
The option fits work that is easy to repeat or where losing the last moments of data is tolerable:
- Bulk loads that you can restart from a known point after a crash.
- Staging or intermediate data in a batch job, where a final step re-checks the results.
- Session tracking, counters, or telemetry where a few lost seconds do not matter.
A useful pattern is to use the fast option for the many small commits inside a job, and to finish the job with a normal COMMIT (WAIT). Redo is written in order, so once a later commit with WAIT returns, the redo of your earlier NOWAIT commits in the same session is on disk too. The job's final state is then durable, even though each step was not individually confirmed.
Setting the behavior without changing the statement
You can leave your COMMIT statements unchanged and set the behavior for a session or the whole instance with two parameters:
ALTER SESSION SET COMMIT_LOGGING = BATCH;
ALTER SESSION SET COMMIT_WAIT = NOWAIT;COMMIT_LOGGING accepts IMMEDIATE or BATCH. COMMIT_WAIT accepts WAIT, NOWAIT, or FORCEWAIT. Setting `COMMITWAIT to FORCE_WAIT makes every commit wait. An older single parameter, COMMIT_WRITE`, did the same job but is deprecated, so avoid it in new code.
The statement-level COMMIT WRITE options override the parameters for that one commit. Prefer the explicit statement form in code that needs it. A session parameter set far from the code is easy to forget, and it changes durability for every commit that follows.
Measuring the difference
The following block commits 5,000 single-row inserts three ways and prints the elapsed time. Run it in a test schema. Your numbers depend on your storage, and the gap is usually largest on storage with slow synchronous writes.
SET SERVEROUTPUT ON
CREATE TABLE commit_demo (
id NUMBER,
v VARCHAR2(20)
);
DECLARE
c_rows CONSTANT PLS_INTEGER := 5000;
t0 PLS_INTEGER;
BEGIN
t0 := DBMS_UTILITY.GET_TIME;
FOR i IN 1 .. c_rows LOOP
INSERT INTO commit_demo VALUES (i, 'wait');
COMMIT WRITE IMMEDIATE WAIT;
END LOOP;
DBMS_OUTPUT.PUT_LINE('IMMEDIATE WAIT: ' || (DBMS_UTILITY.GET_TIME - t0) || ' cs');
t0 := DBMS_UTILITY.GET_TIME;
FOR i IN 1 .. c_rows LOOP
INSERT INTO commit_demo VALUES (i, 'nowait');
COMMIT WRITE IMMEDIATE NOWAIT;
END LOOP;
DBMS_OUTPUT.PUT_LINE('IMMEDIATE NOWAIT: ' || (DBMS_UTILITY.GET_TIME - t0) || ' cs');
t0 := DBMS_UTILITY.GET_TIME;
FOR i IN 1 .. c_rows LOOP
INSERT INTO commit_demo VALUES (i, 'batch');
COMMIT WRITE BATCH NOWAIT;
END LOOP;
DBMS_OUTPUT.PUT_LINE('BATCH NOWAIT: ' || (DBMS_UTILITY.GET_TIME - t0) || ' cs');
COMMIT; -- final commit with the default WAIT behavior
END;
/DBMS_UTILITY.GET_TIME returns hundredths of a second (cs). To see the cause rather than just the result, compare the system-wide wait statistics before and after a run. You need access to the dynamic performance views, which normally means a DBA grant.
SELECT event, total_waits, time_waited_micro
FROM v$system_event
WHERE event IN ('log file sync', 'log file parallel write');With WAIT, log file sync waits grow by roughly one per commit. With NOWAIT, the sessions do not wait on it for those commits.
Common mistakes
- Using NOWAIT to fix a design problem. If a job spends its time committing after every row, the first fix is to commit less often, at a business boundary. NOWAIT hides the cost and keeps the risk.
- Treating the commit as confirmed. After a NOWAIT commit, do not report success to a user or an external system until a WAIT commit has followed.
- Forgetting the scope. A session-level
COMMIT_WAIT = NOWAITstays in force until the session ends. Reset it withALTER SESSION SET COMMIT_WAIT = WAITwhen the fast section is over. - Using it with distributed work. Keep the default in transactions that span database links. Two-phase commit depends on each participant's commit being durable, and the shortcuts defeat that guarantee.
Exercise
A nightly job loads 2 million rows into a staging table, committing every 100 rows with COMMIT WRITE BATCH NOWAIT. The instance crashes at row 1,500,000. Can the database be corrupted, can committed batches be missing after recovery, and what single change makes the end of the job safe?
Show answer
The database cannot be corrupted, because instance recovery rolls back any transaction whose commit record was not written to the redo log. The last few committed batches before the crash can be missing, since NOWAIT returned before the redo was confirmed on disk. The job should end with a plain COMMIT (WAIT), which returns only after the redo is on disk, and should be restartable from a recorded position so a rerun reloads any missing batches.
Exercise
Which two commit options both guarantee that the redo is on disk when COMMIT returns, and what is the only difference in who decides when LGWR writes?
Show answer
COMMIT WRITE IMMEDIATE WAIT and COMMIT WRITE BATCH WAIT. With IMMEDIATE, the commit asks LGWR to write right away. With BATCH, LGWR chooses when to write and may combine several commits. In both cases WAIT makes the session block until the write completes, so the call returns only after the redo is durable.