Tech

Transfer Oracle Views Into PostgreSQL Format

Conversion of view is an important part of Oracle to PostgreSQL database migration. Considering the fact that syntaxes of queries in Oracle and PostgreSQL aren’t similar and even these two DBMS possess distinguished sets of inbuilt features, it is important to transform SQL statement of every query prior to moving it to the destination database.

Oracle offers several features for developing views which aren’t recognized by PostgreSQL. These features needs to be eliminated throughout conversion:

  1. DEFAULT
  2. FORCE / NO FORCE
  3. WITH CHECK OPTION
  4. WITH OBJECT IDENTIFIER
  5. WITH READ ONLY
  6. UNDER
  7. XMLType views are not supported by PostgreSQL

Oracle has a unique one-row and one-column table DUAL to manage some queries which does not call for any table, for instance:

SELECT 1 FROM DUAL;

PostgreSQL does not have such table, so it must be created for possible use in queries:

create table dual (varchar(1) not null );

insert into dual(dummy) values(‘x’);

Additionally, all embedded Oracle features that are missing in PostgreSQL need to be replaced by the right equivalents.

  1. Oracle function CURTIME() is replaced by LOCALTIME or CURRENT_TIME in PostgreSQL
  2. All occurrences of DAY($a) or DAYOFMONTH($a) must be converted into the following expression EXTRACT(day from date($a))::integer
  3. Function DateAdd($Date, $Format, $Days, $Months, $Years) can be converted into this expression:

Date + cast(‘$Days day’ as interval) + cast(‘$Months months’ as interval) + cast(‘$Years years’ as interval)

  1. Oracle function DECODE does not have direct equivalent in PostgreSQL. It must be replaced by CASE-WHEN-THEN-ELSE expression as illustrated by example below. Oracle expression:

SELECT colorid, DECODE(colorid, ‘1’, ‘white’, ‘2’, ‘black’, 3, ‘red’, NULL) AS ‘colorname’ FROM colors

In PostgreSQL the same query should be as follows:

SELECT colorid, CASE colorid

WHEN ‘1’ THEN ‘white’

WHEN ‘2’ THEN ‘black’

WHEN ‘3’ THEN ‘red’

ELSE NULL

END AS ‘colorname’

FROM colors

  1. Simplified format of using Oracle function INSTR($str1, $str2) can be replaced by POSITION($str2 in $str1). More detailed porting of this function is clearly started below
  2. LCASE is Oracle function that makes string lowercase, it must be converted into LOWER in PostgreSQL
  3. All occurrences of LOCATE($str1,$str2), Oracle function searching occurrence of $str1 int $str2,  must be replaced by POSITION($str1 in $str2) in PostgreSQL
  4. Function SUBSTR($string, $from, $for) is converted into SUBSTRING($string, $from, $for)
  5. NVL($a, replace_with) can be converted into COALESCE($a, replace_with)
  6. Oracle function RAND must be replaced by PostgreSQL equivalent RANDOM
  7. Regexp support function REGEXP_LIKE($string, $pattern) is converted into PostgreSQL expression $string LIKE $pattern
  8. All occurrences of SYSDATE must be replaced by PostgreSQL equivalent CURRENT_DATE or NOW()::date
  9. Oracle function UCASE, Oracle function that makes string uppercase, is converted into PostgreSQL synonym UPPER
  10. All occurrences of SYS_GUID() can be replaced by uuid_generate_v1(). In early versions of PostgreSQL (before v9.4) this function depended on the OSSP UUID library. In order to get version-independent solution, the following expression may be used:

SELECT md5(random()::text || clock_timestamp()::text)::uuid

  1. WEEK($a) is converted into EXRACT(week from date($a))
  2. Oracle function YEAR($a) must be replaced by EXRACT(year from date($a))

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close
Close