Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 6 ■ LOCKING AND LATCHING 211 Here is a small example showing how this happens, using three V$ tables: • V$TRANSACTION, which contains an entry for every active transaction. • V$SESSION, which shows us the sessions logged in. • V$LOCK, which contains an entry for all enqueue locks being held as well as for sessions that are waiting on locks. You will not see a row in this view for each row locked in this table by a session. As stated earlier, that master list of locks at the row level doesn’t exist. If a session has one row in the EMP table locked, there will be one row in this view for that session indicating that fact. If a session has millions of rows in the EMP table locked, there will still be just one row in this view. This view shows what enqueue locks individual sessions have. First, let’s start a transaction (if you don’t have a copy of the DEPT table, simply make one using a CREATE TABLE AS SELECT): ops$tkyte@ORA10G> update dept set deptno = deptno+10; 4 rows updated. Now, let’s look at the state of the system at this point. This example assumes a single-user system; otherwise, you may see many rows in V$TRANSACTION. Even in a single-user system, do not be surprised to see more than one row in V$TRANSACTION, as many of the background Oracle processes may be performing a transaction as well. ops$tkyte@ORA10G> select username, 2 v$lock.sid, 3 trunc(id1/power(2,16)) rbs, 4 bitand(id1,to_number('ffff','xxxx'))+0 slot, 5 id2 seq, 6 lmode, 7 request 8 from v$lock, v$session 9 where v$lock.type = 'TX' 10 and v$lock.sid = v$session.sid 11 and v$session.username = USER; USERNAME SID RBS SLOT SEQ LMODE REQUEST --------- ---- --- ---- ------ ----- ------- OPS$TKYTE 145 4 12 16582 6 0 ops$tkyte@ORA10G> select XIDUSN, XIDSLOT, XIDSQN 2 from v$transaction; XIDUSN XIDSLOT XIDSQN ---------- ---------- ---------- 4 12 16582
212 CHAPTER 6 ■ LOCKING AND LATCHING The interesting points to note here are as follows: • The LMODE is 6 in the V$LOCK table and the request is 0. If you refer to the definition of the V$LOCK table in Oracle Server Reference manual, you will find that LMODE=6 is an exclusive lock. A value of 0 in the request means you are not making a request; you have the lock. • There is only one row in this table. This V$LOCK table is more of a queuing table than a lock table. Many people expect there would be four rows in V$LOCK since we have four rows locked. What you must remember, however, is that Oracle does not store a master list of every row locked anywhere. To find out if a row is locked, we must go to that row. • I took the ID1 and ID2 columns and performed some manipulation on them. Oracle needed to save three 16-bit numbers, but only had two columns in order to do it. So, the first column ID1 holds two of these numbers. By dividing by 2^16 with trunc(id1/power(2,16)) rbs, and by masking out the high bits with bitand(id1,➥ to_number('ffff','xxxx'))+0 slot, I am able to get back the two numbers that are hiding in that one number. • The RBS, SLOT, and SEQ values match the V$TRANSACTION information. This is my transaction ID. Now we’ll start another session using the same username, update some rows in EMP, and then try to update DEPT: ops$tkyte@ORA10G> update emp set ename = upper(ename); 14 rows updated. ops$tkyte@ORA10G> update dept set deptno = deptno-10; We’re now blocked in this session. If we run the V$ queries again, we see the following: ops$tkyte@ORA10G> select username, 2 v$lock.sid, 3 trunc(id1/power(2,16)) rbs, 4 bitand(id1,to_number('ffff','xxxx'))+0 slot, 5 id2 seq, 6 lmode, 7 request 8 from v$lock, v$session 9 where v$lock.type = 'TX' 10 and v$lock.sid = v$session.sid 11 and v$session.username = USER; USERNAME SID RBS SLOT SEQ LMODE REQUEST --------- ---- --- ---- ------ ----- ------- OPS$TKYTE 144 4 12 16582 0 6 OPS$TKYTE 144 5 34 1759 6 0 OPS$TKYTE 145 4 12 16582 6 0
- 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 254 and 255: 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 •
- Page 304 and 305: CHAPTER 8 ■ TRANSACTIONS 259 So,
CHAPTER 6 ■ LOCKING AND LATCHING 211<br />
Here is a small example showing how this happens, using three V$ tables:<br />
• V$TRANSACTION, which contains an entry for every active transaction.<br />
• V$SESSION, which shows us the sessions logged in.<br />
• V$LOCK, which contains an entry for all enqueue locks being held as well as for sessions<br />
that are waiting on locks. You will not see a row in this view for each row locked in this<br />
table by a session. As stated earlier, that master list of locks at the row level doesn’t exist.<br />
If a session has one row in the EMP table locked, there will be one row in this view for<br />
that session indicating that fact. If a session has millions of rows in the EMP table locked,<br />
there will still be just one row in this view. This view shows what enqueue locks individual<br />
sessions have.<br />
First, let’s start a transaction (if you don’t have a copy of the DEPT table, simply make one<br />
using a CREATE TABLE AS SELECT):<br />
ops$tkyte@ORA10G> update dept set deptno = deptno+10;<br />
4 rows updated.<br />
Now, let’s look at the state of the system at this point. This example assumes a single-user<br />
system; otherwise, you may see many rows in V$TRANSACTION. Even in a single-user system,<br />
do not be surprised to see more than one row in V$TRANSACTION, as many of the background<br />
<strong>Oracle</strong> processes may be performing a transaction as well.<br />
ops$tkyte@ORA10G> select username,<br />
2 v$lock.sid,<br />
3 trunc(id1/power(2,16)) rbs,<br />
4 bit<strong>and</strong>(id1,to_number('ffff','xxxx'))+0 slot,<br />
5 id2 seq,<br />
6 lmode,<br />
7 request<br />
8 from v$lock, v$session<br />
9 where v$lock.type = 'TX'<br />
10 <strong>and</strong> v$lock.sid = v$session.sid<br />
11 <strong>and</strong> v$session.username = USER;<br />
USERNAME SID RBS SLOT SEQ LMODE REQUEST<br />
--------- ---- --- ---- ------ ----- -------<br />
OPS$TKYTE 145 4 12 16582 6 0<br />
ops$tkyte@ORA10G> select XIDUSN, XIDSLOT, XIDSQN<br />
2 from v$transaction;<br />
XIDUSN XIDSLOT XIDSQN<br />
---------- ---------- ----------<br />
4 12 16582