Oracle Redo Log
The redo log is the log that records every change Oracle makes to data blocks. A server process first writes the change to the redo log buffer in the SGA, and the LGWR process writes it to the online redo log files. A commit completes after that transaction's redo is written to the file, so even if the data blocks are not yet on disk, redo is reapplied after a failure to restore the committed changes.
Key specs
| Item | Value |
|---|---|
| Composition | At least 2 online redo log groups. Each group has at least 1 member file with the same content (multiplexing) |
| How it writes | When one group is full, moves to the next group (log switch), and after the last cycles back to the first |
| Writing process | LGWR. From 12c, the work is sometimes shared with LGnn helper processes |
| When LGWR writes | At commit, when the log buffer is one-third full, about every 3 seconds, before DBWn writes a block |
| Log buffer size | LOG_BUFFER. Usually set automatically |
| Archive | In ARCHIVELOG mode, ARCn copies a full group to an archive log |
| Group status | CURRENT (being written), ACTIVE (still needed for recovery), INACTIVE (reusable) |
| Order relative to data block writes | DBWn writes a block only after that block's redo has been written to disk (write-ahead logging) |
How it works
Figure. Cycling of online redo log groups and archiving
flowchart LR
B["Redo log buffer<br/>SGA"] -- "LGWR" --> G1["Group 1<br/>CURRENT"]
G1 -- "Log switch" --> G2["Group 2<br/>INACTIVE"]
G2 -- "Log switch" --> G3["Group 3<br/>INACTIVE"]
G3 -- "Log switch" --> G1
G3 -. "ARCn copy" .-> AR["Archive log<br/>For point-in-time recovery"]
Diagram notes: changes first accumulate in the Redo log buffer, and LGWR writes them to the group in CURRENT status. When the group fills, a log switch occurs and the next group becomes CURRENT. The previous group stays ACTIVE until every block that reflects its changes has been written to disk (checkpoint), then becomes INACTIVE and can be written again. In ARCHIVELOG mode, ARCn must copy a full group to an Archive log before that group can be overwritten.
Member multiplexing. The same content is written at the same time to all members of the same group. If members are placed on different disks, operation continues with the other members even if one file is damaged.
ARCHIVELOG mode. In NOARCHIVELOG, online redo logs are overwritten, so only instance failure recovery is possible, and media recovery or point-in-time recovery, which restores changes made after a backup, is not. Production databases are usually kept in ARCHIVELOG and recovered with RMAN backups and archive logs. Data Guard also ships redo to the standby database and applies it.
Sizing. If the groups are small, log switches and checkpoints become frequent. If a group whose archiving or checkpoint has not finished must be written again, sessions hit the log file switch (archiving needed) or log file switch (checkpoint incomplete) wait and all changes stall. The size is usually chosen so that a log switch occurs about once every 15 to 20 minutes even at busy times.
Limits. Redo is the information for reapplying a block's after-change value. The before-change value needed for rollback is in Undo, and changes to undo blocks are also protected by redo.
Configuration and checks
-- Groups, size, status
SELECT group#, thread#, sequence#, bytes / 1024 / 1024 AS mb, members, archived, status FROM v$log;
SELECT group#, member, status FROM v$logfile ORDER BY group#;
-- Archive mode
SELECT log_mode FROM v$database;
ARCHIVE LOG LIST; -- SQL*Plus
-- Log switches per hour
SELECT TRUNC(first_time, 'HH24') AS hour, COUNT(*) AS switches
FROM v$log_history WHERE first_time > SYSDATE - 1
GROUP BY TRUNC(first_time, 'HH24') ORDER BY 1;
-- Add a group and switch manually
ALTER DATABASE ADD LOGFILE GROUP 4 ('/u01/oradata/redo04a.log', '/u02/oradata/redo04b.log') SIZE 1G;
ALTER SYSTEM SWITCH LOGFILE;
Comparison with other DBMSs
| Oracle | PostgreSQL | InnoDB | SQL Server | |
|---|---|---|---|---|
| Name | Redo log | WAL | Redo log | Transaction log |
| File structure | Cycles through fixed-size groups | Writes fixed-size segment files in sequence | Cycles through a few fixed-size files | Cycles through one log file in units of virtual log files (VLF) |
| Retention | Archive logs in ARCHIVELOG mode | archive_command, archive_library | Keeps binary logs separately | Log backups |
| Before-change value | Separate Undo | Old row versions remain in the table | Separate undo log | Together inside the log |
In the articles
- 5.2 Log Records and the Log Buffer
- 5.3 The Rule of Writing the Log Before Data Pages
- 5.6 How DBMSs Differ, in One Line
- 6.4 Checkpoint: Writing Dirty Pages and the Recovery Starting Point
NOTE The conditions under which LGWR writes and the helper process configuration may differ by version.