Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 6 ■ LOCKING AND LATCHING 209 Lock Types The three general classes of locks in Oracle are as follows: • DML locks: DML stands for Data Manipulation Language. In general this means SELECT, INSERT, UPDATE, MERGE, and DELETE statements. DML locks are the mechanism that allows for concurrent data modifications. DML locks will be, for example, locks on a specific row of data or a lock at the table level that locks every row in the table. • DDL locks: DDL stands for Data Definition Language (CREATE and ALTER statements, and so on). DDL locks protect the definition of the structure of objects. • Internal locks and latches: Oracle uses these locks to protect its internal data structures. For example, when Oracle parses a query and generates an optimized query plan, it will “latch” the library cache to put that plan in there for other sessions to use. A latch is a lightweight, low-level serialization device employed by Oracle, similar in function to a lock. Do not confuse or be misled by the term “lightweight”—latches are a common cause of contention in the database, as you will see. They are lightweight in their implementation, but not in their effect. We will now take a more detailed look at the specific types of locks within each of these general classes and the implications of their use. There are more lock types than I can cover here. The ones I cover in the sections that follow are the most common and are held for a long duration. The other types of lock are generally held for very short periods of time. DML Locks DML locks are used to ensure that only one person at a time modifies a row and that no one can drop a table upon which you are working. Oracle will place these locks for you, more or less transparently, as you do work. TX (Transaction) Locks A TX lock is acquired when a transaction initiates its first change, and it is held until the transaction performs a COMMIT or ROLLBACK. It is used as a queuing mechanism so that other sessions can wait for the transaction to complete. Each and every row you modify or SELECT FOR UPDATE in a transaction will “point” to an associated TX lock for that transaction. While this sounds expensive, it is not. To understand why this is, you need a conceptual understanding of where locks “live” and how they are managed. In Oracle, locks are stored as an attribute of the data (see Chapter 10 for an overview of the Oracle block format). Oracle does not have a traditional lock manager that keeps a long list of every row that is locked in the system. Many other databases do it that way because, for them, locks are a scarce resource, the use of which needs to be monitored. The more locks are in use, the more these systems have to manage, so it is a concern in these systems if “too many” locks are being used. In a database with a traditional memory-based lock manager, the process of locking a row would resemble the following:
210 CHAPTER 6 ■ LOCKING AND LATCHING 1. Find the address of the row you want to lock. 2. Get in line at the lock manager (which must be serialized, as it is a common inmemory structure). 3. Lock the list. 4. Search through the list to see if anyone else has locked this row. 5. Create a new entry in the list to establish the fact that you have locked the row. 6. Unlock the list. Now that you have the row locked, you can modify it. Later, as you commit your changes you must continue the procedure as follows: 7. Get in line again. 8. Lock the list of locks. 9. Search through the list and release all of your locks. 10. Unlock the list. As you can see, the more locks acquired, the more time spent on this operation, both before and after modifying the data. Oracle does not do it that way. Oracle’s process looks like this: 1. Find the address of the row you want to lock. 2. Go to the row. 3. Lock the row (waiting for the transaction that has it locked to end if it is already locked, unless you are using the NOWAIT option). That’s it. Since the lock is stored as an attribute of the data, Oracle does not need a traditional lock manager. The transaction will simply go to the data and lock it (if it is not locked already). The interesting thing is that the data may appear locked when you get to it, even if it is not. When you lock rows of data in Oracle, the row points to a copy of the transaction ID that is stored with the block containing the data, and when the lock is released that transaction ID is left behind. This transaction ID is unique to your transaction and represents the rollback segment number, slot, and sequence number. You leave that on the block that contains your row to tell other sessions that you “own” this data (not all of the data on the block—just the one row you are modifying). When another session comes along, it sees the lock ID and, using the fact that it represents a transaction, it can quickly see if the transaction holding the lock is still active. If the lock is not active, the session is allowed access to the data. If the lock is still active, that session will ask to be notified as soon as the lock is released. Hence, you have a queuing mechanism: the session requesting the lock will be queued up waiting for that transaction to complete, and then it will get the data.
- Page 204 and 205: CHAPTER 5 ■ ORACLE PROCESSES 159
- Page 206 and 207: CHAPTER 5 ■ ORACLE PROCESSES 161
- Page 208 and 209: CHAPTER 5 ■ ORACLE PROCESSES 163
- Page 210 and 211: CHAPTER 5 ■ ORACLE PROCESSES 165
- 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 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 262 and 263: 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 •
CHAPTER 6 ■ LOCKING AND LATCHING 209<br />
Lock Types<br />
The three general classes of locks in <strong>Oracle</strong> are as follows:<br />
• DML locks: DML st<strong>and</strong>s for Data Manipulation Language. In general this means SELECT,<br />
INSERT, UPDATE, MERGE, <strong>and</strong> DELETE statements. DML locks are the mechanism that<br />
allows for concurrent data modifications. DML locks will be, for example, locks on a<br />
specific row of data or a lock at the table level that locks every row in the table.<br />
• DDL locks: DDL st<strong>and</strong>s for Data Definition Language (CREATE <strong>and</strong> ALTER statements, <strong>and</strong><br />
so on). DDL locks protect the definition of the structure of objects.<br />
• Internal locks <strong>and</strong> latches: <strong>Oracle</strong> uses these locks to protect its internal data structures.<br />
For example, when <strong>Oracle</strong> parses a query <strong>and</strong> generates an optimized query plan, it will<br />
“latch” the library cache to put that plan in there for other sessions to use. A latch is a<br />
lightweight, low-level serialization device employed by <strong>Oracle</strong>, similar in function to a<br />
lock. Do not confuse or be misled by the term “lightweight”—latches are a common<br />
cause of contention in the database, as you will see. They are lightweight in their implementation,<br />
but not in their effect.<br />
We will now take a more detailed look at the specific types of locks within each of these<br />
general classes <strong>and</strong> the implications of their use. There are more lock types than I can cover<br />
here. The ones I cover in the sections that follow are the most common <strong>and</strong> are held for a long<br />
duration. The other types of lock are generally held for very short periods of time.<br />
DML Locks<br />
DML locks are used to ensure that only one person at a time modifies a row <strong>and</strong> that no one<br />
can drop a table upon which you are working. <strong>Oracle</strong> will place these locks for you, more or<br />
less transparently, as you do work.<br />
TX (Transaction) Locks<br />
A TX lock is acquired when a transaction initiates its first change, <strong>and</strong> it is held until the<br />
transaction performs a COMMIT or ROLLBACK. It is used as a queuing mechanism so that other<br />
sessions can wait for the transaction to complete. Each <strong>and</strong> every row you modify or<br />
SELECT FOR UPDATE in a transaction will “point” to an associated TX lock for that transaction.<br />
While this sounds expensive, it is not. To underst<strong>and</strong> why this is, you need a conceptual<br />
underst<strong>and</strong>ing of where locks “live” <strong>and</strong> how they are managed. In <strong>Oracle</strong>, locks are stored as<br />
an attribute of the data (see Chapter 10 for an overview of the <strong>Oracle</strong> block format). <strong>Oracle</strong><br />
does not have a traditional lock manager that keeps a long list of every row that is locked in<br />
the system. Many other databases do it that way because, for them, locks are a scarce resource,<br />
the use of which needs to be monitored. The more locks are in use, the more these systems<br />
have to manage, so it is a concern in these systems if “too many” locks are being used.<br />
In a database with a traditional memory-based lock manager, the process of locking a row<br />
would resemble the following: