Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 6 ■ LOCKING AND LATCHING 217 USERNAME SID ID1 ID2 LMODE REQUEST BLOCK TYPE --------- ---- ------- ------ ----- ------- ----- ---- OPS$TKYTE 161 262151 16584 6 0 0 TX OPS$TKYTE 161 62074 0 3 0 0 TM OPS$TKYTE 161 62073 0 3 0 0 TM ops$tkyte@ORA10G> select object_name, object_id 2 from user_objects 3 where object_name in ('T1','T2') 4 / OBJECT_NAME OBJECT_ID ------------ ---------- T1 62073 T2 62074 Whereas we get only one TX lock per transaction, we can get as many TM locks as the objects we modify. Here, the interesting thing is that the ID1 column for the TM lock is the object ID of the DML-locked object, so it easy to find the object on which the lock is being held. An interesting aside to the TM lock: the total number of TM locks allowed in the system is configurable by you (for details, see the DML_LOCKS parameter definition in the Oracle Database Reference manual). It may in fact be set to zero. This does not mean that your database becomes a read-only database (no locks), but rather that DDL is not permitted. This is useful in very specialized applications, such as RAC implementations, to reduce the amount of intrainstance coordination that would otherwise take place. You can also remove the ability to gain TM locks on an object-by-object basis using the ALTER TABLE TABLENAME DISABLE TABLE LOCK command. This is a quick way to make it “harder” to accidentally drop a table, as you will have to re-enable the table lock before dropping the table. It can also be used to detect a full table lock as a result of the unindexed foreign key we discussed previously. DDL Locks DDL locks are automatically placed against objects during a DDL operation to protect them from changes by other sessions. For example, if I perform the DDL operation ALTERTABLE T, the table T will have an exclusive DDL lock placed against it, preventing other sessions from getting DDL locks and TM locks on this table. DDL locks are held for the duration of the DDL statement and are released immediately afterward. This is done, in effect, by always wrapping DDL statements in implicit commits (or a commit/rollback pair). For this reason, DDL always commits in Oracle. Every CREATE, ALTER, and so on statement is really executed as shown in this pseudo-code: Begin Commit; DDL-STATEMENT Commit; Exception When others then rollback; End;
218 CHAPTER 6 ■ LOCKING AND LATCHING So, DDL will always commit, even if it is unsuccessful. DDL starts by committing—be aware of this. It commits first so that if it has to roll back, it will not roll back your transaction. If you execute DDL, it will make permanent any outstanding work you have performed, even if the DDL is not successful. If you need to execute DDL, but you do not want it to commit your existing transaction, you may use an autonomous transaction. There are three types of DDL locks: • Exclusive DDL locks: These prevent other sessions from gaining a DDL lock or TM (DML) lock themselves. This means that you may query a table during a DDL operation, but you may not modify it in any way. • Share DDL locks: These protect the structure of the referenced object against modification by other sessions, but allow modifications to the data. • Breakable parse locks: These allow an object, such as a query plan cached in the Shared pool, to register its reliance on some other object. If you perform DDL against that object, Oracle will review the list of objects that have registered their dependence and invalidate them. Hence, these locks are “breakable”—they do not prevent the DDL from occurring. Most DDL takes an exclusive DDL lock. If you issue a statement such as Alter table t add new_column date; the table T will be unavailable for modifications during the execution of that statement. The table may be queried using SELECT during this time, but most other operations will be prevented, including all DDL statements. In Oracle, some DDL operations may now take place without DDL locks. For example, I can issue the following: create index t_idx on t(x) ONLINE; The ONLINE keyword modifies the method by which the index is actually built. Instead of taking an exclusive DDL lock, preventing modifications of data, Oracle will only attempt to acquire a low-level (mode 2) TM lock on the table. This will effectively prevent other DDL from taking place, but it will allow DML to occur normally. Oracle accomplishes this feat by keeping a record of modifications made to the table during the DDL statement and applying these changes to the new index as it finishes the CREATE. This greatly increases the availability of data. Other types of DDL take share DDL locks. These are taken out against dependent objects when you create stored, compiled objects, such as procedures and views. For example, if you execute Create view MyView as select * from emp, dept where emp.deptno = dept.deptno; share DDL locks will be placed against both EMP and DEPT, while the CREATE VIEW command is being processed. You can modify the contents of these tables, but you cannot modify their structure.
- Page 212 and 213: CHAPTER 5 ■ ORACLE PROCESSES 167
- Page 214 and 215: CHAPTER 5 ■ ORACLE PROCESSES 169
- Page 216 and 217: CHAPTER 5 ■ ORACLE PROCESSES 171
- Page 218 and 219: CHAPTER 5 ■ ORACLE PROCESSES 173
- Page 220 and 221: CHAPTER 5 ■ ORACLE PROCESSES 175
- Page 222 and 223: CHAPTER 5 ■ ORACLE PROCESSES 177
- Page 224 and 225: CHAPTER 5 ■ ORACLE PROCESSES 179
- Page 226 and 227: CHAPTER 5 ■ ORACLE PROCESSES 181
- Page 228 and 229: CHAPTER 6 ■ ■ ■ Locking and L
- Page 230 and 231: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 232 and 233: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 234 and 235: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 236 and 237: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 238 and 239: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 240 and 241: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 242 and 243: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 244 and 245: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 246 and 247: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 248 and 249: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 250 and 251: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 252 and 253: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 254 and 255: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 256 and 257: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 258 and 259: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 260 and 261: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 264 and 265: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 266 and 267: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 268 and 269: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 270 and 271: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 272 and 273: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 274 and 275: CHAPTER 6 ■ LOCKING AND LATCHING
- Page 276 and 277: CHAPTER 7 ■ ■ ■ Concurrency a
- Page 278 and 279: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 280 and 281: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 282 and 283: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 284 and 285: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 286 and 287: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 288 and 289: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 290 and 291: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 292 and 293: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 294 and 295: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 296 and 297: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 298 and 299: CHAPTER 7 ■ CONCURRENCY AND MULTI
- Page 300 and 301: CHAPTER 8 ■ ■ ■ Transactions
- Page 302 and 303: CHAPTER 8 ■ TRANSACTIONS 257 •
- Page 304 and 305: CHAPTER 8 ■ TRANSACTIONS 259 So,
- Page 306 and 307: CHAPTER 8 ■ TRANSACTIONS 261 X --
- Page 308 and 309: CHAPTER 8 ■ TRANSACTIONS 263 “s
- Page 310 and 311: CHAPTER 8 ■ TRANSACTIONS 265 busi
218<br />
CHAPTER 6 ■ LOCKING AND LATCHING<br />
So, DDL will always commit, even if it is unsuccessful. DDL starts by committing—be<br />
aware of this. It commits first so that if it has to roll back, it will not roll back your transaction.<br />
If you execute DDL, it will make permanent any outst<strong>and</strong>ing work you have performed, even<br />
if the DDL is not successful. If you need to execute DDL, but you do not want it to commit<br />
your existing transaction, you may use an autonomous transaction.<br />
There are three types of DDL locks:<br />
• Exclusive DDL locks: These prevent other sessions from gaining a DDL lock or TM<br />
(DML) lock themselves. This means that you may query a table during a DDL operation,<br />
but you may not modify it in any way.<br />
• Share DDL locks: These protect the structure of the referenced object against modification<br />
by other sessions, but allow modifications to the data.<br />
• Breakable parse locks: These allow an object, such as a query plan cached in the Shared<br />
pool, to register its reliance on some other object. If you perform DDL against that<br />
object, <strong>Oracle</strong> will review the list of objects that have registered their dependence <strong>and</strong><br />
invalidate them. Hence, these locks are “breakable”—they do not prevent the DDL from<br />
occurring.<br />
Most DDL takes an exclusive DDL lock. If you issue a statement such as<br />
Alter table t add new_column date;<br />
the table T will be unavailable for modifications during the execution of that statement. The<br />
table may be queried using SELECT during this time, but most other operations will be prevented,<br />
including all DDL statements. In <strong>Oracle</strong>, some DDL operations may now take place<br />
without DDL locks. For example, I can issue the following:<br />
create index t_idx on t(x) ONLINE;<br />
The ONLINE keyword modifies the method by which the index is actually built. Instead of<br />
taking an exclusive DDL lock, preventing modifications of data, <strong>Oracle</strong> will only attempt to<br />
acquire a low-level (mode 2) TM lock on the table. This will effectively prevent other DDL from<br />
taking place, but it will allow DML to occur normally. <strong>Oracle</strong> accomplishes this feat by keeping<br />
a record of modifications made to the table during the DDL statement <strong>and</strong> applying these<br />
changes to the new index as it finishes the CREATE. This greatly increases the availability of<br />
data.<br />
Other types of DDL take share DDL locks. These are taken out against dependent objects<br />
when you create stored, compiled objects, such as procedures <strong>and</strong> views. For example, if you<br />
execute<br />
Create view MyView<br />
as<br />
select *<br />
from emp, dept<br />
where emp.deptno = dept.deptno;<br />
share DDL locks will be placed against both EMP <strong>and</strong> DEPT, while the CREATE VIEW comm<strong>and</strong> is<br />
being processed. You can modify the contents of these tables, but you cannot modify their<br />
structure.