What is PGA
The Program Global Area (PGA) is a private memory region that contains the data and control information for a server process. Only a server process can access the PGA. Oracle Database reads and writes information in the PGA on behalf of the server process. An example of such information is the run-time area of a cursor. Each time a cursor is executed, a new run-time area is created for that cursor in the PGA memory region of the server process executing that cursor.
For complex queries (such as decision support queries), a big portion of the run-time area is dedicated to work areas allocated by memory intensive operators, including:
- Sort-based operators, such as
ORDER
BY
,GROUP
BY
,ROLLUP
, and window functions - Hash-join
- Bitmap merge
- Bitmap create
- Write buffers used by bulk load operations
A sort operator uses a work area (the sort area) to perform the in-memory sorting of a set of rows. Similarly, a hash-join operator uses a work area (the hash area) to build a hash table from its left input.
Work Area Sizes
Oracle Database enables you to control and tune the sizes of work areas. Generally, bigger work areas can significantly improve the performance of a particular operator at the cost of higher memory consumption. The available work area sizes include:
- OptimalOptimal size is when the size of a work area is large enough that it can accommodate the input data and auxiliary memory structures allocated by its associated SQL operator. This is the ideal size for the work area.
- One-passOne-pass size is when the size of the work area is below optimal size and an extra pass is performed over part of the input data. With one-pass size, the response time is increased.
- Multi-passMulti-pass size is when the size of the work area is below the one-pass threshold and multiple passes over the input data are needed. With multi-pass size, the response time is dramatically increased because the size of the work area is too small compared to the input data size.
For example, a serial sort operation that must sort 10 GB of data requires a little more than 10 GB to run as optimal size and at least 40 MB to run as one-pass size. If the work area is less than 40 MB, then the sort operation must perform several passes over the input data.
When sizing the work area, the goal is to have most work areas running with optimal size (more than 90%, or even 100% for pure OLTP systems), and only a small number of them running with one-pass size (less than 10%). Multi-pass executions should be avoided for the following reasons:
- Multi-pass executions can severely degrade performance.A high number of multi-pass work areas has an exponentially adverse effect on the response time of its associated SQL operator.
- Running one-pass executions does not require a large amount of memory.Only 22 MB is required to sort 1 GB of data in one-pass size.
Even for DSS systems running large sorts and hash-joins, the memory requirement for one-pass executions is relatively small. A system configured with a reasonable amount of PGA memory should not need to perform multiple passes over the input data.
Setting the Initial Value for PGA_AGGREGATE_TARGET
Set the initial value of the PGA_AGGREGATE_TARGET
initialization parameter based on the amount of available memory for the Oracle database instance. This value can then be tuned and dynamically modified at the instance level. By default, Oracle Database uses 20% of the SGA size for this value. However, this setting may be too low for a large DSS system.
To set the initial value for PGA_AGGREGATE_TARGET:
- Determine how much of the total physical memory to reserve for the operating system and other non-Oracle applications running on the same system.For example, you might decide to reserve 20% of the total physical memory for the operating system and other non-Oracle applications, dedicating 80% of the memory on the system to the Oracle database instance.
- Divide the remaining available memory between the SGA and the PGA:
- For OLTP systems, the PGA memory typically makes up a small fraction of the available memory, leaving most of the remaining memory for the SGA.Oracle recommends initially dedicating 20% of the available memory to the PGA, and 80% to the SGA. Therefore, the initial value of the
PGA_AGGREGATE_TARGET
parameter for an OLTP system can be calculated as:PGA_AGGREGATE_TARGET
= (total_mem
* 0.8) * 0.2 wheretotal_mem
is the total amount of physical memory available on the system. - For DSS systems running large, memory-intensive queries, PGA memory can typically use up to 70% of the available memory.Oracle recommends initially dedicating 50% of the available memory to the PGA, and 50% to the SGA. Therefore, the initial value of the
PGA_AGGREGATE_TARGET
parameter for a DSS system can be calculated as:PGA_AGGREGATE_TARGET
= (total_mem
* 0.8) * 0.5 wheretotal_mem
is the total amount of physical memory available on the system.
- For OLTP systems, the PGA memory typically makes up a small fraction of the available memory, leaving most of the remaining memory for the SGA.Oracle recommends initially dedicating 20% of the available memory to the PGA, and 80% to the SGA. Therefore, the initial value of the
For example, if an Oracle database instance is configured to run on a system with 4 GB of physical memory, and if 80% (or 3.2 GB) of the memory is dedicated to the Oracle database instance, then initially set PGA_AGGREGATE_TARGET
to 640 MB for an OLTP system, or 1,600 MB for a DSS system.
About PGA_AGGREGATE_LIMIT
The PGA_AGGREGATE_LIMIT
initialization parameter enables you to specify a hard limit on PGA memory usage. If the PGA_AGGREGATE_LIMIT
value is exceeded, Oracle Database aborts or terminates the sessions or processes that are consuming the most untunable PGA memory in the following order:
- Calls for sessions that are consuming the most untunable PGA memory are aborted.
- If PGA memory usage is still over the
PGA_AGGREGATE_LIMIT
, then the sessions and processes that are consuming the most untunable PGA memory are terminated.
In determining the sessions and processes to abort or terminate, Oracle Database treats parallel queries as a single unit.
By default, the PGA_AGGREGATE_LIMIT
parameter is set to the greater of 2 GB, 200% of the PGA_AGGREGATE_TARGET
value, or 3 MB times the value of the PROCESSES
parameter. However, it will not exceed 120% of the physical memory size minus the total SGA size. The default value is printed into the alert log. A warning message is printed in the alert log if the amount of physical memory on the system cannot be determined.
AUTOMATIC PGA MEMORY MANAGEMENT
Automatic PGA memory management resolved above mentioned issues by allowing DBA to allocate an aggregate PGA to all the server processes for all the SQL operations which could be distributed as per the requirement. In this case, Oracle dynamically adapts the SQL memory allocation based on
- – PGA memory available
- – SQL operator needs
- – System workload
With automatic PGA memory management, sizing of SQL work areas for all dedicated server sessions is automatic and all *_AREA_SIZE initialization parameters are ignored for these sessions. At any given time, the total amount of PGA memory available to active work areas on the instance is automatically derived from the parameter PGA_AGGREGATE_TARGET. This amount is set to the value of PGA_AGGREGATE_TARGET minus the PGA memory allocated for other purposes (for example, session memory). The resulting PGA memory is then allotted to individual active work areas based on their specific memory requirements.
To implement it, two parameters need to be set.
WORKAREA_SIZE_POLICY = AUTO
PGA_AGGREGATE_TARGET = Target size of PGA for the total instance.
Given the specified target PGA, Oracle itself tunes the size of individual PGA’s depending
Earlier releases required DBA to manually specify the maximum workarea size for each type of SQL operator.
PGA Overallocation vs. tunable and non-tunable areas
PGA has two components : Tunable and untunable
– Untunable PGA : consists of
- Context information of each session
- Each open cursor
- PL/SQL, OLAP or Java memory
This component of PGA can’t be tuned i.e. whatever is memory is needed it will be consumed else the operation fails.
– Tunable PGA : consists of memory available to SQL work areas (used by various sort operations)
- approx. 90% of PGA in DSS systems
- approx. 10% of PGA in OLTP systems
This component is tunable in the sense that memory available and hence consumed may be less than what is needed and the operation will still complete but may spill to disk. Hence, increasing available memory may improve performance of such operations.
We can control the number of cursors by the init.ora OPEN_CURSORS parameter, but if this is exceeded we get an ORA-1000 error, which is clearly undesirable. See unpublished Note:1012266.6 – “Overview of ORA-1000 Maximum Number of Cursors Exceeded” for more info. More importantly, however, we have no control over the size of a cursor, and users may open very large cursors dependent on their SQL or PLSQL.
Also note that if we set PGA_AGGREGATE_TARGET too small to accommodate the non-tunable part of the PGA plus the minimum memory required to execute the tunable part, then Oracle cannot honour the PGA_AGGREGATE_TARGET value, and will attempt to allocate extra memory. This is known as overallocation, and an estimation of this can be seen in the view V$PGA_TARGET_ADVICE under the column ESTD_OVERALLOC_COUNT.
As PGA memory is divided as tunable and non-tunable areas, while tunable is constrained under PGA_AGGREGATE_TARGET, non-tunable can be over allocated in any size (till ORA-04030), and Oracle records these activities in column ESTD_OVERALLOC_COUNT of V$PGA_TARGET_ADVICE. Therefore ESTD_OVERALLOC_COUNT is caused by over request of non-tunable areas.
Before each overallocation, probably Oracle tries to deallocate certain less used memory (for example, LRU Algorithm) at first. If not satisfied, new memory is allocated.
OPEN_CURSORS specifies the maximum number of open cursors (handles to private SQL areas) a session can have at once. Under overallocation, session cursor caches could be subject to memory deallocation.
Test Case
Test case 1:-
First we will create a table with 3 GB size.My pga_aggregate_limit =2G and pga_aggregate_target =1G.We will sort the table to check the behavior of PGA.
Initially before running the query let me capture the statistics.
Now I will run the query in another session
In the same time I will check what happens in WORK AREA using the following query:
Let me capture the statistics again.
SQL> col NAME for a40;
Test case 2:-Let me run the sort again.
Let me capture the statistics again
AWR statistics related to PGA
Test Case 3:-
Let me increase the PGA_AGGREGATE_TARGET to 10GB.
SQL> set autotrace traceonly;
SQL> exec dbms_workload_repository.create_snapshot;
PL/SQL procedure successfully completed.
SQL> select * from t1 order by c1;
2999999 rows selected.
In the same time I will check what happens in WORK AREA using the following query:
Let me capture statistics again after query is executed.
AWR report related to PGA:-
Test case 4:-
Now we will test about Non-tunable component like PL/SQL area and we will see effect of PGA_AGGREGATE_LIMIT.
Let me set the PGA parameters to less value
Now create PL/SQL table and execute it
Let me capture the statistics
Now I will increase 2000000 to simulate error .This will cause PL/SQL table to allocate more memory in PGA.
Let me capture statistics after failure
Test case 5:
Now I will increase pga_aggregate_limit to 10G and check the behavior.The PL/SQL table will run fine now.
AWR report status:-
Reference:-
//docs.oracle.com/database/121/TGDBA/tune_pga.htm#TGDBA95344
//oracleinaction.com/tune-pga-i/