datatrend®

Case study · 2021 – 2024

Migration from Oracle 19c to PostgreSQL at an industrial company (building supplier)

CRM and reporting database with a specialist application, reporting, ticket and knowledge systems – 37 schemas, around 4,900 objects, database code from more than ten years.

  • 4.910Objects in the DDL dump
  • 37Schemas
  • 1.823Tables
  • 1.154Indexes
  • 616Views
  • 375Sequences
  • 229Synonyms
  • 201Procedures and functions
  • 27Packages
  • 12Materialized Views

Key figures from the evaluation of the structure dump. All excerpts shown are abbreviated; schema, table and field names have been renamed neutrally. No content from customer data is shown.

From the project

The 10 biggest problems – and how we solved them

Each of these problems occurred in hundreds of objects. They were not solved by hand but by our set of rules, which rewrites every object, executes it in a test database and corrects errors automatically.

  1. 1Oracle outer joins with (+) 224 objects, 1,094 occurrences

    Problem: PostgreSQL does not know the old Oracle notation for outer joins. It is found in views, reports and procedures – often mixed with normal joins and in chains across several tables.

    Solution: Automatic conversion to ANSI joins: mandatory tables become INNER JOIN, optional ones LEFT OUTER JOIN, and the order of the chains is derived from the conditions. Special cases (e.g. (+) within OR conditions) are flagged.

    Oracle (before)
    SELECT pg.partner_id, pa.status, ps.sortierung
      FROM vertrieb.partner_gruppe pg, vertrieb.partner pa,
           vertrieb.partner_status ps
     WHERE pg.partner_id = pa.partner_id
       AND pa.status = ps.status(+)
    PostgreSQL (after)
    SELECT pg.partner_id, pa.status, ps.sortierung
      FROM vertrieb.partner_gruppe pg
     INNER JOIN vertrieb.partner pa ON pg.partner_id = pa.partner_id
      LEFT OUTER JOIN vertrieb.partner_status ps ON pa.status = ps.status
  2. 2Data types and storage clauses 1,845 objects with VARCHAR2, 697 with NVARCHAR2

    Problem: NUMBER without precision, NVARCHAR2, lengths in characters instead of bytes (VARCHAR2(10 CHAR)), DATE with time, CLOB/BLOB/LONG RAW – plus storage specifications such as TABLESPACE, PCTFREE or STORAGE that PostgreSQL does not understand.

    Solution: Mapping via a rules table with more than 200 data types: NUMBER(p,0) to smallint, integer, bigint or numeric depending on size, character strings in character length, DATE to timestamp(0). Storage clauses are dropped.

    Oracle (before)
    "ID" NUMBER(22,0) NOT NULL ENABLE,
    "TEXT" NVARCHAR2(1000),
    "ART" VARCHAR2(10 CHAR) NOT NULL ENABLE,
    "ERLEDIGT" NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,
    "NOTIZ" CLOB
    ) PCTFREE 10 NOCOMPRESS TABLESPACE "USERS"
    PostgreSQL (after)
    id numeric(22) NOT NULL,
    "text" varchar(1000),
    art varchar(10) NOT NULL,
    erledigt smallint DEFAULT 0 NOT NULL,
    notiz text
    )
  3. 3Names in upper case and with special characters all 4,910 objects

    Problem: Oracle stores names in upper case, PostgreSQL in lower case. On top of that there are names with #, umlauts, keywords such as USER and names in mixed case that only work in quotation marks.

    Solution: A uniform rule for every name: lower case (ß and umlauts are kept), # becomes _h_, keywords and mixed case remain in quotation marks, names longer than 63 bytes are shortened uniquely – the same everywhere, including in the code.

    Oracle (before)
    "STATUS#" NUMBER(2,0),
    "ÄNDERUNG_AM" DATE DEFAULT sysdate,
    "USER" VARCHAR2(30 CHAR),
    "Bemerkung" NVARCHAR2(200)
    PostgreSQL (after)
    status_h_ smallint,
    "änderung_am" timestamp(0) DEFAULT LOCALTIMESTAMP(0),
    "user" varchar(30),
    "Bemerkung" varchar(200)
  4. 4SYSDATE and date arithmetic 366 objects, 2,819 occurrences; ADD_MONTHS in 171 objects

    Problem: Oracle calculates "date minus number" in days and knows ADD_MONTHS, LAST_DAY, TRUNC(date) and month names via NLS parameters. In PostgreSQL these functions do not exist or behave differently.

    Solution: SYSDATE becomes LOCALTIMESTAMP(0); a compatibility schema provides ADD_MONTHS (including month end), LAST_DAY, MONTHS_BETWEEN, TRUNC and day arithmetic with Oracle behaviour. NLS parameters are removed and flagged.

    Oracle (before)
    WHERE p.erfasst <= LAST_DAY(ADD_MONTHS(SYSDATE, -2))
      AND p.geaendert > SYSDATE - 14
    PostgreSQL (after)
    WHERE p.erfasst <= LAST_DAY(ADD_MONTHS(LOCALTIMESTAMP(0), -2))
      AND p.geaendert > LOCALTIMESTAMP(0) - 14
  5. 5Synonyms across 37 schemas 229 synonyms

    Problem: Reports access tables in other schemas via synonyms. PostgreSQL has no synonyms.

    Solution: Synonyms on tables and views become views in the respective schema; synonyms on code are resolved via the search path, and synonyms on database links are reported separately.

    Oracle (before)
    CREATE SYNONYM "BERICHT"."PARTNER"
       FOR "VERTRIEB"."PARTNER";
    PostgreSQL (after)
    CREATE OR REPLACE VIEW bericht.partner AS
      SELECT * FROM vertrieb.partner;
  6. 6ROWNUM for limiting 108 objects, 682 occurrences

    Problem: Oracle limits results with ROWNUM – mostly in subqueries ("first activity"). PostgreSQL does not know ROWNUM; a simple LIMIT is only correct if there is no grouping or sorting beforehand.

    Solution: ROWNUM in the condition becomes LIMIT; if there is a grouping, an aggregate or a sort beforehand, the limit is applied exactly as in Oracle. ROWNUM in the output becomes row_number().

    Oracle (before)
    (SELECT a.art FROM vertrieb.aktivitaet a
      WHERE a.projekt_id = p.projekt_id
        AND ROWNUM < 2) AS erste_art
    PostgreSQL (after)
    (SELECT a.art FROM vertrieb.aktivitaet a
      WHERE a.projekt_id = p.projekt_id
      LIMIT 1) AS erste_art
  7. 7Empty text is NULL in Oracle 68 objects

    Problem: In Oracle, '' is the same as NULL: a comparison such as planer_nr != '' is never true there. In PostgreSQL it would be true – the results would change unnoticed.

    Solution: Comparisons with '' are rewritten so that the Oracle result is preserved and flagged for functional review. Concatenations with NULL (Oracle: 'a' || NULL = 'a') behave as in Oracle via the compatibility schema.

    Oracle (before)
    WHERE va.planer_nr != ''
      AND va.art = 'MAI'
    PostgreSQL (after)
    WHERE va.planer_nr != NULL   -- wie Oracle: nie wahr
      AND va.art = 'MAI'
  8. 8Lists via XMLAGG 34 objects

    Problem: Comma-separated lists were built with XMLAGG/XMLELEMENT and EXTRACT – an Oracle peculiarity without a direct counterpart.

    Solution: Detection of the pattern and conversion to string_agg with the same sort order; the result is identical and the query considerably faster.

    Oracle (before)
    RTRIM(XMLAGG(XMLELEMENT(e, g.gebiet || ',')
          ORDER BY g.gebiet).EXTRACT('//text()'), ',')
    PostgreSQL (after)
    RTRIM((string_agg((g.gebiet)::text, ','
          ORDER BY g.gebiet) || ','), ',')
  9. 9Sequences and name collisions 375 sequences; indexes with the name of their table

    Problem: Oracle sequences have maximum values with 28 digits and options such as NOORDER, NOKEEP, NOSCALE. In addition, some indexes had the same name as their table – in PostgreSQL, tables and indexes share a namespace.

    Solution: Sequence options are converted, and maximum values that are too large are dropped (bigint limit). The migration job detects name collisions during the trial run and renames the index automatically (learned error correction).

    Oracle (before)
    CREATE SEQUENCE "VERTRIEB"."SEQ_AKTIVITAET" MINVALUE 1
      MAXVALUE 9999999999999999999999999999 INCREMENT BY 1
      START WITH 41537 CACHE 20 NOORDER NOCYCLE NOKEEP NOSCALE GLOBAL;
    PostgreSQL (after)
    CREATE SEQUENCE vertrieb.seq_aktivitaet MINVALUE 1
      INCREMENT BY 1 START WITH 41537 CACHE 20 NO CYCLE;
  10. 10PL/SQL special cases: autonomous transactions, packages, calendars 7 autonomous transactions, 27 packages, 131 procedures

    Problem: Forecast functions write a log with PRAGMA AUTONOMOUS_TRANSACTION that must be retained even if the transaction is aborted. Packages hold global variables; calendar functions generate days with CONNECT BY LEVEL.

    Solution: Autonomous transactions run via a separate connection (dblink) – the log is retained even after a ROLLBACK (tested). Packages become functions <package>_<name>, global variables become session values; CONNECT BY LEVEL becomes generate_series.

    Oracle (before)
    FUNCTION prognose_projekt (...) RETURN NUMBER IS
      PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
      ... INSERT INTO prognose_protokoll ...;
      COMMIT;
    
    SELECT TRUNC(p_von) + LEVEL - 1 FROM dual
     CONNECT BY LEVEL <= 1 + TRUNC(p_bis) - TRUNC(p_von)
    PostgreSQL (after)
    -- Hülle startet die Funktion in eigener Verbindung:
    SELECT t.r INTO _r FROM dblink(oracompat.autonomous_server(),
      format('SELECT vertrieb.prognose_projekt_at(%L, %L)', ...)) AS t(r numeric);
    
    SELECT TRUNC(p_von) + level - 1
      FROM generate_series(1, (1 + TRUNC(p_bis) - TRUNC(p_von))::int) AS _gen(level)

Result and approach

Structure, data and database code were completely converted in trial runs; data volumes and checksums were automatically compared after each run. Whatever the set of rules cannot convert reliably ends up in an error list with a reason and is resolved by hand in a targeted manner – instead of silently delivering wrong results.

No risk for you: With your database, too, we start with an analysis and trial migration. If the migration then turns out not to be reasonably feasible, or if you are not happy with our quotation, you incur no costs (T&C § 3).