Wednesday, December 5, 2012

How to find out who is using a large amount of pga memory in my Oracle database

How to find PGA usage by Oracle session


The following query will find any sessions in an Oracle dedicated environment using over 20mb pga memory:

set linesize 110

column PGA_ALLOC_MEM format 99,990
column PGA_USED_MEM format 99,990
column inst_id format 99
column username format a15
column program format a25
column logon_time format a10

select s.inst_id, s.sid, s.username, s.logon_time, s.program, PGA_USED_MEM/1024/1024 PGA_USED_MEM, PGA_ALLOC_MEM/1024/1024 PGA_ALLOC_MEM
from gv$session s
, gv$process p
Where s.paddr = p.addr
and s.inst_id = p.inst_id
and PGA_USED_MEM/1024/1024 > 20  -- pga_used memory over 20mb
order by PGA_USED_MEM;



As a DBA why do I care if see database sessions using alot of PGA memory

PGA (Program Global Area) - it is the session memory that is assigned to a particular program inside of Oracle.

I have seen OLTP applications using over 100mb of PGA per session and have over 20 connections into the database.   That is about 2gb of pga memory being used by one application.  I have PGA_AGGREGATE_TARGET set to 1gb for my entire database instance how could 20 connection be consuming 2gb of pga memory what about the other sessions.  The PGA_AGGREATE_TARGET parameter is a recommended setting not a hard setting.

This was happening in an Oracle database RAC cluster with tight memory constraints.   If you have supported an Oracle RAC environment you will know that if a node stops responding for any reasons it will be evicted from the cluster.  A typical reason why a node get's evicted from the cluster is the machine slow's down due to the maching paging memory in and out of swap.  Even though this cluster had tight memory constraints it would only evict a node once a month when the memory constraints were pushed.

In a later blog I will go into more detail around best practices supporting an Oracle RAC environment and troubleshooting node evictions.

In a non Oracle RAC environment the server would just come to a very slow crawl.

In this particular case I showed the developer how to monitor his session for PGA memory usage.  He found out why his application was using so much memory and made the appropiate code changes.

How to find your current sessions PGA memory usage

You can find you  current sessions pga memory usage by either of the following query's.  The first query everyone should be able to run:

column osuser format a15
column name format a20
select s.osuser osuser,s.serial# serial,se.sid,n.name,
       max(se.value) maxmem
from v$sesstat se,
     v$statname n
,v$session s
where n.statistic# = se.statistic#
and   n.name in ('session pga memory','session pga memory max',
                 'session uga memory','session uga memory max')
and s.sid=se.sid
and s.sid = sys_context('USERENV','SID')
group by n.name,se.sid,s.osuser,s.serial#
order by 2;


This query requires you to have access to v$session and v$process data dictionary views.  Since you are only try to find about your current session there is no need to use the global data dictionary views(gv$)  that are used to go across the cluster.

column PGA_ALLOC_MEM format 99,990
column PGA_USED_MEM format 99,990
column inst_id format 99
column username format a15
column program format a25
column logon_time format a10
select s.inst_id, s.sid, s.username, s.logon_time, s.program, PGA_USED_MEM/1024/1024 PGA_USED_MEM, PGA_ALLOC_MEM/1024/1024 PGA_ALLOC_MEM
from v$session s
, v$process p
Where s.paddr = p.addr
and s.inst_id = p.inst_id
and s.sid = sys_context('USERENV','SID');

Thursday, November 29, 2012

 

All about Oracle RAC clustered database services

 

How to create a database service without dbca or gridcontrol:

srvctl add service -d <db_unique_name> -s <service_name> -r "<preferred_list>" -a "<available_list>" -P <TAF Policy Specification>

example:

The following command will create a database service qadb_oltp for the database qadb.   The service will primarily be for the database instance qadb1 and will connect to qadb2 if qadb1 is not available.  The TAF(transparent application failover) policy which will be used is BASIC.

srvctl add service -d qadb -s qadb_oltp -r "qadb1" -a "qadb2" -P BASIC

Here is the command to remove the following service:

srvctl remove service -d qadb -s qadb_oltp

You maybe asking yourself what really happens when you run this command.  Adding a service modifies your clusterware and database configuration.  

What changes did it make to my clusterware?

The Oracle service get's registerd in Oracle Clusterware.  Oracle Clusterware will manage the service(resource) if something happens to the primary node. 

To find how oracle clusterware see's the service you can run the following command:

crsctl status resource ora.qadb.qadb_oltp.qadb1.srv

NAME=ora.qadb.qadb_oltp.qadb1.srv
TYPE=application
TARGET=ONLINE
STATE=ONLINE on bddb-nm1

 

What changes does it make to my database environment?

When the database service starts it sets your instance configuration parameter service_name dynamically.  If the database service is started you will see the following when your query from your database instance that the service is running on:

SQL> select name, value from v$parameter where name = 'service_names';
NAME
--------------------------------------------------------------------------------
VALUE
--------------------------------------------------------------------------------
service_names
qadb_oltp, qadb.company.com

Once the service_name parameter is set inside your database instance it will register with the appropiate listener.


To see if the service registered with your listener you can run the command lsrnctl status

LSNRCTL for Linux: Version 11.2.0.3.0 - Production on 29-NOV-2012 16:23:32
Copyright (c) 1991, 2011, Oracle.  All rights reserved.
....
Service "qadb_oltp.company.com" has 1 instance(s).
  Instance "qadb1", status READY, has 2 handler(s) for this service...
....
The command completed successfully

As you can the the service qadb_oltp is now registerd with my Listener.

The database service will be created inside your database using the package DBMS_SERVICES. To find out settings like like the goal and wheter distributed transaction processing has been set you need to look at the data_dictionary view DBA_SERVICES. 

Here is an example query from dba_services:
set linesize 110
column name format a30
column network_name format a30
column enabled format a7
select name, network_name, enabled, dtp, goal, aq_ha_notifications
from dba_services;

For further info on the view DBA_SERVICES(ALL_SERVICES) see:  http://docs.oracle.com/cd/E11882_01/server.112/e25513/statviews_2063.htm#sthref1447

 

Why would you use DBMS_SERVICES package?

Use DBMS_SERVICES package to configure settings that you cannot configure using srvctl.  Example of setting you must use DBMS_SERVICES are AQ_HA_NOTIFICATIONS(advanced queuing high availability notification) and distributed transaction processing.