COMMIT WRITE
By default, an Oracle commit completes only after LGWR has written all of that transaction's redo to the online redo log file. The COMMIT WRITE clause and the COMMIT_WAIT and COMMIT_LOGGING parameters change this behavior. Not waiting makes the commit response faster, but a commit that has already been acknowledged can disappear in a failure, so this gives up part of Durability (the D in ACID).
Key specs
| Item | Value |
|---|---|
| Statement level | COMMIT WRITE [WAIT | NOWAIT] [IMMEDIATE | BATCH] |
| Session and system level | COMMIT_WAIT (WAIT, NOWAIT, FORCE_WAIT), COMMIT_LOGGING (IMMEDIATE, BATCH) |
| Default | WAIT + IMMEDIATE: forces the redo to be written now and waits until it is fully written |
NOWAIT | Only requests the write from LGWR and responds immediately |
BATCH | Does not force the redo to be written now. Collects it in the buffer and writes it all at once |
FORCE_WAIT | Always waits, ignoring even the statement's NOWAIT (system level) |
| Range that can be lost | Commits not yet written to the redo file (within LGWR's write interval) |
| Related wait events | log file sync (the committing session), log file parallel write (LGWR) |
How it works
Figure. When the commit response arrives with WAIT and NOWAIT
sequenceDiagram
participant S as Session
participant L as LGWR
participant F as Redo log file
S->>L: COMMIT (WAIT)
L->>F: Write redo
F-->>L: Write complete
L-->>S: Commit complete response
S->>L: COMMIT WRITE NOWAIT
L-->>S: Immediate response
L->>F: Write redo later
Diagram explanation: With the default WAIT, the session waits in the log file sync wait state until the write to the Redo log file finishes, and then receives the response. With NOWAIT, the session hands the write to LGWR and receives the response right away. If the instance dies in between, the redo is not in the file even for a commit that was acknowledged, so it is rolled back during restart recovery. Data blocks are not corrupted. Only some of the most recent commits are lost.
IMMEDIATE and BATCH. IMMEDIATE makes LGWR write right away on every commit. BATCH keeps the redo in the buffer and writes it together with other writes. This reduces the number of writes in batches with very frequent commits. Even with the default WAIT, LGWR writes the redo of several sessions that wait at the same time in one operation (group commit), so with high concurrency the extra gain from BATCH shrinks.
Commits inside PL/SQL. Repeated commits inside a PL/SQL block are already optimized so that they do not wait for the write to complete until the block ends. Before the block returns to the user, it waits for the redo of the last commit.
When to use it. Consider it for bulk processing of reproducible data (intermediate load stages, log-type data) when log file sync accounts for most of the time. Do not use it for data that must not be lost, such as money or orders. If log file sync is long, first check the write latency of the redo log disk (log file parallel write) and the number of commits.
Settings and checks
COMMIT WRITE NOWAIT;
COMMIT WRITE BATCH NOWAIT;
ALTER SESSION SET commit_wait = 'NOWAIT';
ALTER SESSION SET commit_logging = 'BATCH';
SELECT name, value FROM v$parameter WHERE name IN ('commit_wait', 'commit_logging');
-- Commit wait and LGWR write time
SELECT event, total_waits, ROUND(time_waited_micro / NULLIF(total_waits, 0) / 1000, 2) AS avg_ms
FROM v$system_event WHERE event IN ('log file sync', 'log file parallel write');
Comparison with other DBMSs
| Oracle | PostgreSQL | InnoDB | SQL Server | |
|---|---|---|---|---|
| Setting | COMMIT WRITE NOWAIT, COMMIT_WAIT | synchronous_commit = off | innodb_flush_log_at_trx_commit = 0 or 2 | Delayed Durability |
| Scope | Statement, session, system | Transaction, session, system | System | Database, transaction |
| Range that can be lost | Commits LGWR has not yet written | The last few hundred milliseconds | About 1 second | Commits remaining in the log buffer |
In the articles
- 6.2 fsync: confirming the write reached persistent media
- 6.3 Group commit: several commits with one fsync
- 6.6 Differences between DBMSs and what you see in practice
NOTE Parameter names and the behavior of the PL/SQL commit optimization can vary by version.