Archive for March, 2010

Installing and configuring Statspack to Oracle (dbms_job configuration)

I was asked to provide instructions on how to install and configure Statspack into Oracle so here goes.

The statspack scripts can be found from under this directory structure:
$Oracle_home/rdbms/admin

The installation needs to be run as SYS user.

Basic installation is done simply by running this script:
@?/rdbms/admin/spcreate.sql

This installs the Statspack engine & tables to database.

You can (if you want) automate the snap creation with this script:
@?/rdbms/admin/spauto.sql

This script takes snaps every hour starting from the next hour.

You can modify this script to make another kind of scheduling.
For example if you want to take snaps every half an hour, you need to modify:
"sysdate+1/24" => "sysdate+1/48"

dbms_job.submit(:jobno, 'statspack.snap;', trunc(sysdate+1/24,'HH'), 'trunc(SYSDATE+1/24,''HH'')', TRUE, :instno);

=>
dbms_job.submit(:jobno, 'statspack.snap;', trunc(sysdate+1/48,'HH'), 'trunc(SYSDATE+1/48,''HH'')', TRUE, :instno);

If you need to ie. change the currently running job interval, you need to find out the job number first.
Connect as dba user to Oracle and run this query:

select job, what, interval from dba_jobs;

JOB
----------
WHAT
-----------------------------
INTERVAL
-----------------------------
21
statspack.snap;
trunc(SYSDATE+1/24,'HH')

So the statspack.snap is run every hour and job number is 21. Fine.
Let’s say that we need to run it every half an hour.
We need to change the interval to: 1/48.

You can update the job interval like this. You can set is as the job owner with this syntax:
exec dbms_job.interval(21,'TRUNC(SYSDATE+1/48,''HH'')');

PL/SQL procedure successfully completed.

Or as another user with DBA privileges:
SQL> show user
USER is "SYS"
SQL> exec dbms_ijob.interval(21,'TRUNC(SYSDATE+1/48,''HH'')');

PL/SQL procedure successfully completed.

Please note: there are two single quotes ‘ around HH, not one double-quote “.

Also you could remove the job with:
exec dbms_job.remove(21);

and re-create it with:

variable jobno number;
variable instno number;
begin
select instance_number into :instno from v$instance;
dbms_job.submit(:jobno, 'statspack.snap;', trunc(sysdate+1/48,'HH'), 'trunc(SYSDATE+1/48,''HH'')', TRUE, :instno);
commit;
end;
/

You should try that the job runs now.

Run as the job owner:
SQL> exec dbms_job.run(21);

PL/SQL procedure successfully completed.

Run as a DBA user:
SQL> exec dbms_ijob.run(21);

PL/SQL procedure successfully completed.

You can check when the next scheduled job run is:

SQL> select job, what, interval, to_char(next_date, 'DD.MM.YYYY HH24:MI:SS') from dba_jobs where job=21;

JOB
----------
WHAT
-------------------------------------
INTERVAL
-------------------------------------
TO_CHAR(NEXT_DATE,'
-------------------
21
statspack.snap;
TRUNC(SYSDATE+1/48,'HH')
29.03.2010 11:00:00

Statspack was introduced in Oracle 9i and it is still an usable tool in current Oracle version of 11.2.
AWR is superior technology compared to Statspack, but for many purposes Statspack still holds its place.

Posted by pparkko on March 26th, 2010 No Comments

Automatic start of Oracle database on Linux

In certain cases Oracle does not start automatically on Linux after installation.
To fix this, you need to do these steps:
Modify oratab
vi /etc/oratab
change the “N” at the end => “Y”
For example:
From: orcl:/u01/app/oracle/product/11.2.0/dbhome_1:N
To: orcl:/u01/app/oracle/product/11.2.0/dbhome_1:Y
Create a dbora file under /etc/init.d/
vi /etc/init.d/dbora
#!/bin/sh
# chkconfig: 345 99 10
# description: Automatic Oracle database start-stop script.
# Set ORA_OWNER to the user id of the owner of the
# Oracle database in ORA_HOME.
ORA_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
ORA_OWNER=oracle
if [ ! -f $ORA_HOME/bin/dbstart ]
then
echo “Oracle startup: cannot start”
exit
fi
case “$1″ in
’start’)
# Start Oracle databases:
su – $ORA_OWNER -c “$ORA_HOME/bin/dbstart $ORA_HOME”
touch /var/lock/subsys/dbora
;;
’stop’)
# Stop Oracle databases:
su – $ORA_OWNER -c “$ORA_HOME/bin/dbshut $ORA_HOME”
rm -f /var/lock/subsys/dbora
;;
esac
If you modify the script above, remember to keep the 3 first commented lines (shell, chkconfig and description).
Change the privileges to appropriate level with:
chmod 750 /etc/init.d/dbora
Now add the script to chkconfig:
chkconfig –add dbora
You can check this with:

