Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: May 25, 2002 - 9:12 pm UTC

Last updated: August 24, 2005 - 8:54 am UTC

Version: 9.0.1

Viewed 1000+ times

You Asked

Hello Tom,


If i have specified sort_area_retained_size to 0 and
sort_area_size = 524288

The below is the output for the query:

select a.name, b.value
from v$statname a, v$mystat b
where a.statistic# = b.statistic #
and a.name like '%ga%';




Session uga memory = 49256
Session uga memory max = 82148
Session pga memory = 4411828
Session Pga memory max = 4411828


When I execute the query :

Select * from all_objects order by 1,2,3,4;


Since Sort_area_retained_size = 0

While processing the above query, I query v$TEMP_EXTENT_POOL
to see any extents are allocated from temporary segments:


I have one extent allocated with 128 blocks.

But after the query got executed:

The below are Statistics:

Session UGA memory = 57728
Session UGA memory max = 632392
Session PGA memory = 4411828
Session PGA memory max = 4411828


My question is:


Since a temporary extent is allocated the sort_area_size of 524288
was not sufficient to handle the sort of the query. Hence it uses a temporary segment. Since my sort_area_retained_size = 0 every sorting
is done on the temporary segment.

q1) Why is there an increase in Session UGA memory ?
q2) why is there no increase in the PGA memory by sort_area_size?

Regards,

PS: I am currently using linux system and its little bit tough in cutting and pasting actual results.


and Tom said...

You seem to be confusing the purpose of sort_area_retained_size.

A sort area retained size will not influence whether a sort is done in RAM or not -- just where the RESULTS of the sort will be stored.

Also, when sort_area_size = 0, it really = sort_area_size. The valid range of values for this is 2 blocks to sort_area_size. By setting it to zero, you set it actually to the sort_area_size. Seeing as how your UGA grew by about the sort_area_size -- this makes total sense.

The PGA is managed as a heap - in it might be many free chunks of memory that were at one time used by are not currently used now. We will get sort_area_size bytes of memory from the PGA free space before asking the OS for more.


Rating

  (7 ratings)

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

Comments

Reader

A reader, December 12, 2003 - 6:22 pm UTC

From 9.2 reference manual:
sort_area_size
--------------
Only one memory area of SORT_AREA_SIZE exists for each
user process at any time.

sort_area_retained_size
-----------------------
Oracle may allocate multiple sort spaces of this size for each query. Usually, only
one or two sorts occur at one time, even for complex queries. In some cases,
however, additional concurrent sorts are required, and each sort keeps its own
memory area.

Q: Does this imply that a process can allocate multiple sort_area_retained size
for reading the sorted result, and ONLY ONE sort_area_size for sorting row source
even if the same query requires multiple sorts

Tom Kyte
December 13, 2003 - 11:33 am UTC

yes it does -- you have many retained perhaps for a query and one sort area size per process (think PQ for a moment -- separate processes/threads).


Say you have a 1 meg retained size and a 2meg sort area size. Oracle will begin by allocated the retained size in the UGA (usually in the pga, but in shared server this is in the SGA). when that hits 1meg, it'll start allocating the sort area size dynamically in the PGA (always the PGA). the retained size will stick around as long as we need it (typically till the result set is exhausted/cursor closed). the sort area size will be released for reuse after the call that required it is done.

Reader

A reader, December 13, 2003 - 2:36 pm UTC

"
Say you have a 1 meg retained size and a 2meg sort area size. Oracle will begin
by allocated the retained size in the UGA (usually in the pga, but in shared
server this is in the SGA). when that hits 1meg, it'll start allocating the
sort area size dynamically in the PGA (always the PGA).
"

I have heard that oracle first allocates sort_area in increments upto sort_area-size
It uses this memory to perform multiple sort runs and merges. When the sorting is completed
oracle uses retained_size to read the result row set and gives it to the session. The
sort_area-size is in PGA and sort_area_retained_size is in UGA. So, if sort_area is 2M
and retained_size is 1M. oracle will allocate upto 2M in PGA first for sorting. When
sort is completed, it will allocate 1M from UGA ( ex: SGA for MTS). Therefore these two
memory allocation are seperate, although they both use the same row set of the sort, Is
my comprehension correct or correct me if I am wrong

Tom Kyte
December 13, 2003 - 3:27 pm UTC

that is not correct.

it will allocate up to sort_area_retained_size in the UGA and the spill over into the "pure" PGA up to sort_area_size if the sort exceeds the sort_area_retained_size AND sort_area_size is greater then sort_area_retained_size.

the sort_area_retained_size parameter is often misunderstood. At a high level, this is what happens:

