Archive

Author Archive

Perl – Writing to file outputting in UTF-16 instead of UTF-8

September 16, 2011 2 comments

Recently I needed to write a perl script that ran on Cygwin. My default setting means that any files written by perl were being written in UTF-16. This led to what appeared to be a lot of Japanese writing, making it completely illegible and unusable.

After a lot of digging around on the internet, I managed to hack together the following code:

open my $SH, ">>:raw:encoding(UTF16-LE):crlf:utf8", "test1.txt";;
print $SH "\x{FEFF}";
print "Some test writing \n";
close ($SH);

This code tells perl we’re going to pass “characters” to this file handle instead of bytes. Next transform \n into \r\n to give DOS line endings. Next apply the UTF16-LE, so that 0x0A becomes 0x0A 0x00. This stops perl writing a byte order mark (BOM) at the beginning of the file. Finally, the raw:encoding removes the default ctrf so that it is not in the wrong place.

Now the file is being opened with the correct coding, we need to write the BOM to the beginning of the file to tell readers of this file what endianness it is. We do this by printing \x{FFF} to the file.

Oracle – Find consuming sql from process id

September 5, 2011 5 comments

If you find an oracle process is consuming a high amount of CPU, you can find the sql_text that is related to that process id using the following code:

SELECT SQL_TEXT
FROM v$sql
WHERE sql_id = (SELECT sql_id FROM v$session WHERE paddr = (sELECT addr
FROM v$process
WHERE spid = '&process_id'));

You will need to find the process id using either top (linux), topas -P (aix), or task manager (windows). Equally you can use Enterprise Manager to find this information, by looking under the performance monitor and any alerts regarding tuning / high CPU consumption.

Oracle – Disable dbms gather_stats_job or optimizer stats


To disable gather_stats_job in 10g:

BEGIN
DBMS_SCHEDULER.DISABLE('GATHER_STATS_JOB');
END;
/

To disable gather_stats_job (also known as optimizer stats) in 11g:

BEGIN
DBMS_AUTO_TASK_ADMIN.DISABLE(
client_name => 'auto optimizer stats collection',
operation => NULL,
window_name => NULL);
END;
/

Oracle – RMAN Restore Database


If you have a current controlfile, spfile, but all datafiles have been lost or damaged, then you must restore and recover the entire database.

RMAN> STARTUP MOUNT

You can use the show all command to check which channels are configured for access to backup devices. If none are configured then you will need to manually allocate one or more channels.

RMAN> SHOW ALL

Restore the database using the restore database command:

RMAN> RESTORE DATABASE;

If you have any read-only tablespaces you will need to force RMAN to restore any missing datafiles belonging to them. This is because RMAN typically skips read-only tablespaces on a restore.

To do this you can issue the following instead of the restore command above:

RMAN> RESTORE DATABASE CHECK READONLY;

Recover the database using the recover database command:

RMAN> RECOVER DATABASE;

NB. You can also recover the database appending delete archivelog maxsize <number> this will delete archivelogs after they have been applied and will prevent them from being bigger than the maxsize indicated. It should be noted that if the maxsize of the archivelogs is greater than the size given here, then the statement will give an error and you will need to increase the maxsize in the statement.

Once you have successfully recovered the database, you can open it from RMAN:

RMAN> ALTER DATABASE OPEN;

Further Reading

Basic Database Restore and Recovery Scenarios – Oracle Documentation 10.1

Oracle – Find View Source Code

August 21, 2011 1 comment

If you wish to find the source code for a view, you can look in the user_views table:

SQL> SELECT view_name, text FROM user_views;

You can check the v$fixed_view_definition table to check the definition of fixed views (views starting with v$).

SQL> SELECT view_name, view_definition FROM v$fixed_view_definition;

Further Reading

http://download.oracle.com/docs/cd/B19306_01/server.102/b14237/dynviews_1109.htm

Oracle – Backup Controlfile

August 20, 2011 1 comment

It is necessary, from time to time, to backup the controlfile. This can be done when the database is open or mounted.

Backup controlfile using SQL

If you wish to backup the controlfile using SQLPLUS then you can issue the following to backup the controlfile to a binary file:

SQL> ALTER DATABASE BACKUP CONTROLFILE TO '/oracle/backup/control.bkp';

This create a duplicate of the current controlfile.

However, if you wish to produce SQL statements that can later be used to re-create the controlfile, you can issue the following:

SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE;

Backup controlfile using RMAN

RMAN will automatically backup the controlfile and server parameter file after every backup and after structural database changes, if CONFIGURE CONTROLFILE AUTOBACKUP is set to ON. It is set to OFF by default.

The controlfile autobackup contains metadata about the previous backup, which is essential for database recovery.

Manually backup controlfile

RMAN> BACKUP CURRENT CONTROLFILE;

It should be noted that a manual backup of the controlfile cannot be automatically restored. This is because it only contains RMAN repository data for backups within the current RMAN session. However, if controlfile autobackup is set to on, RMAN will manually backup the controlfile then it will create an autobackup of the controlfile and server parameter file after the manual backup has completed so it can record the latest backup information.

If you wish to include the current controlfile in a backup then you can perform something like the following:

RMAN> BACKUP DEVICE TYPE sbt TABLESPACE emp INCLUDE CURRENT CONTROLFILE;

If autobackup is enabled then RMAN performs and autobackup of the controlfile after the backup, so the controlfile backup contains details of the latest backup taken.

Backup controlfile as copy

You can also backup the controlfile as a copy. You can use either of the below commands for creating a backup controlfile copy:

RMAN> BACKUP AS COPY CURRENT CONTROLFILE FORMAT '/tmp/controlfile_copy.ctl';

RMAN> BACKUP DEVICE TYPE sbt CONTROLFILECOPY  '/tmp/controlfile_copy.ctl';

Futher Reading

Managing Control Files – Oracle Documentation

Backing Up Database Files and Archived Logs With RMAN – Oracle Documentation

Oracle – PL/SQL decode function

August 19, 2011 1 comment

The decode function acts like IF…THEN…ELSE and can be used within PL/SQL code and SQL code much the same.

The syntax is as follows:

decode(expression, search, result [, search , result]... [, default] )

Example:

SELECT employee_name
DECODE(employee_id, 1, 'Alvin'
2, 'Simon'
Theodore') result
FROM employees;

The above code acts like the following:

IF employee_id = 1 THEN
result:= 'Alvin'
ELSIF employee_id = 2 THEN
result:= 'Simon'
ELSE
result:= 'Theodore'
END IF;

The decode will compare each employee_id one by one.