Search This Blog

Wednesday, November 20, 2013

PROGRAM GLOBAL AREA (PGA)

Program Global Area (PGA):

The Program Global Area (PGA) is a private memory (non-shared) that contains the data and control information for a server process. Only a server process can access the PGA. The total PGA memory allocated by each server process attached to an Oracle instance is also referred to as the aggregated PGA allocated by the instance.

One PGA exists for each server process and background process. The collection of individual PGAs is the total instance PGA. We can query the v$process to see the memory aloocated to each PGA

Select    PNAME,  USERNAME,
               decode(BACKGROUND, '1', 'Y', 'N') “BG”,
                round(PGA_ALLOC_MEM/1024,0) "MemAlloc KB",
               round(PGA_USED_MEM/1024,0)   "MemUsed KB",
                round(PGA_FREEABLE_MEM/1024, 0) "MemFree KB",
                round(PGA_MAX_MEM/1024, 0) "MemMax KB"
from  v$process;

Contents of the PGA:

The contents of the PGA memory varies, depending on the instance is running the shared server option. The PGA memory can be classified as follows.

·         Private SQL Area
·         Session Memory
·         SQL Work Areas

Private SQL Area:

A private SQL area contains data such as bind information and runtime memory structures. When a server process executes SQL or PL/SQL code, the process uses the private SQL area to store bind variable values, query execution state information, and query execution work areas. 

The location of a private SQL area depends on the type of connection established for a session such as “Shared” or “Dedicated”. For Dedicated sessions a part of private SQL area resides in the PGA and for the Shared sessions the private SQL area resides in the library cache of the shared pool in SGA.

Each session that issues a SQL statement has a private SQL area and each user that submits the same SQL statement has own private SQL area that uses a single shared SQL area. Many private SQL areas can be associated with the same shared SQL area.

The private SQL area is itself divided into two areas whose lifetimes are different:

·         Run time Area
·         Persistent Area

A run time area is created as part of the query (DML) execution and closed as soon as the statement execution ends. It holds the current state of query execution information such as rows retrieved so far during the full table scans.

A persistent area contains bind variable values that are supplied to a SQL statement at run time, it’s freed when the cursor is closed. The persistent area is a part of the SGA (Large pool if configured) when the shared server model is used.

A client process is responsible for managing private SQL area but its allocation and de-allocation depends on the application and is limited by the server init parameter OPEN_CURSORS. 

A cursor is a name or handle to a specific private SQL area, in a nutshell cursor is nothing but private SQL area.

We can see cursors opened currently in database using following sql:

Show parameter open_cursors
--
select SID, USER_NAME, CURSOR_TYPE, last_sql_active_time
from v$open_cursor;

Session Memory (UGA):

Session memory is the memory allocated to hold logon information and other session details also known as User Global Area (UGA). The PGA is specific to a single process. Therefore, the UGA is stored in the SGA when using shared server connections, enabling any shared server process access to it. When using a dedicated server connection, the UGA is stored in the PGA.


SQL Work Areas:

A work area is a private allocation of PGA memory used for memory-intensive operations such as sorting, joining and merging. The following areas are allocated accordingly to the memory intensive operations.
  • sort_area_size: Total memory used to for sorting before swapping to disk
  • sort_area_retained_size : Memory that is used to retained data after a sort
  • hash_area_size: Memory used to build a hash tables or algorithms (table joins)
  • bitmap_merge_area_size: Memory used to merge bitmaps retrieved from a range scan of the index.

 For example, a sort operator uses the sort area to sort a set of rows. A hash join operator uses a hash area to build a hash table from input, whereas a bitmap merge uses the bitmap merge area to merge data retrieved from scans of multiple bitmap indexes.

If the amount of input data for any of the above operation is does not fit into the respected SQL work areas, then oracle divides them into smaller pieces and move it to the disk’s temporary storage (TEMP tablespace) for processing. This causes the increase in response time and reduces the performance, thus the allocation of PGA is key for the query performance.

The processing of above memory intensive operations if performed in memory it’s said to be an optimal mode of operation and if it performed on the disk then it’s one-pass/multi-pass (based on no. of packets sent to the disk for processing). If there are any one-pass/multi-pass operations we might want to increase the PGA memeory based on the frequency of the execution.

We can query the SQL work area views to see where the memory intensive operations are performed.

select   round(LAST_MEMORY_USED/1024/1024, 0) MEMUSEd_MB,
             TOTAL_EXECUTIONS, OPTIMAL_EXECUTIONS, ONEPASS_EXECUTIONS,
             MULTIPASSES_EXECUTIONS, ACTIVE_TIME/100/60 "Active(mins)",
              round(LAST_TEMPSEG_SIZE/1024/1024, 0) disk_MB,
              round(MAX_TEMPSEG_SIZE/1024/1024, 0) totaldisk_MB
from     v$sql_WORKAREA
where  sql_id='&sqlid';        

SELECT  s.sql_id, sum(ONEPASS_EXECUTIONS) onepass_cnt,
                  sum(MULTIPASSES_EXECUTIONS) mpass_cnt,
                  round(sum(LAST_TEMPSEG_SIZE)/1024/1024, 0)disk_MB,
                  round(sum(MAX_TEMPSEG_SIZE)/1024/1024, 0) totaldisk_MB
   FROM   V$SQL s, V$SQL_WORKAREA wa
WHERE   s.address = wa.address
GROUP BY s.sql_id
HAVING sum(ONEPASS_EXECUTIONS+MULTIPASSES_EXECUTIONS)>0;


Oracle automatically manages all the work areas/PGA on the whole by the setting of init parameter “pga_aggregate_target”.


