0% found this document useful (0 votes)
140 views4 pages

Internal Locks

Oracle internal_locks

Uploaded by

Daniel Craig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views4 pages

Internal Locks

Oracle internal_locks

Uploaded by

Daniel Craig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Oracle

Professional

Internals of Oracle
Database Locks
Riyaj Shamsudeen
In this article, Riyaj Shamsudeen explains various data Holding a latch, starting from the bucket, the hash chain
structures associated with locks and provides a cursory is searched for the specific resource.
overview of some locking-related initialization parameters. These arrays are fixed arrays and are statically
allocated during the startup and don’t grow dynamically.

L
OCKS (also known as “enqueues”) are used to The length of the resources array is determined by the
control the access to resources. Example of resources enqueue_resources parameter. The length of the locks
include rows, tables, redo threads, media recovery array is determined by the _enqueue_locks parameter.
threads, and so forth. The number of hash buckets is determined by the
_enqueue_hash parameter. The number of latches
Internal operation of locks protecting these hash buckets is determined by the
Before we dive in to the details of the internal locking _enqueue_hash_chain_latches parameter.
behavior, it’s important to understand the terminology
that’s used in this article. x$ tables
These fixed arrays are externalized as x$ tables. The
What’s a resource identifier? resources array is externalized as x$ksqrs fixed table, and
When a session needs access to a particular resource, it the locks array is externalized as x$ksqeq fixed table.
requests a lock on that resource in a specific mode. The
mode requested depends upon the intended operation. Tracking the resources
Resources are identified with the format {<two char When a session requests a lock on a resource, a unique
resource type >, id1, id2}. resource identifier is generated of the format (type, id1,
This two-character type identifies the type of the id2). This resource identifier is hashed in to the enqueues
resource for which a lock has been requested. For hash table to determine the status of the resource.
example, TX identifies a transaction lock and is used Holding one of the enqueue_hash_chain latches, this hash
for row-level locking. Id1 and id2 identify the specific chain is searched for the specific resource (see Figure 1).
resource within that resource type.
Another example is MR locks. MR is the resource type
for the Media Recovery, and DBW0 holds locks on every
datafile on this resource. Id1 is the file#.
A combination of the lock type, id1, and id2 is unique,
and each resource will have a unique resource identifier.

Arrays and hash chains


A few arrays and a hash chain are used to track the
enqueues and locks. The resources array is an array of
resource structures. A resource structure is used to track a
single resource, at any given point in time. Similarly, a
locks array is used to track an array of lock structures.
A hash array is used to improve the performance of
the enqueue lookup operation. There are buckets in this
hash array, and each hash bucket is pointing to a hash
chain. A hash chain is a linked list of resource structures,
and the head of the linked list is linked with the hash
bucket. Since these structures need to be protected during
the read/write operations to this array, latches are used. Figure 1. Hash buckets and resources.

14 Oracle Professional May 2001 http://www.oracleprofessionalnewsletter.com


Now depending upon whether that particular queue of the resource structure.
resource has been locked or not, the operation differs. Processes waiting for the resources sleep on the
semaphores, and semaphores (or the post wait driver on a
Resource available few platforms) are used as sleep/wakeup mechanisms.
If the resource isn’t in the hash chain, then no process is After enqueueing in to the queue, the requesting process
holding a lock on the resource. So the requesting session will sleep on the semaphore using the sync_op call.
can take a lock on this resource. One of the free array
elements from the resources array is selected, and it’s sync_op(SYNC_WAIT, SYNCF_BINARY, 300) = 1

linked with a bucket of the hash array.