to do a sort, Oracle allocates chunks of memory up to a maximum of sort_area_size.

As each chunk is allocated -- Oracle checks to see if the sort area is less then the sort_area_retained_size.

If so, this chunk is allocated in memory that survives from call to call (in the UGA -- where ever the UGA might be -- in the pga or sga). This memory survives as long as needed (across many calls potentially to fetch rows and such)

If not, this chunk is allocated in the "pure pga" and freed at the end of the call. This memory does not survive across calls at all.

Sort area memory is not necessarily contigous memory -- hence bits of it could be in the SGA, some in the PGA -- it matters not to Oracle.

see also
</code> http://docs.oracle.com/docs/cd/B10501_01/server.920/a96536/ch1197.htm#1024241 <code>

...
Oracle may allocate multiple sort spaces of this size for each query. Usually, only one or two sorts occur at one time, even for complex queries. In some cases, however, additional concurrent sorts are required, and each sort keeps its own memory area. If the shared server is used, allocation is to the SGA until the value in SORT_AREA_RETAINED_SIZE is reached. The difference between SORT_AREA_RETAINED_SIZE and SORT_AREA_SIZE is allocated to the PGA.
........



Continuation from previous posting ....

A reader, December 13, 2003 - 3:22 pm UTC

From RDBMS 9.2 Referencxe Manual
"
SORT_AREA_SIZE specifies in bytes the maximum amount of memory Oracle will
use for a sort. After the sort is complete, but before the rows are returned,
Oracle releases all of the memory allocated for the sort, except the amount
specified by the SORT_AREA_RETAINED_SIZE parameter. After the last row is
returned, Oracle releases the remainder of the memory.
"

This seems to imply that SORT_AREA_SIZE and SORT_AREA_RETAINED_SIZE are
both come from the same memory source unless they are specifically referring
to dedicated servers. Could you please clarify

Thanks

Tom Kyte
December 13, 2003 - 3:29 pm UTC

sorts are done in the UGA upto sort_area_retained_size and then spill into the PGA.

In shared server, the UGA is in the SGA.
In dedicated server, the UGA is in the PGA.

that is the difference.

Thanks very much for the clarification

A reader, December 13, 2003 - 3:55 pm UTC


sort_area_retained_size

A reader, December 22, 2003 - 12:43 pm UTC

Hi

if we set WORKAREA_SIZE_POLICY to auto does this make sort_area_retained_size obsolete?

Tom Kyte
December 22, 2003 - 1:47 pm UTC

except for shared server connections in 9i, yes.

is there a way

A reader, December 23, 2003 - 5:53 am UTC

to know a parameter is obsolete from v$parameter?

In this case, sort_retained_size I was looking a doc which states automatic PGA will make this parameter obsolete but found none

Tom Kyte
December 23, 2003 - 11:20 am UTC

it is not that it is "formally obsolete"

it is that it is not used when workarea_size_policy=auto.

the docs for that init.ora parameter state that when set to manual, the *_size parameters are used. (else, not)

the docs for sort_area_size state that:

...
Note: Oracle does not recommend using the SORT_AREA_SIZE parameter unless the instance is configured with the shared server option. Oracle recommends that you enable automatic sizing of SQL working areas by setting PGA_AGGREGATE_TARGET instead. SORT_AREA_SIZE is retained for backward compatibility.
........

so, just check out the reference guide and read about the two parameters.


sort area size

reader, August 23, 2005 - 3:44 pm UTC

When you say, <quote>If not, this chunk is allocated in the "pure pga" and freed at the end of the
call.<quote>

Is it freed and given back to operating system? Thanks for clarifying.


Tom Kyte
August 24, 2005 - 8:54 am UTC

depends on the release. 8i and before, the pga memory is freed back to the process (for other allocations by that process) but not to the OS.

With workareas, they can be dynamically attached and freed back to the OS.

You can see this
</code> http://oracle.apress.com//betabooks/betabook.html?betabook_id=30&APRESSESSID=c5e74fa001f64357db462f5e677af1b1 <code>
at work.

....
You can see from the Autotrace statistics, that Oracle (using autotrace in SQL*Plus) has reported that we've done 8 recursive calls of some sort during the processing of our query. 3 sorts in memory and 1 sort that spilled to disk. By the time we finished our query and exhausted the result set, we can see our UGA memory went back down to what it started at (we released the sort areas from our UGA) and the PGA shrunk back somewhat (note that in Oracle 8i and before, you would not expect to see the PGA shrink back at all, this is new with Oracle 9i and later). Now, let's retry that operation but play around with the size of our SORT_AREA increasing it to 1 MB:
.......