A database instance is a set of memory structures that manage database files.
A database is a set of physical files on disk created by the CREATE DATABASE statement. The instance manages its associated data and serves the users of the database.
Because an instance exists in memory and a database exists on disk, an instance can exist without a database and a database can exist without an instance.
When an instance is started, Oracle Database allocates a memory area called the system global area (SGA) and starts one or more background processes.
Startup/shutdown是对实例操作而不是数据库。在RAC中实例各自startup/mount/open
A database instance begins when it is created with the STARTUP command and ends when it is terminated. During this period, a database instance can associate itself with one and only one database.
12.2 RAC可以设置read only实例
Every database instance is either read/write or read-only.
In previous releases, all database instances—unless they accessed a standby database—were read/write. Starting in Oracle Database 12c Release 2 (12.2), read-only and read/write instances can co-exist within a single database. This configuration is useful for parallel SQL statements that both query and modify data, because both read/write and read-only instances can query, while the read/write instances modify.
Unlike read/write instances, read-only instances have the following characteristics:
- Can only open a database that has already been opened by a read/write instance
- Disable many background processes, including the checkpoint and archiver processes, which are not necessary
- Can mount a disabled redo thread or a thread without any online redo log
- Data files can be taken offline and online. However, you cannot take permanent tablespaces offline.
- Offline data files and tablespaces can be recovered.
- The control file remains available for updates about the state of the database.
- Temporary tablespaces created with the CREATE TEMPORARY TABLESPACE statement are read/write.
- Writes to operating system audit trails, trace files, and alert logs can continue.
To designate an instance as read-only, set the INSTANCE_MODE initialization parameter to READ_ONLY. The default value of the parameter is READ_WRITE.
(一)Database Instance Startup and Shutdown
- How an Instance Is Started
The stages are as follows:
- Searches for a server parameter file in a platform-specific default location and, if not found, for a text initialization parameter file (specifying STARTUP with the SPFILE or PFILE parameters overrides the default behavior)
- Reads the parameter file to determine the values of initialization parameters
- Allocates the SGA based on the initialization parameter settings
- Starts the Oracle background processes
- Opens the alert log and trace files and writes all explicit parameter settings to the alert log in valid parameter syntax
- How a Database Is Mounted
To mount the database, the instance obtains the names of the database control files specified in the CONTROL_FILES initialization parameter and opens the files. Oracle Database reads the control files to find the names of the data files and the online redo log files that it will attempt to access when opening the database.
When you open the database, Oracle Database performs the following actions:
- Opens the online data files in tablespaces other than undo tablespaces
- Acquires an undo tablespace
- Opens the online redo log files
(二)Database and Instance Shutdown
Note: In a CDB, issuing SHUTDOWN ABORT on a PDB is equivalent to issuing SHUTDOWN IMMEDIATE on a non-CDB.
- How a Database Is Closed
How a Database Is Closed During Normal Shutdown
When a database is closed as part of a SHUTDOWN with any option other than ABORT, Oracle Database writes data in the SGA to the data files and online redo log files.
Afterward, the database closes online data files and online redo log files. Any offline data files of offline tablespaces have been closed already. When the database reopens, any tablespace that was offline remains offline.
How a Database Is Closed During Abnormal Shutdown
In an abnormal shutdown, Oracle Database does not write data in the buffers of the SGA to the data files and redo log files. The subsequent reopening of the database requires instance recovery, which Oracle Database performs automatically.
After a database is unmounted, Oracle Database closes the control files of the database. At this point, the database instance remains in memory.
When the database instance shuts down, the SGA ceases to occupy memory, and the background processes terminate.
有时oracle不能shutdown clearly会残存内存或进程未移除,这时在下次启动数据库时会失败,你可以在启动前执行shutdown abort或手动清理下
In unusual circumstances, shutdown of a database instance may not occur cleanly. Memory structures may not be removed from memory or one of the background processes may not be terminated. When remnants of a previous instance exist, a subsequent instance startup may fail. In such situations, you can force the new instance to start by removing the remnants of the previous instance and then starting a new instance, or by issuing a SHUTDOWN ABORT statement.
另外有时清理进程自身可能遇到问题,导致PMON或实例中止
In some cases, process cleanup itself can encounter errors, which can result in the termination of process monitor (PMON) or the instance.
The dynamic initialization parameter INSTANCE_ABORT_DELAY_TIME specifies how many seconds to delay an internally generated instance failure. This delay gives you a chance to respond. The database writes a message to the alert log when the delayed termination is initiated.
Oracle是根据 redo threads的状态来决定是否需要进行instance recovery
A redo thread is a record of all of the changes generated by an instance. A single-instance database has one thread of redo, whereas an Oracle RAC database has multiple redo threads, one for each database instance.
Whether instance recovery is required depends on the state of the redo threads.
A redo thread is marked open in the control file when a database instance opens in read/write mode, and is marked closed when the instance is shut down consistently. If redo threads are marked open in the control file, but no live instances hold the thread enqueues corresponding to these threads, then the database requires instance recovery.
Oracle Database performs instance recovery automatically in the following situations:
- The database opens for the first time after the failure of a single-instance database or all instances of an Oracle RAC database. This form of instance recovery is also called crash recovery. Oracle Database recovers the online redo threads of the terminated instances together.
- Some but not all instances of an Oracle RAC database fail. Instance recovery is performed automatically by a surviving instance in the configuration.
The SMON background process performs instance recovery, applying online redo automatically. No user intervention is required.
- reapplies all changes recorded in online redo log to rolling forward
这里是从最近一次checkpoint开始应用日志
Oracle Database uses the checkpoint position, which guarantees that every committed change with an SCN lower than the checkpoint SCN is saved on disk.
undo的更改也会记录到redo, 所以会同时恢复undo表空间
- 通过检查redo log记录中未提交的事务,找出其指向的undo blocks来进行回滚
Oracle Database can roll back multiple transactions simultaneously as needed. All transactions that were active at the time of failure are marked as terminated. Instead of waiting for the SMON process to roll back terminated transactions, new transactions can roll back individual blocks themselves to obtain the required data.