Thursday, November 14, 2013

SYSTEM GLOBAL AREA or SHARED GLOBAL AREA (SGA)


A System Global Area (SGA) is a group of shared memory structures that contain data and control information for one Oracle database instance. If multiple users are concurrently connected to the same instance, then the data in the SGA is shared among the users. Consequently, the SGA is sometimes also called Shared Global Area.

SGA and Oracle background processes such as (pmon, smon, mmon, arch01, dbwr01 .....) constitutes an Oracle instance. Oracle automatically allocates memory for SGA when you start oracle instance using either PFILE or SPFILE. And the operating system reclaims the memory when you shut down the instance.

Each instance has its own SGA. The SGA is read/write all users connected to a multiple-process database instance can read information contained within the SGA, and several processes write to the SGA during execution of Oracle.

The SGA is allocated in memory and virtual memory.  The size of the SGA can be established by a DBA thru assigning a value to the parameter “SGA_MAX_SIZE” in the parameter file—this is an optional parameter. The Value of the SGA is also given at the time of Oracle Software Installation.

In Unix/Linux environments it is allocated in  “/etc/sysctl.conf”

       kernel.shmmax = 4294967295 (Bytes)
       kernel.shmmni = 4096

Checking the total and free memory (GB):

free -g
                            total              used       free     shared    buffers     cached
Mem:                   47                  34           12          0              0               6

To find the memory allocated to oracle:
                                                                
ipcs -m | grep ora  (*If the instance is not yet started, it won’t display anything)
key                  shmid         owner    perms      bytes               nattch     status
0x66e7a3e0    1245214    oracle     600         220200960      15
0x099288d0    2097183    oracle     600         6327107584*   56   

*5.89 G (allocated to oracle)

ipcs :Active Inter-Process Communication
m     :Shared Memory Segment
s       :Active Semaphores.
                                                           
To get the information of SGA from database:

Sql>  show  sga

Total System Global Area     6413680640 bytes
Fixed Size                                      2240376 bytes
Variable Size                          1157628040 bytes
Database Buffers                   5100273664 bytes                                     
Redo Buffers                             153538560 bytes


Sql> select   *  from v$sgainfo;

NAME                                                 BYTES              RESIZEABLE
--------------------------------------             -------------------   --------------------
Fixed SGA Size                                         2238528  No
Redo Buffers                                          140206080  No
Buffer Cache Size                               4328521728  Yes
Shared Pool Size                                1090519040  Yes
Large Pool Size                                       67108864  Yes
Java Pool Size                                         16777216  Yes
Streams Pool Size                                                   0  Yes
Shared IO Pool Size                                                0  Yes
Granule Size                                                6777216  No
Maximum SGA Size                             5645373440  No
Startup overhead in Shared Pool          342473512  No
Free SGA Memory Available                                   0

Memory can be managed in database via Automatic Shared Memory Management (ASMM) or Automatic Memory Management (AMM).

The main difference between ASMM and AMM is that AMM can manage SGA and PGA settings. ASMM can only manage the SGA.

Automatic Shared Memory Management (ASMM): is a pre 11g feature that automatically readjusts the sizes of the main pools (db_cache_size, shared_pool_size, large_pool_size, java_pool_size, Streams_pool) based on existing workloads.

When we set a value for SGA_TARGET, Oracle Database automatically sizes the most commonly configured components, including:
  • The shared pool (for SQL and PL/SQL execution)
  • The Java pool (for Java execution state)
  • The large pool (for large allocations such as RMAN backup buffers)
  • The buffer cache
  • The Streams pool
ASMM is enabled by setting following parameters:                      
  • You must use spfile for the initialization parameters.
  • sga_target parameter is set to a non-zero value
  • statistics_level parameter set to TYPICAL or ALL (Should not be BASIC)
  • shared_pool_size must be set to a non-zero value

sga_max_size:  sets the overall amount of memory the SGA can consume but is not dynamic. The sga_max_size parameter is the max allowable size to resize the SGA memory area parameters. The sga_target value can be adjusted up to the sga_max_size parameter, not more than sga_max_size parameter value.

Automatic Memory Management (AMM): is introduced in 11g enables the database to automatically tune the sizes of the individual SGA components and the sizes of the individual PGAs. AMM is the simplest way of managing the memory for the database.

AMM is enabled by setting these two initialization parameters “MEMORY_TARGET” & “MEMORY_MAX_TARGET” in spfile or pfile. The MEMORY_MAX_TARGET parameter specifies max size the MEMORY_TARGET can take memory from the system.

To avoid the commonly occur Error ORA-00845: MEMORY_TARGET not supported on this system on Linux system make sure to configure “/dev/shm” before enabling automatic memory management (AMM). dev/shm is nothing but implementation of traditional shared memory concept an efficient means of passing data between programs.

To control the minimum size of one or more automatically sized SGA components, set those component sizes to the desired value. Set the values of the other automatically sized SGA components to zero. Do this either in PFILE or SPFILE.

To monitor the all resize operations done on SGA and current size of SGA componenets run the following query respectively:


select   component, oper_type,  oper_mode,
              initial_size/1024/1024 "Initial",
              TARGET_SIZE/1024/1024  "Target",
              FINAL_SIZE/1024/1024   "Final",
               status
from      v$sga_resize_ops;

select    component,
              current_size/1024/1024 "CURRENT_SIZE",
              min_size/1024/1024 "MIN_SIZE",
              user_specified_size/1024/1024 "USER_SPECIFIED_SIZE",
              last_oper_type "TYPE"
from      v$sga_dynamic_components;