Now the resource has been set up, and a lock on this Once the process holding the resource is ready to
resource needs to be set up as well. An element from the release the resource, it looks at the queue attached to the
locks array is linked with the resource structure in the resource structure. If there’s a process in the queue, it
owner’s queue, and proper lock properties are set up. sends a semaphore signal to the waiting process using
These lock structures are implemented as queue (as a sync_op call.
two-way linked list). These lock structures are linked
sync_op(0x0005, SYNCF_BINARY, 134491620) = 1
with the resource structure connected by the linked list
(see Figure 2). The waiting process will handle the signal and will
Furthermore, all of these structures are embedded in wake up. This waiting process will modify the status of
specialized state objects for PMON’s recovery. the resource structure, unlink its own lock structure,
and proceed.
Resource unavailable
If the resource is unavailable, then the resource is already Other features
allocated. If the resource is in the hash chain, then there’s There are a few other features available to improve the
no need to allocate a new resource structure. But a new performance of lock operations.
lock structure needs to be allocated. The requested mode
is checked with the mode held. If the resource is locked in Vector post
an incompatible state, then a new lock structure is _use_vector_post is implemented in a few platforms.
selected from the lock array and attached to the waiters This is implemented using the
SYNC_POSTVEC option of the
sync_op call. It’s used to signal
multiple processes sleeping on
semaphores to be signaled in a single
sync_op call. Certain background
processes can take advantage of this
feature and can pass the list of PIDs
or process handles to be signaled.
With this implementation, multiple
processes can be awakened using a
single sync_op call. Since numerous
calls to the sync_op call are avoided,
and the resulting context switch
overhead is also avoided, there’s a
slight performance improvement.

Post wait drivers


Some platforms support the
post_wait_driver in place of
semaphores. Semaphores are
implemented using the OS system
calls, and each operation on the
semaphore involves system calls.
Hence, context switches take place
for each of these calls.

Figure 2. Resources, locks, and queues. Continues on page 19

http://www.oracleprofessionalnewsletter.com Oracle Professional May 2001 15


Oracle Database Locks... Enqueue frequency and distribution
To find the frequency and distribution of enqueue waits,
Continued from page 15 you could use the x$ksqst table. This table lists various
Post_wait_drivers are implemented in user space, without enqueue types and the number of times a process has
the need for a kernel context switch, to minimize the waited for this resource. Since this doesn’t list the
overhead associated with process signaling and control. impact of wait on these enqueues, use this information
with caution.
Resource limit
To determine whether there are enough slots in the All right, how do I use this information?
resources array, you need to query the v$resource_limit. Now we’ll take a look at some real-world applications of
The enqueue_resources parameter is usually over- the information I’ve presented here.
allocated by the DBA, and each resource structure
takes around 72 bytes of memory. The _enqueue_locks Tuning
parameter is derived and best left undisturbed. Knowledge of internal operation is a key to performance
tuning. If your application has locking contention issues,
Riyaj, this line is too long—

Enqueue dumps then you might be able to use this information to pinpoint
You can dump the enqueues with the following command: the problem area and tune it. For example, to find the
how should it break?

breakup of enqueues, you could use x$ksqst table. If you


Alter session set events 'immediate trace name enqueues have waits on the enqueues hash chain latches, then you
level 3'
could increase the enqueues_hash_chain latches to
Here are details for various levels for the dump: decrease contention and improve performance.
At times, LGWR has to post many processes using
Level 1 enqueues hash table many semaphore calls. Vector posting can be enabled if
Level 2 hash table + resources that’s the case to improve the performance.
Level 3 hash table + resources + locks + queues

AD
A GE
LF
P
HA

http://www.oracleprofessionalnewsletter.com Oracle Professional May 2001 19


ORA-52, ORA-53 errors Riyaj Shamsudeen is a certified Oracle DBA, currently working for i2
If you receive either of these errors, it indicates that technologies. He’s worked with numerous versions of Oracle and many
you ran out of slots in the resources or locks array, flavors of UNIX. Performance tuning is his favorite part of the job.
[email protected].
respectively. Check the v$resource_limit table and
increase the size of the resources and locks array using
the enqueue_resources and _enqueue_locks init.ora
parameters, if necessary.

Disclaimer
Oracle resource locking and management is a complex
subject. Due to space constraints, some details have been
omitted. Additionally, I can only speak to the relevance of Sign up now for Pinnacle’s FREE eNewsletters!
the information in this article as it relates to the Sun, Get tips, tutorials, and news from gurus in the field
Sequent, and HP platforms. However, other UNIX delivered straight to your Inbox.
platforms can probably be expected to provide similar
http://www.FREEeNewsletters.com
services and functionality as outlined here. The Windows
NT platform might introduce other problems or subtleties XML • Web Development • SQL Server • Visual Basic • MS Access • Oracle • Visual C++ •
Delphi • FoxPro • XML • Web Development • SQL Server • Visual Basic • MS Access • Oracle
not explored in this article. ▲

