Chapter03 PDF
Chapter03 PDF
Oracle8i
Architecture
3
C H A P T E R
✦ ✦ ✦ ✦
In This Chapter
The processes that make up an Oracle instance allow you to change and retrieve
your data. Some of the processes also work to protect the integrity of your data and
ensure the recoverability of your data in the event of a system crash, loss of a disk,
or other unforeseen event. Most Oracle processes are referred to as background
processes because they are always running and they aren’t associated with any
particular user.
When an Oracle instance is running and a database is being used, the interaction
between the various processes, database files, and shared memory will look
something like the diagram in Figure 3-1.
v v v v
control
datafile datafile logfile logfile
file
Figure 3-1: A typical Oracle instance
N
o System Global Area
d
e
Database System Process Log
1 Writer Monitor Monitor Writer
v v v v
control
datafile datafile logfile logfile
file
v v v v
Note Oracle Parallel Server is available only with the Enterprise Edition of Oracle.
Because Oracle Parallel Server allows many instances to open a database, you are
somewhat insulated from problems that might occur if an instance crashes. In the
event of a crash, the other instances will continue to run. Users will still be able to
connect and get work done. In a stand-alone configuration, if an instance crashes,
everyone is dead in the water until you can restart it.
You can also use Oracle Parallel Server to increase your database’s throughput because
the load can be spread over several computers instead of just one. This works best if
you can partition your database tables and indexes in a way that minimizes the number
of times that two instances will require access to the same data.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 72
Parameter
File
The contents of
Control each control file are
Control
File identical File
Figure 3-3 shows the parameter file as a database file. Strictly speaking, that’s not
correct. However, understanding some points about the parameter file will help you
better understand how Oracle keeps track of control files and archived log files.
The next few sections discuss in detail each of the file types shown in Figure 3-3.
Even though the parameter file isn’t a database file, it is important. Without the
correct parameter file, you may not be able to open your database. Even if you did
manage to open a database using the wrong parameter file, it might not work the
way you expect because the parameter file settings greatly influence how an Oracle
instance functions. You will want to treat your parameter files with the same care as
you do your database files.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 73
Relative to database files, the parameter file performs two important functions. One
is to point to the control files for a database. The other is to point to the archive log
destination for a database. If you were to open the parameter file for an Oracle
database, you would see lines like this:
control_files = (“E:\Oracle\oradata\jonathan\control01.ctl”,
“F:\Oracle\oradata\jonathan\control02.ctl”)
log_archive_dest_1 =
“location=G:\Oracle\oradata\jonathan\archive”
The control_files entry shown here tells Oracle where to find the control files
for a database. Once an instance has found the control files, it can open those
files and read the locations of all the other database files. The second entry, for
log_archive_dest_1, tells Oracle where it should copy redo log files as they are
filled.
$ORACLE_HOME/dbs
The naming convention used for parameter files on UNIX systems is initXXXX.ora,
where “XXXX” represents the instance name. So if you have a database instance
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 74
named ORCL, which is the default name of Oracle’s starter database, your
parameter file would be named initORCL.ora.
C:\Oracle\Ora8i\Database
Not only is the directory path for parameter files different under Windows NT, but
the naming convention is different as well. For a database instance named ORCL, the
Windows NT version of Oracle expects the parameter file to be named
initORCL.ora.
The control file leads Oracle to the rest of the database files. When you start an
instance, Oracle reads the control file names and locations from the parameter file.
When you mount a database, Oracle opens the control file. When you finally open a
database, Oracle reads the list of database files from the control file and opens each
of them.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 75
control_files = (“E:\Oracle\oradata\jonathan\control01.ctl”,
“F:\Oracle\oradata\jonathan\control02.ctl”)
The control_file entry will list all the control files that were opened when the
database was started. Each control file contains identical information. If one is lost,
Oracle can continue by using the other.
A second way to find the control files for a database is to log on as the SYSTEM user,
or some other privileged user, and issue the following SELECT statement:
You can issue this query from SQL*Plus, as shown in this screen output example:
STATUS NAME
------- ----------------------------------------
E:\ORACLE\ORADATA\JONATHAN\CONTROL01.CTL
F:\ORACLE\ORADATA\JONATHAN\CONTROL02.CTL
Of course, for this second method to work, the instance must be running.
Using datafiles
Not surprisingly, Oracle stores your data in datafiles. Datafiles also typically
represent the bulk of an Oracle database in terms of the disk space that they use.
In terms of quantity also, you will probably have more datafiles than any other
type of file.
Datafile Contents
Datafiles contain the following types of data:
✦ Table data
✦ Index data
✦ Data dictionary definitions
✦ Information necessary to undo transactions (rollback data)
✦ Code for stored procedures, functions, and packages
✦ Temporary data, often used for sorting
For performance reasons, it’s usually best to separate data by type, each into its
own file or set of files, and place those files on separate disks. This is especially
true for data dictionary information, rollback data, and temporary data. You will
almost universally store these types of data separately from the others.
The following example shows the results you will get if you execute this query from
SQL*Plus. The two COLUMN commands format the output of the bytes and name the
columns to make the output more readable.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 77
7 rows selected.
The status column tells you whether Oracle has the file open. A status of ONLINE
or SYSTEM means that Oracle has the file open and that the data within that file is
accessible. A status of OFFLINE means that the file is closed. Files may be offline
because they were taken offline purposely by the DBA or because of a problem,
such as a drive failure, that makes the file inaccessible to Oracle. You cannot access
the data in an offline datafile.
The SYSTEM status indicates that a file is part of the system tablespace. The system
tablespace is the one that contains the data dictionary. You’ll learn more about
tablespaces in Chapter 6, “Database Space Management.” Oracle needs the data
dictionary to access the data in the other datafiles, so the system tablespace
always has to be open whenever the database is open.
There are two types of redo log files: online redo log files and archived redo log
files. Archived redo log files are sometimes referred to as offline redo log files. All
Oracle databases use online redo log files. Archived redo log files are only
generated when a database is run in archivelog mode, and allow for up-to-the-
minute recovery in the event that a datafile is lost.
table, one redo log entry might be enough to document that change. If you create
an index on a large table, Oracle generates a prodigious number of redo log entries
to describe the changes being made.
To protect the redo log from being lost, Oracle writes it as fast as possible to a set of
disk files known as redo log files. When you commit a transaction, Oracle waits until
all redo log entries for that transaction have been written to the log files before
telling you that the commit was successful. That way, you can’t possibly lose any
committed changes. Each Oracle database contains at least two redo log files, and
often more. Oracle writes to these files in a circular fashion, as shown in Figure 3-4.
Log file #1
Log file #2
Log file #4
current
log file
Log file #3
Figure 3-4: Oracle writes to the redo log files in a circular fashion.
Note As with control files, redo log files should always be mirrored. You can use hard-
ware mirroring if your system is capable of it, or you can have the Oracle software
mirror the files for you.
In a production setting, you should keep all redo log files that have been created
since the most recent full backup of the database. Do this by having Oracle copy
each redo log file as it is filled to a secure, long-term storage location. This process
is known as archiving a redo log file. This gets to the heart of what the redo log buys
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 79
you. Beginning with the most recent full backup of your database, the redo log files
(both archived and online) provide you with a history of all changes made up to the
current moment.
When you lose a disk drive, and someday you will, you’ll lose all the datafiles on
that drive. The redo log allows you to recover from what would otherwise be a
disastrous event by simply performing the following three steps:
Note There are alternatives to replacing the disk. You could just as easily restore the files
to a different disk and use the ALTER DATABASE RENAME FILE command to tell
Oracle about the new location.
Oracle automates the third step. If you’ve configured your system correctly, the
process of reapplying changes from the redo log is painless. All you have to do is
issue one short command, sit back, and watch.
SELECT member
FROM v$logfile
You can execute this query from SQL*Plus, as shown in this example:
MEMBER
----------------------------------------
E:\ORACLE\ORADATA\JONATHAN\REDO04.LOG
E:\ORACLE\ORADATA\JONATHAN\REDO03.LOG
E:\ORACLE\ORADATA\JONATHAN\REDO02.LOG
E:\ORACLE\ORADATA\JONATHAN\REDO01.LOG
Finding your offline, or archived, log files is a different matter. Technically, they
aren’t considered part of the database. However, Oracle does keep track of them.
You can query the v$archived_log view to get a list. You can find out the name of
the directory, or device, to which Oracle is copying the archived log files by issuing
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 80
an ARCHIVE LOG LIST command. Consider this example showing how to query the
v$archived_log view:
NAME
---------------------------------
D:\ORADATA\JONATHAN\ARCH_299.1
D:\ORADATA\JONATHAN\ARCH_300.1
D:\ORADATA\JONATHAN\ARCH_301.1
D:\ORADATA\JONATHAN\ARCH_302.1
D:\ORADATA\JONATHAN\ARCH_303.1
To use the ARCHIVE LOG LIST command, you have to connect as either SYSDBA or
SYSOPER, and issue the ARCHIVE LOG LIST command. Consider this example:
Note The archive destination that you see with the ARCHIVE LOG LIST command is
only the current destination. You can change this destination dynamically, but Oracle
won’t keep track of your changes. It only knows about the current destination.
Sharing memory this way allows the background processes to communicate with
each other quickly and minimizes the overhead of interprocess communication.
This contributes greatly to Oracle’s efficiency as a database.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 81
PGA
Server
Process
Ethernet Link
Client PC
Figure 3-5: All processes share the SGA, and each process
has its own PGA.
Database Buffer
Large Pool
Cache
Properly sizing the structures in the SGA is absolutely critical to proper database
performance. You control their size. To properly size the structures in the SGA, you
need to understand how they are used. The next few sections explain the use of
each SGA structure. For suggestions on sizing these structures, see Chapter 20,
“Database and Instance Tuning.”
The database buffer cache consists of a number of buffers in memory. The size of
each buffer matches the database block size. As blocks are read from disk, they
are placed into the buffers. The number of buffers in the buffer cache is controlled
by the db_block_buffers parameter. Thus, if your parameter file specifies
db_block_buffers = 8192, then Oracle will reserve enough memory to hold
8,192 database blocks.
Buffer Pools
The database buffer cache is frequently the largest part of the SGA. It consists of
three smaller structures known as buffer pools, each of which is used to cache data
with different access characteristics. Figure 3-7 shows these three buffer pools.
Default
Buffer Pool
Keep Recycle
Buffer Pool Buffer Pool
Two of the buffer pools, the keep buffer pool and the recycle buffer pool, are
optional. Every instance has at least one buffer pool — the default buffer pool. The
buffer pools serve the following purposes:
keep buffer pool Use the keep buffer pool for frequently accessed schema
objects, such as code tables, that you want to keep in
memory all the time. Data read into the keep buffer pool
is retained until you shut down the database. It is never
aged out of memory to make room for new data.
recycle buffer pool Use the recycle buffer pool for schema objects that you
want flushed out of memory as quickly as possible. A
large table that you frequently scan in its entirety
would be a good candidate for the recycle buffer pool.
default buffer pool Use the default buffer pool for all objects that don’t fall
into the keep or recycle category.
Note Prior to the release of Oracle8, Oracle supported only one buffer pool. It was the
equivalent of the default buffer pool, but it wasn’t named because there was no
need to distinguish it from other buffer pools.
db_block_buffers 8192
buffer_pool_keep 2000
buffer_pool_recycle 1000
----
size of DEFAULT pool 5192
In this example, the total size of the database buffer cache is 8,192 buffers. You set
this by using the db_block_buffers parameter. Of those 8,192 buffers, 2,000 were
allocated to the keep buffer pool. Another 1,000 were allocated to the recycle buffer
pool. This leaves 5,192 for the default buffer pool.
Here, the state_codes table was assigned to the keep buffer pool. This makes
sense because the state_codes table won’t be very big, and in all likelihood, it will
be a frequently referenced table.
Dirty List 13 2 5 3 12 7
Buffer Pool
1 2 3 4 5 6 7 8 9 10 11 12 13
Modified Buffers
Unmodified Buffers
LRU List 6 9 8 10 11 4 1
Oracle uses the LRU list to decide which buffers to overwrite when new data needs
to be read in from disk. The LRU list has two ends: a least recently used (LRU) end
and a most recently used (MRU) end. Every time a buffer is accessed to satisfy a
SQL statement, Oracle moves the pointer to that buffer to the most recently used
end of the LRU list. This results in the LRU list always containing a list of buffers in
the order in which they have recently been accessed.
When Oracle needs to read new data from disk, it starts at the least recently used
end of the LRU list and looks for a buffer that hasn’t been modified. When it finds
one, the newly read data is placed in that buffer. Pointers to frequently accessed
data blocks will tend to migrate to the most recently used end of the LRU list, and
consequently, they will be the last to be overwritten. Keeping the most frequently
used data in memory is a great asset in terms of performance. Memory is much
faster than disk.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 85
The dirty list is used to keep track of which buffers have been changed and need to
be written back to disk. Whenever a buffer is modified, usually as a result of a SQL
statement, Oracle will mark the buffer as dirty. Dirty buffers are quickly added to
the dirty list. The database writer background processes, which you will read more
about later in this chapter, check the dirty list regularly, and write those modified
blocks back to disk.
Note Buffers aren’t always added to the dirty list immediately when they are modified.
Oracle tends to perform processes asynchronously, and it is possible for dirty
buffers to remain in the LRU list for a short period of time. Ultimately, though,
they’ll be moved to the dirty list and then written to disk.
Shared Pool
Library Cache
PL/SQL Area
In addition to what is shown in Figure 3-9, the shared pool contains several
relatively minor items such as locks, library cache handles, and memory-for-
character-set conversion.
shared_pool_size = 50000000
You can also use M and K prefixes to specify the size in megabytes or kilobytes.
Oracle determines the size of all the structures within the shared pool automatically
based on the total size that you specify. You have no control over how much shared
pool space gets allocated to the library cache vs. the data dictionary cache. Oracle
determines that.
Think for a minute about what Oracle has to do when you execute a SQL statement.
First it has to parse the statement. Parsing refers to the process of taking the syntax
of the statement apart, verifying that it is correct, and validating that the table and
column names used in the statement are correct. Parsing can be an expensive
operation in terms of time and disk I/O because Oracle needs to read information
from the data dictionary to parse a SQL statement. To get that information, Oracle
actually issues SQL statements internally. These are referred to as recursive SQL
statements, and they too must be parsed.
After a statement has been parsed and Oracle understands what you want to do,
Oracle must then figure out how to do it. It must build an execution plan for the
statement. Figure 3-10 provides an example of one. Given the SELECT statement on
the left, Oracle might come up with the execution plan shown on the right. Building
the execution plan might involve even more recursive SQL statements, and Oracle
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 87
often needs to consider several possible plans before it can determine which is
most efficient.
Although all this parsing and execution-plan building is expensive, you can short-
circuit much of it. Typically, the programmers define the SQL statements used by
any given application when they write that application. You may have a lot of
people using an application, but they will all be executing the same SQL statements
over and over again. The people developing the Oracle software recognized that
they could gain efficiency by simply saving the parsed SQL statements together
with their execution plans. When the statement is next executed, Oracle simply
needs to retrieve the preexisting plan. What a savings! No parsing. No rebuilding
of the execution plan. No recursive SQL.
The shared SQL area is the part of the SGA that stores parsed SQL statements
and their execution plans. Being able to reuse execution plans is critical to good
database performance. Consequently, it’s important to size the shared pool so that
the shared SQL area is large enough to hold all the SQL statements that you use
regularly. Chapter 20, “Database and Instance Tuning,” provides some techniques
that you can use to determine whether your shared SQL area is large enough.
When you execute a PL/SQL program unit, such as a trigger or a stored procedure,
Oracle must load the compiled version of that program unit into memory. Sometimes,
especially with PL/SQL packages, a program unit can be quite large. If a second user
comes along and needs to execute the same trigger, stored procedure, or function,
you don’t want to have to load that same code into memory twice. Doing so costs
both disk I/O and memory usage. To avoid this, Oracle loads compiled PL/SQL code
into an area of the Library Cache set aside for that purpose. If two people execute the
same code, they will both share the same copy.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 88
Note While users share copies of PL/SQL code, they don’t share copies of the variables.
Each user actually gets his or her own private PL/SQL area where any variables are
stored. This is what enables many users to execute the same code without con-
flicting with one another.
As changes are made to the database, Oracle always adds redo entries onto the
head of the log, advancing the head pointer each time. The log writer process,
which writes these entries to disk, always writes from the tail of the log. Thus, the
tail chases the head in a circular fashion. In Figure 3-11, the buffer contains 11
entries. As each pointer advances past entry 11, it will be reset to point at entry 1,
and the process of advancing through the buffer will begin all over again. This
continues for as long as the database is running.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 89
log_buffer = 1024000
You want the redo log buffer to be large enough to accommodate normal bursts of
activity that occur during daily operations. Chapter 20, “Database and Instance
Tuning,” tells you how to monitor for problems related to the redo log buffer size.
If you don’t allocate a large pool, memory for user sessions and backup and restore
operations end up being allocated from the shared pool. Oracle may sometimes be
forced to reduce the amount of memory available for caching SQL statements to
allocate enough memory for a multithreaded server session or for a backup
operation. This can have a negative impact on performance.
large_pool_size = 600K
If you don’t explicitly set the large pool’s size using the large_pool_size
parameter, Oracle will default to not using a large pool at all.
The session information box is sometimes referred to as the user global area (UGA)
because it contains information specific to a particular database user connection.
When a multithreaded server is being used, the UGA is stored in either the shared
pool or the large pool because it must be accessible to more than one server
process.
Note Under the multithreaded server configuration, the server process handling a user’s
SQL statements may change from one statement to the next: hence, the need for
the UGA to be accessible to more than one process.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 91
Session
Information Stack Space
Note The maximum sort area space is not allocated unless required. If the sort area size
is 10MB, and the largest sort you do requires only 2MB, then only 2MB will be
allocated.
When you are using the multithreaded server option, the retained portion of the
sort area is allocated in the SGA, and the amount allocated in the PGA will be
equivalent to sort_area_size – sort_area_retained_size.
The sort area isn’t the only structure in the PGA, and the sort area parameters
aren’t the only ones that affect the size of the PGA. The open_links and db_files
parameters also affect the size of the PGA. However, as far as tuning goes, you need
to worry about the sort area parameters.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 92
Each of the processes shown in Figure 3-13 has a specific job to do. You’ll read
more about each process later. For now, here’s a brief synopsis of each process’s
function:
Not all processes will be present for every Oracle instance. Some of them are
optional, such as the SNP0 processes. You will see them only if you have set the
job_queue_processes initialization parameter to a value greater than zero.
Using the appropriate operating system commands, you can list the processes that
are running for any given Oracle instance. This is sometimes useful as a quick
check to be sure that the instance is up and running.
You can use a variation of this technique to quickly see which databases are
running. For instance, instead of using grep to search for an instance name, use
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 94
grep to search for one of the mandatory processes instead. You’ll get a line of
output for each instance that is currently running. Here’s an example:
This example searched for the SMON process. You could just as easily use grep for LGWR
or DBW0. Be careful with the numbered processes, though. If you use grep for DBW9 and
not all instances are configured to run nine database writers, you’ll miss some.
Figure 3-14: Oracle Administration Assistant for Windows NT showing the threads
in an Oracle instance
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 95
Note In order to use the Oracle Administration Assistant for Windows NT, you need to
install the Microsoft Management Console version 1.1. You can download it from
Microsoft’s website.
If you need to know which instances are running, your best bet is to look at the
Services control panel. It will tell you which Oracle database services are running,
although it’s still possible for an instance to be shut down even though its service is
running.
Every instance gets one Database Writer by default. You can change the number
of Database Writer processes by using the db_writer_processes initialization
parameter. To configure an instance for the full complement of ten Database Writer
processes, add the following line to the parameter file:
db_writer_processes = 10
Multiple Database Writers make sense only in a system with multiple CPUs because
they allow you to spread the task of writing data evenly over those CPUs. If you are
running on a single-CPU system, you should use just one Database Writer process.
Archiver processes
Archiver processes (ARC0) have the task of copying filled redo log files to the archive
log destination. An Oracle database has a finite number of redo log files, and Oracle
writes to these in a circular fashion. It fills up each online redo log file in sequence,
and when it gets to the end, it circles around and starts filling up the first redo log file
again. If you want to save the log files for possible use in recovering the database, you
need to make a copy of each log file before it is reused. You can do this manually, or
you can start one or more archiver processes to automate the process.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 96
As with database writers, Oracle allows you to have up to ten Archiver processes.
The log_archive_max_processes parameter is used to set the maximum number
that you want to allow. For example, the following entry in your database parameter
file will set a maximum of five Archiver processes:
log_archive_max_processes = 5
Unlike the case with database writers, Oracle won’t necessarily start five archiver
processes just because you tell it to. Instead, it automatically starts and stops
archiver processes as necessary to keep up with the amount of redo being
generated. As the name indicates, the log_archive_max_processes parameter
sets an upper limit on the number of archiver processes that Oracle can start.
✦ Crash recovery
✦ Cleanup of temporary segments
✦ Coalescing free space
Crash recovery happens when you restart the database after a system crash or an
instance crash. When a crash occurs, you will very likely lose changes that were
buffered in memory, but not written to disk, before the crash occurred. Some of
these changes might represent committed transactions. When the instance is
restarted, SMON recovers these transactions by reapplying the lost changes based
on the information in the online redo log files. This works because Oracle always
ensures that all changes for a committed transaction have been recorded in the
redo log and that those log entries have been physically written to disk.
Coalescing free space is the last of SMON’s major functions. For tablespaces with
a default PCTINCREASE setting that is greater than 0, SMON continuously checks
data files, looking for two or more adjacent areas of free space. Whenever it finds
adjacent areas of free space, SMON combines them into one larger area. This helps
avoid fragmentation and also lets you allocate larger extents that might not be
possible otherwise.
job_queue_processes = 36
The 36 job queue processes are named SNP0 through SNP9, and then SNPA
through SNPZ.
4623-6 ch03.f.qc 1/28/00 12:25 PM Page 98
aq_tm_processes = 3
Queue Monitor processes are named QMN0 through QMN9, depending on how many
you create.
Summary
In this chapter, you learned:
✦ ✦ ✦