Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!