top of page

Oracle to PostgreSQL fine-tuning and customization

When migrating complex databases from Oracle to PostgreSQL, there are several factors that require more fine-tuning and customization. Below are key areas where extra attention is needed, along with the adjustments you may need to make:


1. Data Types Mapping


  • Oracle and PostgreSQL support different sets of data types, and not all types have a one-to-one mapping. For complex databases, some types might require manual handling.


Examples:

  • NUMBER: Oracle's NUMBER can map to various types in PostgreSQL depending on precision (NUMERIC, DECIMAL, or INTEGER).

  • CLOB/BLOB: Oracle’s large object types (CLOB, BLOB) should be converted into PostgreSQL’s TEXT, BYTEA, or custom large object types.

  • DATE and TIMESTAMP: Oracle’s DATE may behave slightly differently in PostgreSQL and might need mapping to TIMESTAMP or TIMESTAMPTZ in PostgreSQL to account for time zone differences.

You may need to define custom mappings in ora2pg:

bash

DATA_TYPE DATE DATE DATA_TYPE NUMBER NUMERIC


2. PL/SQL to PL/pgSQL


  • Oracle’s PL/SQL procedures, functions, and packages do not translate directly to PostgreSQL's PL/pgSQL.

  • Complex stored procedures with Oracle-specific syntax (like CURSORS, LOOPS, and EXCEPTIONS) must be rewritten manually or using a tool with partial automation.


Examples of changes:

  • Oracle:

    plsql

    FOR rec IN (SELECT col FROM table) LOOP -- Processing END LOOP;

  • PostgreSQL:

    pgsql

    FOR rec IN SELECT col FROM table LOOP -- Processing END LOOP;


Additionally, Oracle has built-in packages such as DBMS_OUTPUT and DBMS_SCHEDULER, which need custom implementation or conversion in PostgreSQL.


3. Sequences and Auto-Increment


  • Oracle handles auto-incrementing fields with sequences, while PostgreSQL uses SERIAL or BIGSERIAL data types.

  • When migrating tables with sequence-based primary keys, you’ll need to manually create equivalent sequences in PostgreSQL or adjust the schema to use SERIAL or IDENTITY columns.


Example:

  • In Oracle:

    CREATE SEQUENCE my_seq START WITH 1;

  • In PostgreSQL:

    sql

    CREATE SEQUENCE my_seq START WITH 1; ALTER TABLE my_table ALTER COLUMN id SET DEFAULT nextval('my_seq');


4. Triggers


  • Oracle's trigger logic may need to be re-implemented in PostgreSQL. The syntax and functionality of triggers can differ significantly.


Example of changes:

  • Oracle:

    sql

    CREATE OR REPLACE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW BEGIN -- Logic END;

  • PostgreSQL:

    sql

    CREATE FUNCTION trigger_function() RETURNS trigger AS $$ BEGIN -- Logic RETURN NEW; END; $$ LANGUAGE plpgsql; CREATE TRIGGER trigger_name BEFORE INSERT ON table_name FOR EACH ROW EXECUTE FUNCTION trigger_function();


5. Partitioning


  • Oracle supports partitioned tables natively, while PostgreSQL has a different approach to partitioning. If your Oracle database uses table partitioning, you may need to modify the implementation to align with PostgreSQL's declarative partitioning.


Example:

  • Oracle:

    sql

    CREATE TABLE partitioned_table ( id NUMBER, value VARCHAR2(50) ) PARTITION BY RANGE(id) ( PARTITION p1 VALUES LESS THAN (1000), PARTITION p2 VALUES LESS THAN (2000) );

  • PostgreSQL:

    sql

    CREATE TABLE partitioned_table ( id INT, value VARCHAR(50) ) PARTITION BY RANGE (id); CREATE TABLE partition_p1 PARTITION OF partitioned_table FOR VALUES FROM (MINVALUE) TO (1000); CREATE TABLE partition_p2 PARTITION OF partitioned_table FOR VALUES FROM (1000) TO (2000);


6. Indexing and Performance Optimization


  • Complex queries, joins, and indexes in Oracle may not perform optimally in PostgreSQL without tuning. PostgreSQL’s optimizer differs from Oracle’s, so you may need to revise indexing strategies.

  • Oracle often uses Bitmap indexes and Function-based indexes, which may need to be replaced with appropriate indexing strategies in PostgreSQL.

  • After migration, you might need to analyze your database and adjust your indexes for better performance:

    bash

    ANALYZE; VACUUM FULL;


7. Transactional Differences


  • Oracle's handling of transactions, isolation levels, and concurrency control may differ from PostgreSQL’s. Be mindful of how PostgreSQL handles COMMIT, ROLLBACK, and transaction visibility, and adjust transactional logic in your application accordingly.


8. Materialized Views


  • If your Oracle database uses materialized views, you will need to recreate them in PostgreSQL, but note that PostgreSQL's materialized views don’t automatically refresh like Oracle’s.

  • You can use triggers or scheduled jobs to refresh materialized views in PostgreSQL.

Example in PostgreSQL:

sql

REFRESH MATERIALIZED VIEW my_view;


9. Job Scheduling


  • Oracle’s DBMS_SCHEDULER and DBMS_JOB are used to schedule jobs and maintenance tasks, while PostgreSQL uses pg_cron or external cron jobs.

  • Replace Oracle’s job scheduling functionality with pg_cron or other similar tools.


10. Advanced Features


  • Features like Oracle’s Flashback queries, Global Temporary Tables, or Advanced Queuing (AQ) do not have direct equivalents in PostgreSQL and may require additional effort for emulation or alternative solutions.


11. Third-Party Tools

  • For complex migrations, it might be beneficial to use professional migration tools such as AWS Schema Conversion Tool, EnterpriseDB Migration Toolkit, or others that provide automation and insights for large, complex databases.


12. Testing and Validation


  • Thoroughly test the migrated database. Complex queries, especially those involving analytical functions, might require optimization.

  • You may need to rewrite certain complex queries that were optimized for Oracle but perform poorly on PostgreSQL.


By understanding these challenges and implementing the necessary changes, you can ensure that the migration of a complex Oracle database to PostgreSQL is successful and performs optimally. Testing is crucial at every step of this process, as even small differences between the two systems can impact application behavior or performance.

12 views

Recent Posts

See All

Comments


AiTech

©2023 by AiTech

bottom of page