Downloads May 2001 Source Code


• HOTKA05.ZIP—Source code to accompany Dan Hotka’s • FEUER05.ZIP—Source code to accompany Steven Feuerstein’s
article, “Geeky Internal Stuff: Rollback Segment Internals.” article, “Bulking up with Oracle8i PL/SQL—Part 2.”

Contributing Editors Steven Feuerstein, Dan Hotka;


Oracle Professional Subscription Information:1-800-788-1900 Publisher Robert Williford; Vice President/General
or http://www.oracleprofessionalnewsletter.com Manager Connie Austin; Executive Editor of IT Farion Grove;
Executive Editor Heidi Frost; Editorial Assistant Micki Bailey

Subscription rates: Direct all editorial, advertising, or subscription-


United States: One year (12 issues): $199 related questions to Pinnacle Publishing, Inc.:
Two years (24 issues): $358 1-800-788-1900 or 770-992-9401
Canada:* One year: $214; two years: $373 Fax: 770-993-4323
Other:* One year: $219; two years: $378 Pinnacle Publishing, Inc.
PO Box 769389
Single issue rate: $25 ($27.50 in Canada; $30 outside North America)*
Roswell, GA 30076-8220
Australian newsletter orders:
Ashpoint Pty., Ltd., 9 Arthur Street, E-mail: [email protected]
Dover Heights, N.S.W. 2030, Australia.
Phone: +61 2-9371-7399. Fax: +61 2-9371-0180. Pinnacle Web Site:
E-mail: [email protected] http://www.pinnaclepublishing.com
Internet: http://www.ashpoint.com.au
Oracle technical support:
* Funds must be in U.S. currency.
Call Oracle Corporation at 414-506-1500

Oracle Professional (ISSN 1525-1756) is published monthly (12 times Oracle, Oracle CASE Dictionary, and Oracle CASE Designer are or implied, respecting the contents of this publication, including
per year) by Pinnacle Publishing, Inc., 1000 Holcomb Woods Pkwy, registered trademarks and Oracle 7, Cooperative Development but not limited to implied warranties for the publication,
Bldg 200, Suite 280, Roswell, GA 30076. Environment, Oracle Forms, PL/SQL, Oracle Forms Generator, Oracle performance, quality, merchantability, or fitness for any particular
Reports Generator, Oracle CASE, Oracle Reports, Oracle Graphics, purpose. Pinnacle Publishing, Inc., shall not be liable to the purchaser
POSTMASTER: Send address changes to Oracle Professional, PO Box and Oracle Access are trademarks of Oracle Corporation. Other or any other person or entity with respect to any liability, loss, or
769389, Roswell, GA 30076-8220. brand and product names are trademarks or registered trademarks damage caused or alleged to be caused directly or indirectly by
of their respective holders. this publication. Articles published in Oracle Professional reflect
Copyright © 2001 by Pinnacle Publishing, Inc. All rights reserved. No the views of their authors; they may or may not reflect the view
part of this periodical may be used or reproduced in any fashion This publication is intended as a general guide. It covers a highly of Pinnacle Publishing, Inc. Inclusion of advertising inserts does
whatsoever (except in the case of brief quotations embodied in technical and complex subject and should not be used for making not constitute an endorsement by Pinnacle Publishing, Inc. or
critical articles and reviews) without the prior written consent of decisions concerning specific products or applications. This Oracle Professional.
Pinnacle Publishing, Inc. Printed in the United States of America. publication is sold as is, without warranty of any kind, either express

The Source Code portion of the Oracle Professional Web site is available to paid subscribers only.
Log in for access to all current and archive content and source code. For access to this issue only,
User name iodine
go to www.oracleprofessionalnewsletter.com, click on “Source Code,” select the file(s) you want
from this issue, and enter the User name and Password at right when prompted. Password jealous

20 Oracle Professional May 2001 http://www.oracleprofessionalnewsletter.com

You might also like