Wednesday, July 9, 2008

Automatic killing of dead lock sessions


If you are experiencing frequent dead lock problem in your database and want to kill automatically causing sessions. You can accomplished this task with Oracle Resource Manager.


It will kill the sessions those are waiting /hanging more than 20 minutes due to dead lock but you can change minutes as per your requirement. There are 2 groups and this package will be implemented on first group "all_dev_users" and 2nd group "other_groups" will be not effected this package.


BEGIN


Dbms_Resource_Manager.Clear_Pending_Area();

Dbms_Resource_Manager.Create_Pending_Area();

-- Create plan
--============================
Dbms_Resource_Manager.Create_Plan(
plan => 'kill_blocking_idle_session',
comment => 'Plan to kill blocking idle sessions.');

-- Create consumer groups
--============================
Dbms_Resource_Manager.Create_Consumer_Group(
consumer_group => 'all_dev_users',
comment => 'for all dev10g users');


-- Assign consumer groups to plan and define priorities
--============================
Dbms_Resource_Manager.Create_Plan_Directive (
plan => 'kill_blocking_idle_session',
group_or_subplan => 'all_dev_users',
comment => '20 mins killed blocking idle sessions',
max_idle_blocker_time => 1200);


-Change minutes as per your requirement


Dbms_Resource_Manager.Create_Plan_Directive(
plan => 'kill_blocking_idle_session',
group_or_subplan => 'OTHER_GROUPS',
comment => 'all other users' );
-- Users belong to group 'OTHER_GROUPS' will not be killed.


Dbms_Resource_Manager.Validate_Pending_Area;


Dbms_Resource_Manager.Submit_Pending_Area();


END;
/


BEGIN
-- Assign users to consumer groups

Dbms_Resource_Manager_Privs.Grant_Switch_Consumer_Group(
grantee_name => 'FAMI_API',
consumer_group => 'all_dev_users',
grant_option => FALSE);

Dbms_Resource_Manager.Set_Initial_Consumer_Group('FAMI_API', 'all_dev_users');
END;
/

--To enable the resource plan kill_blocking_idle_session

--==========================================
ALTER SYSTEM SET RESOURCE_MANAGER_PLAN = kill_blocking_idle_session scope=both;

--To know how many has been killed since last startup of database

--================================================


select id, NAME, IDLE_BLKR_SESSIONS_KILLED from v$RSRC_CONSUMER_GROUP;

Friday, July 4, 2008

Backup of control file (text format) of your database

Here is script that will create backup of control file (text format) of your database

1. It will identified trace file
2. Edit trace file to make neat and clean script to create control file
3. Copy to identify folder.

How to execute
==============
50 16 * * * /home/oracle/scripts/backup_controlfile.ksh db_name

Output of script
=================
[oracle@dev ~]$ /home/oracle/scripts/backup_controlfile.ksh db_name


GETTING CONTROLFILE TRACE



Copied Trace file to /home/oracle/scripts/dev10g_fri_bk_control.sql
cp /oracle/admin/dev10g/udump/dev10g_ora_25079.trc /home/oracle/scripts/dev10g_fri.trc


Manipulating trace file



New backup Control file: /home/oracle/scripts/dev10g_fri_bk_control.sql


#=============Start of Script=============================
#/home/oracle/scripts/backup_controlfile.kshif [ $# -ne 1 ]then echo Usage Error. Please provide DBNames echo Usage: Ex. ./backup_controlfile.ksh dbname exitfi

export ORACLE_SID=$1BACKUP_FOLDER=/home/oracle/scripts/BACKUP_FILENAME_tmp=${ORACLE_SID}_`/bin/date +%a /usr/bin/tr A-Z a-z`.trcBACKUP_FILENAME=${ORACLE_SID}_`/bin/date +%a /usr/bin/tr A-Z a-z`_bk_control.sql#!/bin/kshecho ""echo ""echo "GETTING CONTROLFILE TRACE"echo ""(sqlplus /nolog< $BACKUP_FOLDER/temptracefile
#echo cp `cat $BACKUP_FOLDER/temptracefile` $BACKUP_FOLDER$BACKUP_FILENAME_tmpcp `cat $BACKUP_FOLDER/temptracefile` $BACKUP_FOLDER$BACKUP_FILENAME_tmpif [[ $? = 0 ]]thenecho ""echo ""echo "Copied Trace file to $BACKUP_FOLDER$BACKUP_FILENAME"echo " cp `cat $BACKUP_FOLDER/temptracefile` $BACKUP_FOLDER$BACKUP_FILENAME_tmp "echo ""echo ""elseecho "FAILED TO GET TRACE FILE"fi
echo "Manipulating trace file"echo ""echo "STARTUP NOMOUNT">$BACKUP_FOLDER$BACKUP_FILENAMEecho ""echo ""cat $BACKUP_FOLDER$BACKUP_FILENAME_tmpsed -e'/^#/d' -e'1,/STARTUP/d' -e'/STARTUP/,//d' -e'/CFILESETSNAPSHOTNAME/d' >> $BACKUP_FOLDER$BACKUP_FILENAMEecho " New backup Control file:" $BACKUP_FOLDER$BACKUP_FILENAME

#===================== end of script==================================

Inspiration

I got inspiration to start my blog from my friend buddy Vish Vangapally.