2010-01-19

Send mail from Windows

When we create backup scripts sometimes we need to email notifications of success or failure.
There are few ways to do it. One of them is to use blat. Another one is VBScript. Here is example of such vbs:

Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
Set objEmail = CreateObject("CDO.Message")
objEmail.From = user+"@"+comp
objEmail.To = "your-name@your-email.com"
objEmail.Subject = comp + " RMAN backup failed"
objEmail.Textbody = "Please check RMAN log file"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        "your.smtp.server.com"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send

2010-01-08

Database performance monitoring

Some handy commands to help with database performance monitoring and troubleshooting.
Environment: Oracle 10.2.0.4 on Solaris
  1. PGA usage, top consumers
    select
    round(p.pga_alloc_mem/1024/1024) "pga_alloc_MiB",
    round(p.pga_used_mem/1024/1024) "pga_used_MiB",
    p.spid "OS_pid",
    s.sid, s.serial#, s.username, s.status, s.client_info, s.osuser, s.machine, s.terminal, s.program, s.module,s.sql_id
    from v$process p, v$session s
    where p.addr=s.paddr
    order by p.pga_alloc_mem desc;
  2. UNDO usage, top consumers
    select round(t.used_ublk*(select block_size from dba_tablespaces where tablespace_name=(select value from v$parameter where name = 'undo_tablespace'))/1024/1024) "undo_used_MiB",
    t.used_ublk used_blocks,
    t.START_DATE trn_started,
    s.sid, s.serial#, s.username, s.status,s.osuser, s.machine, s.terminal, s.program, s.module,s.sql_id
    from V$TRANSACTION t, v$session S
    where t.SES_ADDR=s.saddr
    order by used_ublk desc;
  3. TEMP usage, top consumers
    select t.tablespace, t.blocks,
    round((t.blocks * ts.block_size)/1024/1024) "Size_MiB", t.segtype,
    s.sid, s.serial#, s.username, s.status,s.osuser, s.machine, s.terminal, s.program, s.module,s.sql_id
    from v$session s, v$tempseg_usage t, dba_tablespaces ts
    where t.tablespace=ts.tablespace_name and s.saddr = t.session_addr
    order by t.blocks desc;
  4.  CPU usage
    • UNIX top command, it can be sorted by CPU or memory (SIZE or RES). Press “h” for help.
    • In OEM you can look at Performance page of host.
    • UNIX Solaris command prstat
      sorted by CPU usage:

      prstat
      processes sorted by CPU and totals per user
      prstat -a
      sorted by memory and totals per user. Careful! It is different from top command as it shows shared memory as well.
      prstat -a -s rss


  5. Free memory
    UNIX top command and OEM host performance page can be used. They show the same result.
     
  6.  Paging and Swapping
    In OEM:
    Go to target host, "Performance" tab. Click "Swap Utilization" number to see a graph or click "Paging Activity" to see more metrics.
    Unix commands:
    vmstat -S 3 12
    Where 3 is the number of seconds between statistics samplings and 12 is the number of samples to take. Watch "si" and "so", swap in/out columns, and scan rate "sr". If scan rate is non-zero for an extended period of time, you need more RAM.
    Note that first line of output includes all activity since the last vmstat report.

2010-01-06

Video problem in Skype on Linux

Environment:
Ubuntu 9.10 Karmic Koala 64-bit, Skype 2.1.0.47 (deb package downloaded from skype.com)
Problem:
test video is just a green screen.
Solution:
You need to use correct v4l1compat.so library.
az@az1:~$ locate v4l1compat.so
/usr/lib/libv4l/v4l1compat.so
/usr/lib32/libv4l/v4l1compat.so

Second one works ok for 64-bit linux. Try from shell:
LD_PRELOAD=/usr/lib32/libv4l/v4l1compat.so skype &

To make it permanent go to (using Gnome)
System > Preferences > Main Menu
and change properties of Skype shortcut to

env LD_PRELOAD=/usr/lib32/libv4l/v4l1compat.so skype

2009-12-15

Oracle LogMiner (3)

Environment

Oracle 10g (10.2.0.4) on Solaris 10

Purpose

Extract data with given time interval and filter.
Time interval:
from 2009-12-09 10:35 to 2009-12-09 10:35
Filter:
table_space='TABLES' and operation in ('UPDATE','INSERT','DELETE');

Solution

Switch logs to make sure that latest data changes are in archived logs.
alter system switch logfile;

Find needed logs:
select * from v$archived_log
where completion_time > to_date('2009-12-09 10:35','yyyy-mm-dd hh24:mi')
order by first_time;

