In Oracle, pinning packages refers to the practice of keeping frequently accessed packages, procedures, functions, triggers, or sequences in memory, specifically in the shared pool, to improve database performance. The shared pool is a component of the Oracle System Global Area (SGA) that stores parsed SQL statements, PL/SQL blocks, dictionary cache, and server handles among other things.
The term "pinning" comes from the concept that you're "pinning" an object in memory so it's not aged out over time by the least recently used (LRU) algorithm, which Oracle uses to manage memory in the shared pool.
Pinning packages and other objects can be beneficial for performance in some cases because it reduces the overhead of re-loading and re-parsing objects. When an unpinned package is called, if it is not already in the shared pool, it must be loaded from disk. This requires I/O operations and takes more time than retrieving it from memory. If the package is already in memory because it's been pinned, then the system can skip the loading process.
The DBMS_SHARED_POOL package in Oracle provides the capabilities for pinning and unpinning objects in the shared pool. The syntax to pin an object is as follows:
EXEC DBMS_SHARED_POOL.KEEP('SCOTT.EMP_PKG', 'P');
In the above example, 'SCOTT.EMP_PKG' is the name of the package, and 'P' is the type of the object (in this case, package).
However, it's important to note a couple of things about pinning:
Pinning isn't always necessary and can be overused. Oracle Database is very efficient at managing memory. In most cases, frequently accessed objects naturally remain in the shared pool because the LRU algorithm keeps recently used objects in memory.
Pinning consumes memory, and over-pinning can lead to issues such as shared pool fragmentation. Pinning should be used judiciously, only for objects that are large and are accessed frequently but not frequently enough to stay naturally in the shared pool.
As of Oracle Database 10g and later, Oracle introduced the concept of Automatic Shared Memory Management (ASMM) and Automatic Memory Management (AMM), which further reduce the need for manual pinning.
Thus, while pinning can be used as a technique to improve database performance, it should be used judiciously and as part of a broader performance optimization strategy. It's always a good idea to monitor and understand your database's behavior before implementing such changes.
Oracle Database 10g introduced Automatic Shared Memory Management, while Oracle Database 11g added Automatic Memory Management for the SGA and PGA instance components. Whenever the MEMORY_TARGET (new to Oracle Database 11g) or SGA_TARGET initialization parameter is set, the database automatically distributes the memory among various SGA components providing optimal memory management. The shared memory components automatically sized include the shared pool (manually set using SHARED_POOL_SIZE), the large pool (LARGE_POOL_SIZE), the Java pool (JAVA_POOL_SIZE), the buffer cache (DB_CACHE_SIZE), and the streams pool
(STREAMS_POOL_SIZE). Automatic memory management initialization parameters can be set through Oracle Enterprise Manager.
The background processes interact with the operating system and each other to manage the memory structures for the instance.
These processes also manage the actual database on disk and perform general housekeeping for the instance.
Figure 3-10 illustrates the memory structures and background processes discussed in the following section.