Subtle Art of Code Conversion in Oracle to PostgreSQL Migration.

In any database migration project, code conversion plays a critical role in achieving overall success and instilling confidence. During an Oracle to PostgreSQL migration, I am aware of conversion tools such as AWS Schema Conversion Tool and Ora2pg. These tools generally perform well in converting the dialect of the code. However, there are instances when we come across complex proprietary features or intricate code that prove to be challenging for automated conversion tools. In such cases, manual conversion becomes necessary to tackle these complexities effectively.

Code conversion is the subtle art of understanding the source engine and dissecting it with knowledge of the target engine.

Code conversion is about understanding how a code functions in the source and transforming it into the target dialect to achieve a similar result or functionality. This process involves dissecting and analyzing the intricacies of the source engine and applying that knowledge to effectively adapt the code to the target engine.

Let’s consider a sample code that I encountered during an Oracle to PostgreSQL migration. This code includes hierarchical queries and string functions, making it complex with multiple layers. Such code can pose a challenge for conversion tools.

create or replace function  fun_check_string_common(
                pi_str1 IN VARCHAR2,
                pi_delimiter IN VARCHAR2)
   n_cnt PLS_INTEGER := 0;
   INTO n_cnt
    (SELECT TRIM( SUBSTR ( txt , INSTR (txt,pi_delimiter, 1, LEVEL ) + 1 , INSTR (txt, pi_delimiter, 1, LEVEL+1 ) - INSTR (txt, pi_delimiter, 1, LEVEL) -1 ) ) AS u
          ( SELECT pi_delimiter||pi_str1||pi_delimiter AS txt FROM dual
          CONNECT BY LEVEL <= LENGTH(txt)-LENGTH(REPLACE(txt,pi_delimiter,''))-1
     ) );
RETURN n_cnt;
                  RETURN -1;
END fun_check_string_common;

As a conversion engineer, if I were to approach it literally and try to convert the code line by line, it could become a tedious task. I’m not saying it’s impossible, but it would likely require a significant amount of time. One crucial aspect of the conversion process is working backwards. This means that sometimes we need to consider why the code was originally built and what functionality it was intended to achieve. By understanding the original intent, we can approach the conversion more strategically.

When examining the sample Oracle code, it appears to involve string manipulation and serves as a utility within the database. Conducting a quick evaluation with sample data can provide us with some insights about its functionality.

select fun_check_string_common('abc_123_abc','_') from dual;
select fun_check_string_common('abc_123','_') from dual;
select fun_check_string_common('ab','_') from dual;




The initial test provides a fair understanding of the expected functionality of the source Oracle function.

Return the count of splits that can be created based on the input delimiter.

By leveraging our understanding of the expected code functionality, we can reconstruct it by utilizing the capabilities of the target engine instead of converting the code line by line. In PostgreSQL, we can achieve similar functionality by using functions like regexp_split_to_array and unnest.

[ora_migrate] > select count(1) from (select unnest(a) from (SELECT regexp_split_to_array('ABC_123_ABC','_') ) as dt(a)) alias1;
(1 row)

[ora_migrate] > select count(1) from (select unnest(a) from (SELECT regexp_split_to_array('ABC_123','_') ) as dt(a)) alias1;
(1 row)

[ora_migrate] > select count(1) from (select unnest(a) from (SELECT regexp_split_to_array('AB','_') ) as dt(a)) alias1;
(1 row)

Overall, code conversion requires a understanding of both the source and target engines, careful analysis of the code intricacies, and strategic thinking to achieve successful migrations and maintain functionality.

About Deepak Mahto

Database Guy with expertise in database performance, migration and Cloud Adoption.
This entry was posted in Oracle to PG migration, postgresql and tagged , , , . Bookmark the permalink.

2 Responses to Subtle Art of Code Conversion in Oracle to PostgreSQL Migration.

  1. Peter says:

    There is no need to unnest the array:
    select cardinality(regexp_split_to_array(‘ABC_123_ABC’,’_’));

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s