Create SQL script to add logs to LogMiner session:
select 'DBMS_LOGMNR.ADD_LOGFILE(LOGFILENAME => '''||name||''');' from v$archived_log
where first_time > to_date('2009-12-06 10:35','yyyy-mm-dd hh24:mi')
order by first_time;

After adding DBMS_LOGMNR.NEW to first log, it looks like this:

begin
DBMS_LOGMNR.ADD_LOGFILE(LOGFILENAME => '/oracle/archive/DHMERR03/disk/1_628_689683506.dbf',OPTIONS => DBMS_LOGMNR.NEW);
DBMS_LOGMNR.ADD_LOGFILE(LOGFILENAME => '/oracle/archive/DHMERR03/disk/1_629_689683506.dbf');
DBMS_LOGMNR.ADD_LOGFILE(LOGFILENAME => '/oracle/archive/DHMERR03/disk/1_630_689683506.dbf');
DBMS_LOGMNR.ADD_LOGFILE(LOGFILENAME => '/oracle/archive/DHMERR03/disk/1_631_689683506.dbf');
end;

Alternatively you can define only first log and use option CONTINUOUS_MINE to add following logs automatically.

Start LogMiner:
Begin
DBMS_LOGMNR.START_LOGMNR(startTime => to_date('2009-12-09 10:35','yyyy-mm-dd hh24:mi'), endTime => to_date('2009-12-15 11:10','yyyy-mm-dd hh24:mi'), OPTIONS => DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG + DBMS_LOGMNR.COMMITTED_DATA_ONLY);
End;

As PK supplemental logging was used in this case, ROWID could be excluded by adding option DBMS_LOGMNR.NO_ROWID_IN_STMT to previous statement. It would look like:
OPTIONS => DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG + DBMS_LOGMNR.COMMITTED_DATA_ONLY + DBMS_LOGMNR.NO_ROWID_IN_STMT)

Return data changes using given filter:
select * from V$LOGMNR_CONTENTS
where table_space='TABLES' and operation in ('UPDATE','INSERT','DELETE');

If necessary you can record the result into table.
create table alexz.LOGMINER_CONTENTS_20091215 tablespace users
as select * from V$LOGMNR_CONTENTS
where table_space='TABLES' and operation in ('UPDATE','INSERT','DELETE');

grant select on alexz.LOGMINER_CONTENTS_20091215 to public;

Finish LogMiner session
EXECUTE DBMS_LOGMNR.END_LOGMNR;

2009-12-01

Oracle LogMiner (2) supplemental logging

Environment

Oracle 10g (10.2.0.4) on Solaris 10

Purpose

Test Logminer with different supplemental logging modes.
Compare actual and Logminer SQL and choose proper supplemental logging to re-apply some application code.

Prepare database

CREATE TABLE "ALEXZ"."TBL1"
("ID" NUMBER,
 "N1" NUMBER,
 "V1" VARCHAR2(77),
 "D1" DATE, "D2" DATE,
PRIMARY KEY ("ID") VALIDATE);

Select log_mode, SUPPLEMENTAL_LOG_DATA_MIN, SUPPLEMENTAL_LOG_DATA_PK, SUPPLEMENTAL_LOG_DATA_UI from v$database;
LOG_MODE     SUPPLEME SUP SUP
------------ -------- --- ---
NOARCHIVELOG NO       NO  NO

Startup mount;
alter database archivelog;
alter database add supplemental log data (primary key) columns;
alter database open;

Logminer session

Choose current log file.
select * from v$log;
select * from v$logfile;

Start logminer session.
EXECUTE sys.DBMS_LOGMNR.ADD_LOGFILE(LOGFILENAME => '/oracle/redo/01/DHMERT01/DHMERT01redo01.dbf', OPTIONS => sys.DBMS_LOGMNR.NEW);
EXECUTE sys.DBMS_LOGMNR.START_LOGMNR(OPTIONS => sys.DBMS_LOGMNR.DICT_FROM_ONLINE_CATALOG + sys.DBMS_LOGMNR.COMMITTED_DATA_ONLY);

select * from V$LOGMNR_CONTENTS
where seg_name ='TBL1' order by CSCN;

Task 1

PK supplemental logging
Actual SQL:
insert into tbl1 values (1,1,'aa',sysdate,sysdate);
update tbl1 set d2=sysdate where id=1;

SQL_REDO from Logminer:
insert into "ALEXZ"."TBL1"("ID","N1","V1","D1","D2") values ('1','1','aa',TO_DATE('2009-12-01 12:29', 'yyyy-mm-dd hh24:mi'),TO_DATE('2009-12-01 12:29', 'yyyy-mm-dd hh24:mi'));

update "ALEXZ"."TBL1" set "D2" = TO_DATE('2009-12-01 12:29', 'yyyy-mm-dd hh24:mi') where "ID" = '1' and "D2" = TO_DATE('2009-12-01 12:29', 'yyyy-mm-dd hh24:mi') and ROWID = 'AAAip0AADAAA6GqAAA';

Task 2

All columns supplemental logging
alter database drop supplemental log data (primary key) columns;
alter database add supplemental log data (all) columns;
Select log_mode, SUPPLEMENTAL_LOG_DATA_MIN, SUPPLEMENTAL_LOG_DATA_PK, SUPPLEMENTAL_LOG_DATA_UI from v$database;

LOG_MODE     SUPPLEME SUP SUP
------------ -------- --- ---
ARCHIVELOG   IMPLICIT NO  NO

Actual SQL:
insert into tbl1 values (2,2,'aa',sysdate,sysdate);
update tbl1 set d2=sysdate+1 where id=2;

SQL_REDO from Logminer:
insert into "ALEXZ"."TBL1"("ID","N1","V1","D1","D2") values ('2','2','aa',TO_DATE('2009-12-01 12:35', 'yyyy-mm-dd hh24:mi'),TO_DATE('2009-12-01 12:35', 'yyyy-mm-dd hh24:mi'));

update "ALEXZ"."TBL1" set "D2" = TO_DATE('2009-12-02 12:36', 'yyyy-mm-dd hh24:mi') where "ID" = '2' and "N1" = '2' and "V1" = 'aa' and "D1" = TO_DATE('2009-12-01 12:35', 'yyyy-mm-dd hh24:mi') and "D2" = TO_DATE('2009-12-01 12:35', 'yyyy-mm-dd hh24:mi') and ROWID = 'AAAip0AADAAA6GqAAB';

Task 3

All columns + PK supplemental logging (it seems working like “all columns”)
alter database add supplemental log data (primary key) columns;
LOG_MODE     SUPPLEME SUP SUP
------------ -------- --- ---
ARCHIVELOG   IMPLICIT YES NO

insert into tbl1 values (3,3,'aa',sysdate,sysdate);
update tbl1 set d2=sysdate+1 where id=3;

SQL_REDO from Logminer:

insert into "ALEXZ"."TBL1"("ID","N1","V1","D1","D2") values ('3','3','aa',TO_DATE('2009-12-01 12:37', 'yyyy-mm-dd hh24:mi'),TO_DATE('2009-12-01 12:37', 'yyyy-mm-dd hh24:mi'));

update "ALEXZ"."TBL1" set "D2" = TO_DATE('2009-12-02 12:38', 'yyyy-mm-dd hh24:mi') where "ID" = '3' and "N1" = '3' and "V1" = 'aa' and "D1" = TO_DATE('2009-12-01 12:37', 'yyyy-mm-dd hh24:mi') and "D2" = TO_DATE('2009-12-01 12:37', 'yyyy-mm-dd hh24:mi') and ROWID = 'AAAip0AADAAA6GqAAC';

Task 4

Update without PK
PK supplemental logging
alter database drop supplemental log data (all) columns;
alter database drop supplemental log data (primary key) columns;
LOG_MODE     SUPPLEME SUP SUP
------------ -------- --- ---
ARCHIVELOG   NO       NO  NO

alter database add supplemental log data (primary key) columns;
LOG_MODE     SUPPLEME SUP SUP
------------ -------- --- ---
ARCHIVELOG   IMPLICIT YES NO

update tbl1 set v1='bb' where n1<3;

SQL_REDO from Logminer:
update "ALEXZ"."TBL1" set "V1" = 'bb' where "ID" = '1' and "V1" = 'aa' and ROWID = 'AAAip0AADAAA6GqAAA';
update "ALEXZ"."TBL1" set "V1" = 'bb' where "ID" = '2' and "V1" = 'aa' and ROWID = 'AAAip0AADAAA6GqAAB';

Task 5

Update without PK
Minimal supplemental logging
alter database drop supplemental log data (primary key) columns;
alter database add supplemental log data;
LOG_MODE     SUPPLEME SUP SUP
------------ -------- --- ---
ARCHIVELOG   YES      NO  NO

update tbl1 set v1='cc' where n1<3;

SQL_REDO from Logminer:
update "ALEXZ"."TBL1" set "V1" = 'cc' where "V1" = 'bb' and ROWID = 'AAAip0AADAAA6GqAAA';
update "ALEXZ"."TBL1" set "V1" = 'cc' where "V1" = 'bb' and ROWID = 'AAAip0AADAAA6GqAAB';

2009-11-20

Restoring 10g database to another location

Restoring M02 (10.2.0.4) from cold RMAN backup of M03 (10.2.0.3).
1.    stop all for M02
2.    delete M02 data files and redo
3.    change owner to ora10P1 (10.2.0.3 owner) for oradata and redo directories
/oracle/oradata/01/DHMERM02
/oracle/redo/DHMERM02
4.    copy M03 init file to new home (10.2.0.3)
5.    change $ORACLE_HOME and $ORACLE_SID to new home and SID=M03
6.    startup nomount as ora10P1
7.    restore control file
restore controlfile from '/oracle/oradata/01/DHMERM02/backup/DHMERM03/2009-09-17/dhmerm03_cf_1ukpfn6a_1_1';
8.    mount database
9.    copy backup files or NFS
10.    catalog the backup pieces
catalog backuppiece '/oracle/oradata/01/DHMERM02/backup/DHMERM03/2009-09-17/dhmerm03_cf_1ukpfn6a_1_1';
catalog backuppiece '/oracle/oradata/01/DHMERM02/backup/DHMERM03/2009-09-17/dhmerm03_db_1mkpfesv_1_1';
11.    create soft links for oradata and redo directories to link original M03 paths to M02 directories.
Alternatively  you can use SET NEWNAME FOR DATAFILE|TEMPFILE to rename files.
12.    restore database with RMAN
run
{
ALLOCATE CHANNEL d1 DEVICE TYPE DISK;
ALLOCATE CHANNEL d2 DEVICE TYPE DISK;
restore database;
}
alter database open resetlogs;
13.    prepare create control file script to rename database and files
14.    shutdown M03 and startup nomount M02
15.    create control file and open M02 with resetlogs
16.    add file to TEMP tablespace
17.    shutdown M02
18.    change ownership of oradata and redo directories and files to ora10M2 (10.2.0.4 owner)
19.    upgrade M02 to 10.2.0.4
lsnrctl start listener_name
sqlplus / as sysdba
startup upgrade
@?/rdbms/admin/utlu102i.sql
@?/rdbms/admin/catupgrd.sql
shutdown immediate
startup
@?/rdbms/admin/utlrp.sql

2009-09-13

Convert database from Solaris to AIX on destination server

Oracle RDBMS 10.2.0.3

1. Make sure source and destination oracle homes are the same version.

2. Get destination platform name from
select PLATFORM_NAME from V$DB_TRANSPORTABLE_PLATFORM;

3. Open database read only and check if it is ok to convert.
startup mount;
alter database open read only;

set serveroutput on
declare
db_ready boolean;
begin
db_ready := dbms_tdb.check_db('AIX-Based Systems (64-bit)',0);
end;
/
-- if no message is returned it is ok to convert db

set serveroutput on
declare
external boolean;
begin
/* value of external is ignored, but with SERVEROUTPUT set to ON
* dbms_tdb.check_external displays report of external objects
* on console */
external := dbms_tdb.check_external;
end;
/

output:
The following directories exist in the database:
SYS.WORK_DIR, SYS.ADMIN_DIR, SYS.DATA_PUMP_DIR

4. Run RMAN command to create conversion scripts and init.ora file:
FORMAT defines location of init.ora file
DB_FILE_NAME_CONVERT defines final data files location on destination server.
If source database is not available (only backup) you can skip this step. Convert and transport scripts can be created manually.

CONVERT DATABASE ON TARGET PLATFORM
CONVERT SCRIPT '/oracle/oradata/01/DHMERM03/testdb/convertscript.rman'
TRANSPORT SCRIPT '/oracle/oradata/01/DHMERM03/testdb/transportscript.sql'
new database 'dhmerm10'
FORMAT '/oracle/oradata/01/DHMERM03/testdb/%U'
DB_FILE_NAME_CONVERT =('DHMERM03/testdb/dhmerm09','DHMERM04/testdb/dhmerm10');

5. Prepare environment file. Unset LD_LIBRARY_PATH and LD_LIBRARY_PATH_64 for AIX.
6. Copy init.ora file to destination server $ORACLE_HOME/dbs, rename and edit.
Make sure *dump directories exist.

7.
Copy convertscript.rman to destination server.
Copy transportscript.sql to destination server.


8. Identify datafiles that contain undo data by running the following query:

select distinct(file_name)
from dba_data_files a, dba_rollback_segs b
where a.tablespace_name=b.tablespace_name;

Copy unconverted data files (system, undo) into stage area on destination server.
Copy rest datafiles to final location on destination server.

9. Edit the convert script and leave the CONVERT DATAFILE commands for only the datafiles that contain undo data.
CONVERT DATAFILE points to unconverted files (local stage area or NFS to source file system).
FORMAT defines final destination.
You can convert all datafiles, especially if source database is not available and you are not sure about undo segments.

RUN {
CONVERT DATAFILE '/oracle/oradata/01/DHMERM04/testdb/stage/system01.dbf'
FROM PLATFORM 'Solaris[tm] OE (64-bit)'
FORMAT '/oracle/oradata/01/DHMERM04/testdb/dhmerm10/system01.dbf';

CONVERT DATAFILE '/oracle/oradata/01/DHMERM04/testdb/stage/undotbs01.dbf'
FROM PLATFORM 'Solaris[tm] OE (64-bit)'
FORMAT '/oracle/oradata/01/DHMERM04/testdb/dhmerm10/undotbs01.dbf';
}

10. Create script for temporary control file create_temp_cf.sql. Datafile path must be current, unconverted files.
You can create it from transport script or from trace backup (alter database backup controlfile to trace).

CREATE CONTROLFILE REUSE SET DATABASE "DHMERM10" RESETLOGS NOARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 '/oracle/oradata/01/DHMERM03/testdb/dhmerm10/redo01.log' SIZE 50M,
GROUP 2 '/oracle/oradata/01/DHMERM03/testdb/dhmerm10/redo02.log' SIZE 50M,
GROUP 3 '/oracle/oradata/01/DHMERM03/testdb/dhmerm10/redo03.log' SIZE 50M
DATAFILE
'/oracle/oradata/01/DHMERM04/testdb/stage/system01.dbf',
'/oracle/oradata/01/DHMERM04/testdb/stage/undotbs01.dbf',
'/oracle/oradata/01/DHMERM04/testdb/dhmerm10/sysaux01.dbf',
'/oracle/oradata/01/DHMERM04/testdb/dhmerm10/users01.dbf'
CHARACTER SET AL32UTF8
;

11. Check environment variables:
echo $ORACLE_SID
echo $ORACLE_HOME

12. Startup instance on destination server and create temporary control file.
sqlplus / as sysdba
startup nomount
@create_temp_cf.sql

13. Run conversion:
rman target /
@convertscript.rman
shutdown immediate;

14. Configure listener.
15. Create password file.
16. Edit transport script, change names for log and temp files, check other paths.
sqlplus / as sysdba
@transportscript.sql

Transport script:


STARTUP NOMOUNT
CREATE CONTROLFILE REUSE SET DATABASE "DHMERM10" RESETLOGS NOARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 100
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 '/oracle/oradata/01/DHMERM04/testdb/dhmerm10/redo01.log' SIZE 50M,
GROUP 2 '/oracle/oradata/01/DHMERM04/testdb/dhmerm10/redo02.log' SIZE 50M,
GROUP 3 '/oracle/oradata/01/DHMERM04/testdb/dhmerm10/redo03.log' SIZE 50M
DATAFILE
'/oracle/oradata/01/DHMERM04/testdb/dhmerm10/system01.dbf',
'/oracle/oradata/01/DHMERM04/testdb/dhmerm10/undotbs01.dbf',
'/oracle/oradata/01/DHMERM04/testdb/dhmerm10/sysaux01.dbf',
'/oracle/oradata/01/DHMERM04/testdb/dhmerm10/users01.dbf'
CHARACTER SET AL32UTF8
;
ALTER DATABASE OPEN RESETLOGS;
ALTER TABLESPACE TEMP ADD TEMPFILE '/oracle/oradata/01/DHMERM04/testdb/dhmerm10/temp01.dbf' SIZE 20971520 AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;
SHUTDOWN IMMEDIATE
STARTUP UPGRADE
@@ ?/rdbms/admin/utlirp.sql
SHUTDOWN IMMEDIATE
STARTUP
-- The following step will recompile all PL/SQL modules.
@@ ?/rdbms/admin/utlrp.sql

Useful info:
Metalink 415884.1 Cross Platform Database Conversion with same Endian
Metalink 414878.1 Cross-Platform Migration on Destination Host Using Rman Convert Database
Metalink 732053.1 Avoid Datafile Conversion during Transportable Database
Metalink 417455.1 Datafiles are not converted in parallel for transportable database
Platform Migration using Transportable Database
RMAN Cross-Platform Transportable Databases and Tablespaces