Guaranteed undo retention, meaning that undo data will never be overwritten until the time specified by the undo retention has passed, is enabled at the tablespace level.. Once you activ
Trang 1If you are using UNDO_MANAGEMENT=AUTO, you must also specify UNDO_ TABLESPACE This parameter nominates a tablespace, which must have been created
as an undo tablespace, as the active undo tablespace All the undo segments within it will be brought online (that is, made available for use) automatically
Lastly, UNDO_RETENTION, set in seconds, is usually optional It specifies a target for keeping inactive undo data and determines when it becomes classified as expired rather than unexpired If, for example, your longest running query is thirty minutes, you would set this parameter to 1800 Oracle will then attempt to keep all undo data for at least 1800 seconds, and your query should therefore never fail with ORA-1555
If, however, you do not set this parameter, or set it to zero, Oracle will still keep data for as long as it can anyway The algorithm controlling which expired undo data is overwritten first will always choose to overwrite the oldest bit of data; therefore, UNDO_RETENTION is always at the maximum allowed by the size of the tablespace Where the UNDO_RETENTION parameter is not optional is if you have configured guaranteed undo retention The default mode of operation for undo is that Oracle will favor transactions over queries If the sizing of the undo tablespace is such that a choice has to be made between the possibility of a query failing with ORA-1555 and the certainty of a transaction failing with ORA-30036, Oracle will choose to let the transaction continue by overwriting undo data that a query might need In other words, the undo retention is only a target that Oracle will try to achieve But there may be circumstances when successful queries are considered more important than successful transactions An example might be the end-of-month billing run for a utilities company, when it might be acceptable to risk transactions being blocked for
a few hours while the reports are generating Another case is if you are making use of flashback queries, which rely on undo data
Guaranteed undo retention, meaning that undo data will never be overwritten until the time specified by the undo retention has passed, is enabled at the tablespace level This attribute can be specified at tablespace creation time, or an undo tablespace can
be altered later to enable it Once you activate an undo tablespace for which retention guarantee has been specified, all queries will complete successfully, provided they finish within the undo retention time; you will never have “snapshot too old” errors again The downside is that transactions may fail for lack of undo space
If the UNDO_RETENTION parameter has been set, and the datafile(s) making
up the undo tablespace is set to autoextend, then Oracle will increase the size of the datafile automatically if necessary to keep to the undo retention target This combination
of guaranteed undo retention and autoextending datafiles means that both queries and transactions will always succeed—assuming you have enough disk space If you don’t, the automatic extension will fail
A database might have one tablespace used in normal operations where undo retention is not guaranteed, and another to be used during month-end reporting where retention is guaranteed
Sizing and Monitoring the Undo Tablespace
The undo tablespace should be large enough to store the worst case of all the undo generated by concurrent transactions, which will be active undo, plus enough unexpired
Trang 2Chapter 8: DML and Concurrency
357
undo to satisfy the longest running query In an advanced environment, you may also
have to add space to allow for flashback queries as well The algorithm is simple:
calculate the rate at which undo is being generated at your peak workload, and multiply
by the length of your longest query
The V$UNDOSTAT view will tell you all you need to know There is also an
advisor within Database Control that will present the information in an immediately
comprehensible way
Figure 8-8 shows the undo management screen of Database Control To reach this,
take the Server tab from the database home page, then the Automatic Undo Management
link in the Database Configuration section
The configuration section of the screen shows that the undo tablespace currently
in use is called UNDO1, and it is 100MB in size Undo guarantee has not been set,
but the datafile(s) for the tablespace is auto-extensible Making your undo datafiles
auto-extensible will ensure that transactions will never run out of space, but Oracle
will not extend them merely to meet the UNDO_RETENTION target; it is therefore
still possible for a query to fail with “snapshot too old.” However, you should not
rely on the auto-extend capability; your tablespace should be the correct size to begin
with The Change Tablespace button will issue an ALTER SYSTEM command to
activate an alternative undo tablespace
Further information given on the System Activity tab, shown in Figure 8-9, tells
you that the peak rate for undo generation was only 1664KB per minute, and the
Figure 8-8 Undo management settings, through Database Control
Trang 3longest running query was 25 minutes It follows that the minimum size of the undo tablespace to be absolutely sure of preventing errors would be, in kilobytes,
1664 * 25 = 40265
which is just over 40M If the current size were less than that, this would be pointed out in the Undo Advisor section There have been no transaction errors caused by lack
of undospace, and no query failures caused by lack of undo data
Creating and Managing Undo Tablespaces
So far as datafile management is concerned, an undo tablespace is the same as any other tablespace: files can be added, resized, taken online and offline, and moved or renamed But it is not possible to specify any options regarding storage: you cannot specify automatic segment space management, you cannot specify a uniform extent size To create an undo tablespace, use the keyword UNDO:
CREATE UNDO TABLESPACE tablespace_name
DATAFILE datafile_name SIZE size
[ RETENTION NOGUARANTEE | GUARANTEE ] ;
By default, the tablespace will not guarantee undo retention This characteristic can be specified at tablespace creation time, or set later:
ALTER TABLESPACE tablespace_name
Figure 8-9 Undo activity, summarized by Database Control
Trang 4Chapter 8: DML and Concurrency
359
EXAM TIP Unless specified at creation time in the datafile clause, the
datafile(s) of an undo tablespace will not be set to autoextend But if your
database is created with DBCA, it will enable automatic extension for the
undo tablespace’s datafile with maximum size unlimited Automatic extension
can be enabled or disabled at any time, as it can be for any datafile
It is not possible to create segments in an undo tablespace, other than the undo
segments that will be created automatically Initially, there will be a pool of ten undo
segments created in an undo tablespace More will be created if there are more than
ten concurrent transactions Oracle will monitor the concurrent transaction rate and
adjust the number of segments as necessary
No matter how many undo tablespaces there may be in a database, generally
speaking only one will be in use at a time The undo segments in this tablespace will
have a status of online (meaning that they are available for use); the segments in any
other undo tablespaces will have status offline, meaning that they will not be used If
the undo tablespace is changed, all the undo segments in the old undo tablespace will
be taken offline, and those in the new undo tablespace will be brought online There
are two exceptions to this:
• In a RAC database, every instance opening the database must have its own
undo tablespace This can be controlled by setting the UNDO_TABLESPACE
parameter to a different value for each instance Each instance will bring its
own undo segments online
• If the undo tablespace is changed by changing the UNDO_TABLESPACE
parameter, any segments in the previously nominated tablespace that were
supporting a transaction at the time of the change will remain online until
the transaction finishes
Two-Minute Drill
Describe Each Data Manipulation Language (DML)
Statement
• INSERT enters rows into a table
• UPDATE adjusts the values in existing rows
• DELETE removes rows
• MERGE can combine the functions of INSERT, UPDATE, and DELETE
• Even though TRUNCATE is not DML, it does remove all rows in a table
• It is possible for an INSERT to enter rows into multiple tables
• Subqueries can be used to generate the rows to be inserted, updated, or
deleted
• An INSERT, UPDATE, or DELETE is not permanent until it is committed
Trang 5• TRUNCATE removes every row from a table.
• A TRUNCATE is immediately permanent: it cannot be rolled back
Control Transactions
• A transaction is a logical unit of work, possibly comprising several DML statements
• Transactions are invisible to other sessions until committed
• Until committed, transactions can be rolled back
• A SAVEPOINT lets a session roll back part of a transaction
Manage Data Using DML
• All DML commands generate undo and redo
• Redo protects all changes to segments—undo segments, as well as data segments
• Server processes read from datafiles; DBWn writes to datafiles
Identify and Administer PL/SQL Objects
• Anonymous PL/SQL is stored on the client; stored PL/SQL in the data dictionary
• Procedures and functions can be packaged; triggers cannot be packaged
• PL/SQL code can call SQL code
Monitor and Resolve Locking Conflicts
• The default level of locking is row level
• Locks are required for all DML commands and are optional for SELECT
• A DML statement requires shared locks on the objects involved and exclusive locks on the rows involved
• A DDL lock requires an exclusive lock on the object it affects
• Deadlocks are resolved automatically
Overview of Undo
• All DML statements generate undo data
• Undo data is used for transaction rollback and isolation and to provide read consistency, and also for flashback queries
• Automatic undo management using undo segments is the default with
release 11g.
Trang 6Chapter 8: DML and Concurrency
361
Transactions and Undo Data
• Undo data will always be kept until the transaction that generated it
completes with a COMMIT or a ROLLBACK This is active undo.
• Undo data will be retained for a period after it becomes inactive to satisfy any
read consistency requirements of long running queries; this is unexpired undo.
• Expired undo is data no longer needed for read consistency and may be
overwritten at any time as space in undo segments is reused
Managing Undo
• An instance will use undo segments in one, nominated, undo tablespace
• More undo tablespaces may exist, but only one will be used at a time
• The undo tablespace should be large enough to take into account the
maximum rate of undo generation and the longest running query
• Undo tablespace datafiles are datafiles like any others
Self Test
1 Which of the following commands can be rolled back? (Choose all correct
answers.)
B DELETE
C INSERT
D MERGE
E TRUNCATE
F UPDATE
2 If an UPDATE or DELETE command has a WHERE clause that gives it a
scope of several rows, what will happen if there is an error partway through
execution? The command is one of several in a multistatement transaction
(Choose the best answer.)
A The command will skip the row that caused the error and continue
B The command will stop at the error, and the rows that have been updated
or deleted will remain updated or deleted
C Whatever work the command had done before hitting the error will be
rolled back, but work done already by the transaction will remain
D The whole transaction will be rolled back
Trang 73 Study the result of this SELECT statement:
SQL> select * from t1;
C1 C2 C3 C4
1 2 3 4
5 6 7 8
If you issue this statement:
insert into t1 (c1,c2) values(select c1,c2 from t1);
why will it fail? (Choose the best answer.)
A Because values are not provided for all the table’s columns: there should
be NULLs for C3 and C4
B Because the subquery returns multiple rows: it requires a WHERE clause to restrict the number of rows returned to one
C Because the subquery is not scalar: it should use MAX or MIN to generate scalar values
D Because the VALUES keyword is not used with a subquery
E It will succeed, inserting two rows with NULLs for C3 and C4
4 You want to insert a row and then update it What sequence of steps should you follow? (Choose the best answer.)
A INSERT, UPDATE, COMMIT
B INSERT, COMMIT, UPDATE, COMMIT
C INSERT, SELECT FOR UPDATE, UPDATE, COMMIT
D INSERT, COMMIT, SELECT FOR UPDATE, UPDATE, COMMIT
5 Which of these commands will remove every row in a table? (Choose one or more correct answers.)
A A DELETE command with no WHERE clause
B A DROP TABLE command
C A TRUNCATE command
D An UPDATE command, setting every column to NULL and with no WHERE clause
6 User JOHN updates some rows and asks user ROOPESH to log in and check the changes before he commits them Which of the following statements is true? (Choose the best answer.)
A ROOPESH can see the changes but cannot alter them because JOHN will have locked the rows
B ROOPESH will not be able to see the changes
C JOHN must commit the changes so that ROOPESH can see them and, if necessary, roll them back
D JOHN must commit the changes so that ROOPESH can see them, but only JOHN can roll them back
Trang 8Chapter 8: DML and Concurrency
363
7 There are several steps involved in executing a DML statement Place these in
the correct order:
A Apply the change vectors to the database buffer cache
B Copy blocks from datafiles into buffers
C Search for the relevant blocks in the database buffer cache
D Write the change vectors to the log buffer
8 When a COMMIT is issued, what will happen? (Choose the best answer.)
A All the change vectors that make up the transaction are written to disk
B DBWn writes the change blocks to disk
C LGWR writes the log buffer to disk
D The undo data is deleted, so that the changes can no longer be rolled back
9 What types of segment are protected by redo? (Choose all correct answers.)
A Index segments
B Table segments
C Temporary segments
D Undo segments
10 Which of these commands will terminate a transaction? (Choose all correct
answers.)
A CREATE
B GRANT
C SAVEPOINT
11 What type of PL/SQL objects cannot be packaged? (Choose the best answer.)
A Functions
B Procedures
C Triggers
D All PL/SQL objects can be packaged, except anonymous blocks
12 If several sessions request an exclusive lock on the same row, what will
happen? (Choose the best answer.)
A The first session will get the lock; after it releases the lock there is a
random selection of the next session to get the lock
B The first session will get an exclusive lock, and the other sessions will get
shared locks
C The sessions will be given an exclusive lock in the sequence in which they
requested it
D Oracle will detect the conflict and roll back the statements that would
otherwise hang
Trang 913 When a DML statement executes, what happens? (Choose the best answer.)
A Both the data and the undo blocks on disk are updated, and the changes are written out to the redo stream
B The old version of the data is written to an undo segment, and the new version is written to the data segments and the redo log buffer
C Both data and undo blocks are updated in the database buffer cache, and the updates also go to the log buffer
D The redo log buffer is updated with information needed to redo the transaction, and the undo blocks are updated with information needed to reverse the transaction
14 Your undo tablespace consists of one datafile on one disk, and transactions
are failing for lack of undo space The disk is full You have enabled retention guarantee Any of the following options could solve the problem, but which would cause downtime for your users? (Choose the best answer.)
A Create another, larger, undo tablespace and use alter system set undo_tablespace= to switch to it
B Move the datafile to a disk with more space, and use alter database resize datafile to make it bigger
C Reduce the undo_retention setting with alter system set undo_retention=
D Disable retention guarantee with alter tablespace retention guarantee
15 Examine this query and result set:
SQL> select BEGIN_TIME,END_TIME,UNDOBLKS,MAXQUERYLEN from V$UNDOSTAT; BEGIN_TIME END_TIME UNDOBLKS MAXQUERYLEN
- - - -02-01-08:11:35:55 02-01-08:11:41:33 14435 29 02-01-08:11:25:55 02-01-08:11:35:55 120248 296 02-01-08:11:15:55 02-01-08:11:25:55 137497 37 02-01-08:11:05:55 02-01-08:11:15:55 102760 1534 02-01-08:10:55:55 02-01-08:11:05:55 237014 540 02-01-08:10:45:55 02-01-08:10:55:55 156223 1740 02-01-08:10:35:55 02-01-08:10:45:55 145275 420 02-01-08:10:25:55 02-01-08:10:35:55 99074 120
The blocksize of the undo tablespace is 4KB Which of the following would be
the optimal size for the undo tablespace? (Choose the best answer.)
A 1GB
B 2GB
C 3GB
D 4GB
Trang 10Chapter 8: DML and Concurrency
365
Self Test Answers
1 þ B, C, D, and F These are the DML commands: they can all be rolled back.
ý A and E COMMIT terminates a transaction, which can then never be
rolled back TRUNCATE is a DDL command and includes a built-in COMMIT
2 þ C This is the expected behavior: the statement is rolled back, and the rest
of the transaction remains uncommitted
ý A, B, and D A is wrong because, while this behavior is in fact configurable,
it is not enabled by default B is wrong because, while this is in fact possible in
the event of space errors, it is not enabled by default D is wrong because only
the one statement will be rolled back, not the whole transaction
3 þ D The syntax is wrong: use either the VALUES keyword or a subquery, but
not both Remove the VALUES keyword, and it will run C3 and C4 would be
populated with NULLs
ý A, B, C, and E A is wrong because there is no need to provide values for
columns not listed B and C are wrong because an INSERT can insert a set of
rows, so there is no need to restrict the number with a WHERE clause or by
using MAX or MIN to return only one row E is wrong because the statement
is not syntactically correct
4 þ A This is the simplest (and therefore the best) way.
ý B, C, and D All these will work, but they are all needlessly complicated:
no programmer should use unnecessary statements
5 þ A and C The TRUNCATE will be faster, but the DELETE will get there too.
ý B and D B is wrong because this will remove the table as well as the rows
within it D is wrong because the rows will still be there—even though they
are populated with NULLs
6 þ B The principle of isolation means that only JOHN can see his
uncommitted transaction
ý A, C, and D A is wrong because transaction isolation means that no
other session will be able to see the changes C and D are wrong because a
committed transaction can never be rolled back
7 þ C, B, D, and A This is the sequence All others are wrong.
8 þ C A COMMIT is implemented by placing a COMMIT record in the log
buffer, and LGWR flushing the log buffer to disk
ý A, B, and D A is wrong because many of the change vectors (perhaps
all of them) will be on disk already B is wrong because DBWn does not
participate in commit processing D is wrong because the undo data may well
persist for some time; a COMMIT is not relevant to this