How to successfully migrate from Oracle to Postgres Pro Enterprise

Migrating schemas and data from Oracle DB to Postgres Pro Enterprise presents a myriad of challenges, often resembling a rocky road filled with potential pitfalls. However, the journey becomes significantly more manageable thanks to the extended features of Postgres Pro Enterprise, which replicate certain Oracle DBMS mechanisms that are absent in the standard PostgreSQL version. While the open-source utility ora2pg offers automatic migration capabilities, it falls short in supporting these advanced features. This gap led to the creation of ora2pgpro, a tool specifically designed for seamless migration from Oracle to Postgres Pro Enterprise, taking its unique capabilities into account.

Roadblocks when migrating to vanilla PostgreSQL

The initial challenge in any migration process is determining which database objects need to be exported. Essential components underpinning business logic—such as tables, indexes, PL/SQL functions, autonomous transactions, packages, and schemas—must be carefully considered. Ensuring that the target database technologies align with the functionality of Oracle DB is crucial; otherwise, the business logic may not operate as intended. Notably, vanilla PostgreSQL lacks native support for packages and autonomous transactions, complicating the migration process. In contrast, Postgres Pro Enterprise addresses these limitations effectively.

Given that the database structure often encapsulates intricate business logic, manual conversion is not a viable option due to the extensive time investment it would require. This necessity has driven the development of automated database converters like ora2pg and ora2pgpro.

What are ora2pg and ora2pgpro?

Ora2pg is a free utility that automates the conversion of Oracle DB schemas to PostgreSQL, offering extensive functionality to streamline most schema migration tasks. It can export a diverse array of database objects, including tables, user rights, schemas, BLOBs, and PL/SQL code, while also estimating migration costs by identifying objects that cannot be converted automatically. However, when it comes to migrating to Postgres Pro Enterprise, ora2pgpro steps in as the commercial counterpart, designed to accommodate Oracle-like features present in Postgres Pro Enterprise.

The enhancements that distinguish ora2pgpro include:

  1. Support for autonomous transactions in Postgres Pro Enterprise.
  2. Support for Postgres Pro Enterprise packages.
  3. Support for associative arrays via the enterprise extension pg_variables.
  4. A more sophisticated algorithm for translating PL/SQL into PL/pgSQL.

Next, we will delve into common migration challenges encountered with ora2pg and how ora2pgpro effectively addresses these issues when migrating to Postgres Pro Enterprise.

Support for autonomous transactions

While ora2pg attempts to simulate autonomous transactions, vanilla PostgreSQL lacks this feature entirely. Postgres Pro Enterprise, on the other hand, offers native support for autonomous transactions, which are sub-transactions that can be committed or rolled back independently of their parent transaction. This capability allows results, such as logging or auditing, to be visible immediately after completion, even if the parent transaction is still in progress or later rolled back. Imitating these transactions through methods like dblink or pg_background can negatively impact performance due to the overhead of opening new connections or launching background processes.

Recognizing these limitations, we have implemented autonomous transactions directly within the Enterprise core, resulting in a significantly faster solution. Ora2pg, however, cannot leverage this functionality. In contrast, ora2pgpro can directly export autonomous transactions in Postgres Pro format with a simple configuration change: set POSTGRESPRO_ATX=1.

Support for packages

Another major hurdle in migration is the absence of package support in vanilla PostgreSQL. While ora2pg does provide a workaround for package export by simulating packages using schemas, this approach introduces considerable overhead and can lead to performance issues and semantic mismatches. Postgres Pro Enterprise addresses this gap by incorporating true package support at the core level, complete with real package variables and scoping. Unfortunately, ora2pg cannot export into this model.

Ora2pgpro, however, fills this void by allowing the direct export of Oracle packages as Postgres Pro packages. To enable this feature, users simply need to set TYPE=PACKAGE in the configuration. This functionality ensures that each Oracle package is automatically converted into its own schema, preserving modularity and encapsulation. Unlike ora2pg’s workaround, ora2pgpro supports automatic package initialization via init() and maintains public/private method separation, delivering high-performance operation in the Postgres Pro Enterprise environment.

Support for associative arrays

Similar to packages, vanilla PostgreSQL does not support indexed collections, complicating the migration process. Consequently, ora2pg struggles to interpret collections accurately. To address this, we developed the pg_variables module, which includes associative array support. Ora2pgpro maps Oracle associative arrays directly onto pg_variables collections, translating Oracle collection methods into pg_variables function calls.

For instance:

  • Setting an array element in Oracle translates to pgv_set_elem in pg_variables.
  • Removing an element corresponds to pgv_remove_elem.
  • Checking for element existence is done using pgv_exists_elem.

Support for VARRAYs

In addition to associative arrays, ora2pgpro can accurately interpret VARRAYs, which often pose challenges for the open-source ora2pg. Through advanced syntactic analysis, ora2pgpro effectively handles VARRAYs, mapping them to either native Postgres Pro Enterprise arrays or pg_variables as necessary. This capability allows for the correct translation of VARRAY operations into equivalent PL/pgSQL constructs, ensuring that complex data structures are preserved during migration.

Advanced PL/SQL → PL/pgSQL translation

Migration issues frequently arise when converting PL/SQL code into PL/pgSQL. Although ora2pg can technically perform this translation, its reliance on basic Perl regular expressions often results in inaccurate conversions. In contrast, ora2pgpro employs a robust syntax and semantic analysis approach, enabling it to parse PL/SQL code accurately and translate complex constructs effectively.

The translation process follows a structured pipeline:

  1. Extract PL/SQL code from Oracle DB.
  2. Parse the code into an Abstract Syntax Tree (AST).
  3. Convert the AST into PL/pgSQL code.
  4. Provide warnings for any unconvertible fragments, requiring manual review by the developer.

This AST-based methodology ensures a higher accuracy of conversion and allows for future enhancements to support new constructs. It is important to note that ora2pgpro is a commercial product, and unauthorized modifications to its code are prohibited under licensing terms.

Handling complex package specifications

One of ora2pgpro’s standout features is its ability to analyze the syntax and semantics of PL/SQL code, enabling it to translate even the most complex package specifications accurately. Unlike ora2pg, which relies on primitive regular expressions, ora2pgpro’s AST-based approach delivers a far superior level of precision in conversion.

For example, ora2pgpro can interpret temporal data types with varying precision, time zones, and intervals, as well as complex Boolean expressions and user-defined data types. This capability ensures that all elements are properly mapped, preserving the semantic integrity of Oracle data types in Postgres Pro Enterprise and minimizing the need for manual adjustments post-migration.

Tech Optimizer
How to successfully migrate from Oracle to Postgres Pro Enterprise