Skip to Main Content

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Sandeep.

Asked: December 05, 2016 - 4:39 pm UTC

Last updated: December 07, 2016 - 2:29 pm UTC

Version: 12c

Viewed 1000+ times

You Asked

Hi Connor,

I have no intention of complaining.
But all over the web I find lot of discussion about latch , latch spin, and latch sleep.
And the description goes like below.
1] Try to acquire a latch
2] Failed ! Try again after sometime
3] Retrying is known as Latch Spin
4] After the spin count it goes to sleep resulting in "latch free" wait

And from experts to novice, all has to say the above four points, to me it looks completely amalgamated!

On failing you are trying again? are you trying immediately? If after sometime, and that sometime is also accountable, why everybody silent about it.
What is this "SLEEP" is all about? And when actually this happens?

Tried once failed,
after some time you tried again failed...
''''''
''''''
So on ....
spin count reached, and you went to sleep?

I am not understanding! what is going on here and what is referred as spin count?

if spin count is 10
and sleep second is 5
and if the failure duration is one minute

How many spins will happen ?
How many sleeps will happen ?

Could you please help understanding.

Thanks and Regards,
Sandeep

and Connor said...

it is about the relative performance of cpu operations.

To try get a latch in a mode of:

- try to get latch
- sleep for a millisecond
- try again

then the sleep is an *eternity* in cpu terms. Just the act of coming "off" a cpu, and getting rescheduled to run is incredibly "slow" when compared to trying to get a latch (which is a low level atomic memory operation).

So if I am on the cpu (trying to get a latch), then I *dont want* to relinquish that cpu if possible. So I'll try lots of times to get that latch in a tight loop before giving up and sleeping.

Hence the term 'spin'.

Historically it was pretty simple - try and get the latch 'n' times then sleep. Things are more complicated now with the algorithm more sophisticated.

A good blog post is here on the topic:

https://andreynikolaev.files.wordpress.com/2016/03/latches_and_mutexes_in_oracle_12c.pdf

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Sandeep, December 06, 2016 - 10:17 am UTC

Hi Connor,

Thank you for the response.

One more confusion !

Just speaking in a crude way, about LOCK.
1] I want to modify a data
2] This block is not there in memory
3] I read the block into memory
4] Another person trying to modify the same
5] But the second person will find the data locked by me

This makes clear to some extent what is the use of LOCK

But I am not understanding at what point I am using the Latching mechanism, or in other words I know I am locking a db block, but when I am latching or locking a shared memory location ?

Is it something like the lock (transaction id) which is present in the block header, is escalating into a latch to protect the shared memory location, to which the block is read into, from modification. As one modification initiated by me is already in progress ?

Could you please help understanding ?

Thanks and Regards,
Sandeep

Connor McDonald
December 07, 2016 - 2:29 pm UTC

Each row on a table (in the block) has a "lock byte" - a single byte that indicates the active transaction on that block.

(which is why MAXTRANS is 255)

So when trying to lock a row, we can see from the lock byte that a row is locked.