$> chkconfig –list | grep db
dbora 0:off 1:off 2:off 3:on 4:on 5:on 6:off
You’re done, verify after next reboot:
ps -ef | grep smon

Posted by pparkko on March 8th, 2010 No Comments

SELinux and Oracle – How to disable SELinux and make Oracle work

If you encounter this error on Oracle it is a good possibility that you have SELinux enabled on your Linux box:

$> sqlplus / as sysdba
sqlplus: error while loading shared libraries: /u01/app/oracle/product/11.2.0/dbhome_1/lib/libclntsh.so.11.1: cannot restore segment prot after reloc: Permission denied
The quick way to get Oracle working is to use setenforce:

$> setenforce 0
$> sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 13 15:31:35 2010
Copyright (c) 1982, 2009, Oracle. All rights reserved.

However, this solution will only work until you reboot the server.
To permanently fix this issue you need to modify the SELinux configuration file:

$> vi /etc/selinux/config
Change:
SELINUX=enforcing
To:
SELINUX=disabled
You should also be able to use permissive mode on single instances, if you change to this mode BEFORE you install Oracle:
SELINUX=permissive
CRS will not like permissive mode, so this does not apply for RAC installations.
Now SQL*Plus works:

$> sqlplus / as sysdba
SQL*Plus: Release 11.2.0.1.0 Production on Wed Jan 13 15:31:35 2010
Copyright (c) 1982, 2009, Oracle. All rights reserved.

Posted by pparkko on March 8th, 2010 No Comments

Working with Unicode in Oracle

I have been working mostly with “normal” character set databases.
The last project however uses Unicode database (AL32UTF8) in 11.2
I ran into this strange behaviour (until I knew better):

create table test(field varchar2(10));
Table created.
SQL> insert into testi values (‘öäåÖÄÅ’);
insert into testi values (‘öäåÖÄÅ’)
*
ERROR at line 1:
ORA-12899: value too large for column “”TEST”.”FIELD” (actual: 18,
maximum: 10)
SQL> insert into test values (‘öä’);
1 row created.
So – I created a field for 10 characters (or so I thought!) and tried to store only 6 characters and failed.
What an odd thing.
Closer look tells that each character actually uses 3 bytes each:

SQL> select dump(field) from test;
DUMP(KENTTA)
——————————————————————————–
Typ=1 Len=6: 239,191,189,239,191,189
1 row selected.
All right, so it seems that there is actually space for only 10 BYTES – not characters…
After 5 seconds of furious googling the reason is clear and my world shattered, again.
The VARCHAR2(10) REALLY means VARCHAR2(10 BYTE).
What I should have always used is VARCHAR2(10 CHAR).
A new test:

create table test2 (field varchar2(10 char));
=>
SQL> insert into test2 values(‘öäåÖÄÅ’);
1 row created.
How embarrassing after so many years of working with Oracle

Posted by pparkko on March 8th, 2010 No Comments

Limiting rows per block in Oracle database table – setting Hakan factor

This is something I learned today by accident
There are times when you need to optimize physical data arrangement in database, in this case in a database table. The reasons for this vary from row migration to concurrency problems.
Here is one way to limit rows inside database table to a set value.
You must understand this is may not be the best way to accomplish this.
Of course this can be done also by setting PCTFREE to a high value, but that is not as exact science as this.
Here goes:

create table test(field number);
insert into test values(1);
commit;
alter table test minimize records_per_block;
–From this point on, number of rows per block will be limited to 1.
I learned this from Mark J. Bobak from Oracle forums.
Here are his words:
“The alter table … minimize records_per_block;
functionality sets the Hakan factor. This limits the number of rows per block to the max of the largest number of rows in any block in the table. This functionality was originally conceived to help optimize builds of bitmap indexes, but it can be used anytime you want to ‘artificially’ limit the number of rows per block.”

Posted by pparkko on March 8th, 2010 No Comments

Oracle 11g and default 180 day default password life time ( PASSWORD_LIFE_TIME )

Just a quick reminder about maybe the most common small problem in new 11g installations / upgrades.
When entering the 11g world, remember to do something to the default setting of password life time of 180 days. If you do nothing, your users / schemas will cease to work after half an year from creation.
select profile, limit from dba_profiles where resource_name = ‘PASSWORD_LIFE_TIME’;
PROFILE LIMIT
—————————— —————————————-
DEFAULT 180
MONITORING_PROFILE DEFAULT
If you want to remove the limits altogether, here is how to do it:
SQL> alter profile default limit PASSWORD_LIFE_TIME unlimited;
Profile altered.
Verify with:
1* select profile, limit from dba_profiles where resource_name = ‘PASSWORD_LIFE_TIME’
PROFILE LIMIT
—————————— —————————————-
DEFAULT UNLIMITED
MONITORING_PROFILE DEFAULT

Posted by pparkko on March 8th, 2010 No Comments

Operating System (OS) authentication in Windows

I had a question in training about OS authentication in Windows.
The question was about normal user authentication, not the SYSDBA authentication.
Hmm. I had never used it for authenticating normal users – so I just had to try.
First I need to know which user I am:
SQL> select UPPER(sys_context(‘userenv’,'os_user’)) from dual;
UPPER(SYS_CONTEXT(‘USERENV’,'OS_USER’))
—————————————————————
PASICITRUS\PASI PARKKONEN
SQL>
The next step is to create that user and grant user rights:
create user “PASICITRUS\PASI PARKKONEN” identified externally;
User created.
SQL> grant dba to “PASICITRUS\PASI PARKKONEN”;
Grant succeeded.
Now I can try to connect:
SQL> connect /
Connected.
Who am I..?
SQL> show user
USER is “PASICITRUS\PASI PARKKONEN”
Great, done!

Posted by pparkko on March 8th, 2010 No Comments

How to reinstall Enterprise Manager (Database Control)

If you have problems in running Enterprise Manager (aka Database Control), you may need to reinstall it.
Here is how to do it.

Run the following command: emca -deconfig dbcontrol db -repos drop

C:\>emca -deconfig dbcontrol db -repos drop

STARTED EMCA at 3.2.2010 13:10:47
EM Configuration Assistant, Version 11.1.0.5.0 Production
Copyright (c) 2003, 2005, Oracle. All rights reserved.

Enter the following information:
Database SID: orcl
Listener port number: 1521
Password for SYS user:
Password for SYSMAN user:
Password for SYSMAN user:
Do you wish to continue? [yes(Y)/no(N)]: Y
3.2.2010 13:13:34 oracle.sysman.emcp.EMConfig perform
INFO: This operation is being logged at C:\app\oracle\cfgtoollogs\emca\orcl\em
_2010_02_03_13_10_47.log.
3.2.2010 13:13:45 oracle.sysman.emcp.EMDBPreConfig performDeconfiguration
WARNING: EM is not configured for this database. No EM-specific actions can be
erformed.
3.2.2010 13:13:50 oracle.sysman.emcp.EMReposConfig invoke
INFO: Dropping the EM repository (this may take a while) …
3.2.2010 13:34:40 oracle.sysman.emcp.EMReposConfig invoke
INFO: Repository successfully dropped
Enterprise Manager configuration completed successfully
FINISHED EMCA at 3.2.2010 13:34:40

The process may take a while.

After the drop you can reinstall the Enterprise Manager with: emca -config dbcontrol db -repos create

C:\>emca -config dbcontrol db -repos create

STARTED EMCA at 3.2.2010 13:37:18
EM Configuration Assistant, Version 11.1.0.5.0 Production
Copyright (c) 2003, 2005, Oracle. All rights reserved.

Enter the following information:
Database SID: orcl
Listener port number: 1521
Password for SYS user:
Password for DBSNMP user:
Password for SYSMAN user:
Password for SYSMAN user: Email address for notifications (optional):
Outgoing Mail (SMTP) server for notifications (optional):
—————————————————————–

You have specified the following settings

Database ORACLE_HOME ……………. C:\app\oracle\product\11.1.0\db_1

Local hostname ……………. IP_address
Listener port number ……………. 1521
Database SID ……………. orcl
Email address for notifications ……………
Outgoing Mail (SMTP) server for notifications ……………

—————————————————————–
Do you wish to continue? [yes(Y)/no(N)]: Y
3.2.2010 13:38:43 oracle.sysman.emcp.EMConfig perform
INFO: This operation is being logged at C:\app\oracle\cfgtoollogs\emca\orcl\emca
_2010_02_03_13_37_18.log.
3.2.2010 13:38:45 oracle.sysman.emcp.EMReposConfig createRepository
INFO: Creating the EM repository (this may take a while) …
3.2.2010 13:53:25 oracle.sysman.emcp.EMDBPostConfig performConfiguration
INFO: Database Control started successfully
3.2.2010 13:53:25 oracle.sysman.emcp.EMDBPostConfig performConfiguration
INFO: >>>>>>>>>>> The Database Control URL is https://IP_address:5500/em <<<<<<<
<<<<
3.2.2010 13:53:27 oracle.sysman.emcp.EMDBPostConfig invoke
WARNING:
************************ WARNING ************************

Management Repository has been placed in secure mode wherein Enterprise Manager
data will be encrypted. The encryption key has been placed in the file: C:\app\
oracle\product\11.1.0\db_1\10.8.0.171_orcl\sysman\config\emkey.ora. Please ens
ure this file is backed up as the encrypted data will become unusable if this fi
le is lost.

***********************************************************
Enterprise Manager configuration completed successfully
FINISHED EMCA at 3.2.2010 13:53:27

Great! Now you can try to log in: https://IP_address:5500/em

Posted by pparkko on March 8th, 2010 No Comments