Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Your company is migrating 60 Oracle schemas to PostgreSQL. Applications rely on Oracle sequences, ROWNUM pagination, and SYSDATE in SQL. How do you plan the SQL and id generation side of the migration?

I would start by measuring: inventory all Oracle-specific SQL with static analysis of the code and with the statements Oracle actually runs (V$SQL or AWR), so we know the size of the work per schema. Then sequences move to PostgreSQL sequences with their current values and increments preserved and allocationSize kept aligned; ROWNUM pagination is rewritten to FETCH FIRST, which runs on both engines and so allows dual running; and SYSDATE becomes CURRENT_TIMESTAMP, LOCALTIMESTAMP or now(), with care for time zones and for Oracle DATE carrying a time part. ora2pg converts the schemas, and real workloads with row-by-row comparison prove the result.

Why this question is asked

Moving 60 schemas off Oracle is mostly not a data copying problem. The data moves with tools; what breaks is the SQL the applications send and the assumptions they make about ids and time. Interviewers ask this design question to see whether we can turn a large, vague migration into measured, ordered work with a way to prove it is correct. In this answer, we will size the work, then go through id generation, pagination and dates one at a time, look at the tooling, and finish with how we test and cut over.

1Sizing the Work Before Planning It

With 60 schemas, guessing is not an option. We want, for each schema, a count of statements that use Oracle-only features, so we can plan waves and staff them. Two sources give us this. Static analysis scans the application code, mapper XML files, native queries and stored procedures for patterns such as ROWNUM, SYSDATE, NVL, DECODE, CONNECT BY, (+) outer joins, DUAL and sequence NEXTVAL calls. Runtime data from Oracle shows what really executes, including SQL built dynamically that static analysis misses.

The runtime list matters because code that exists is not the same as code that runs. A schema with 2,000 matches in the code but 40 distinct statements in V$SQL over a month is a much smaller job than it looks.

SQL
-- Distinct statements per schema that use Oracle-specific constructs (run over several weeks)
SELECT parsing_schema_name, COUNT(DISTINCT sql_id) AS statements
FROM v$sql
WHERE REGEXP_LIKE(sql_fulltext, 'ROWNUM|SYSDATE|NVL\(|DECODE\(|CONNECT BY|\(\+\)', 'i')
GROUP BY parsing_schema_name
ORDER BY statements DESC;
-- APP_ORDERS 412
-- APP_BILLING 187

V$SQL only holds what is currently in the shared pool, so we sample it regularly or use AWR's history views (which need the Diagnostics Pack licence). The result is a ranked list of schemas by effort, which drives the order of the waves: one or two small, low-risk schemas first to prove the process, then the rest.

2Moving Sequences and Keeping Ids Safe

Oracle sequences map directly to PostgreSQL sequences. The important thing is that ids continue from where Oracle stopped, with the same increment, so nothing collides and Hibernate's allocationSize still matches. ora2pg creates the sequences, but the current value must be set at cutover, after the last write on Oracle.

SQL
-- On Oracle at cutover (after writes stop)
SELECT sequence_name, last_number, increment_by FROM user_sequences;
-- ORDERS_SEQ 1840051 50
-- On PostgreSQL
CREATE SEQUENCE orders_seq INCREMENT BY 50;
SELECT setval('orders_seq', 1840051 + 50, false); -- next nextval returns 1840101

Notice that LAST_NUMBER on Oracle includes the cached values not yet handed out, so it is already past every id in use, and we add a margin of one increment to be safe. On the Java side the mapping does not change if the increment stays the same. If some tables used Oracle 12c identity columns, they become PostgreSQL identity columns, GENERATED BY DEFAULT AS IDENTITY, with their start value set the same way. Applications that call orders_seq.NEXTVAL in native SQL need rewriting to nextval('orders_seq'), which is one of the patterns in our inventory.

3Rewriting ROWNUM Pagination

ROWNUM does not exist in PostgreSQL. We could rewrite to LIMIT and OFFSET, but there is a better target: the standard FETCH FIRST clause, which Oracle supports from 12c and PostgreSQL supports too. If we rewrite the SQL in Oracle first, the same statement runs on both databases. That lets us change the code months before cutover, run it in production on Oracle, and remove one large risk from the cutover day.

SQL
-- Before (Oracle only)
SELECT * FROM (SELECT * FROM orders ORDER BY created_at DESC, id DESC) WHERE ROWNUM <= 20;
-- After (Oracle 12c+ and PostgreSQL)
SELECT * FROM orders ORDER BY created_at DESC, id DESC FETCH FIRST 20 ROWS ONLY;

Queries generated by JPA need no rewriting at all, because Hibernate's dialect produces the right limit syntax for each database. This is a good moment to add a unique tie breaker to ORDER BY where it is missing, because the two engines may order ties differently and the comparison tests would flag every such page.

4Dates, SYSDATE and Time Zones

Dates are where silent bugs hide. Oracle's DATE type stores a date and a time down to the second. PostgreSQL's date type stores only the day. A naive mapping of DATE to date drops the time from every value, and nothing fails; the data is just wrong. So Oracle DATE columns become timestamp(0), or timestamptz when the values represent moments in time.

SYSDATE returns the current date and time of the database server's operating system, with no time zone attached. PostgreSQL has CURRENT_TIMESTAMP and now(), which return the transaction start time as timestamptz, LOCALTIMESTAMP, which returns it without a time zone in the session's zone, and clock_timestamp(), which changes during a statement. We must decide per use which one matches, and set the server and session time zone explicitly, because Oracle servers often ran in local time and SYSDATE quietly depended on it.

SQL
-- Oracle
SELECT * FROM orders WHERE created_at > SYSDATE - 1; -- last 24 hours, server local time
-- PostgreSQL
SELECT * FROM orders WHERE created_at > LOCALTIMESTAMP - INTERVAL '1 day'; -- if created_at is timestamp
SELECT * FROM orders WHERE created_at > now() - INTERVAL '1 day'; -- if created_at is timestamptz

5Tooling and Proving Correctness

ora2pg reads an Oracle schema and produces PostgreSQL DDL, data export scripts and a migration effort report, and it can convert much of the simpler PL/SQL. It is a starting point, not the final answer. Some teams also use the orafce extension, which provides Oracle-compatible functions such as NVL and SYSDATE-like helpers, to reduce rewrites; I prefer to use it only as a bridge, because it keeps Oracle habits alive.

Correctness is proven by running the same workload against both databases. We replay captured production reads against Oracle and PostgreSQL and compare results row by row, with ordering normalised, and we compare checksums of each table after the data load. Other differences our tests should cover include Oracle treating an empty string as NULL, upper-case unquoted identifiers in Oracle versus lower-case in PostgreSQL, and NUMBER columns that need explicit precision in PostgreSQL.

Step throughOne migration wave
  1. Inventory SQLstatic scan plus V$SQL or AWR
  2. Rewrite to portable SQL on OracleFETCH FIRST, explicit date functions
  3. Convert schema with ora2pgreview types: DATE -> timestamp(0)
  4. Load data, set sequencessetval above Oracle LAST_NUMBER
  5. Replay workload on bothcompare results row by row
  6. All differences explained?
    YesCut over the schemaNoFix and replay again

6 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Why not translate every query automatically?

Automatic translation handles syntax, but not meaning. It can turn SYSDATE into now(), but it cannot know whether that code depended on the server being in local time, or whether an empty string was relied on to mean NULL. We automate what is mechanical and review what carries meaning, using the inventory to know where to look.

How do you handle the cutover for 60 schemas?

In waves, grouped by which applications share data. Within a wave, writes stop on Oracle, the final data delta is loaded, sequences are set above Oracle's LAST_NUMBER, the applications are pointed at PostgreSQL, and a rollback path back to Oracle stays ready for a defined period, for example by capturing changes on PostgreSQL so they can be replayed to Oracle if we must go back.

What about PL/SQL in the database?

That is usually the biggest single item. Simple procedures convert to PL/pgSQL with ora2pg and review. Packages with state, autonomous transactions or heavy use of Oracle built-in packages often have no direct equivalent and are better moved into the application, which is a separate stream of work in the plan.

7Conclusion

In this answer, we planned the SQL and id side of an Oracle to PostgreSQL migration by first measuring the Oracle-specific SQL with static analysis and V$SQL or AWR. Sequences move with their values and increments preserved, set above Oracle's LAST_NUMBER at cutover, with allocationSize unchanged. ROWNUM pagination is rewritten to FETCH FIRST on Oracle first, so the same SQL runs on both engines. SYSDATE maps to now(), CURRENT_TIMESTAMP or LOCALTIMESTAMP depending on meaning and time zone, and Oracle DATE becomes a timestamp rather than date. ora2pg converts schemas, and replaying real workloads with row-by-row comparison proves the result.

What to say in the interviewAbout 80 seconds aloud

Open with

I would begin by sizing the work, because sixty schemas cannot be planned by guesswork. A static scan of the code finds patterns like ROWNUM, SYSDATE, NVL, DECODE and NEXTVAL, and sampling V$SQL or AWR shows what actually runs per schema, which gives a ranked list for planning waves.

Then, if they want more

For ids, Oracle sequences map to PostgreSQL sequences with the same increment, so Hibernate's allocationSize still matches. At cutover I read LAST_NUMBER on Oracle, which already includes cached values, and call setval on PostgreSQL a little above it.

For pagination I rewrite ROWNUM queries to FETCH FIRST while we are still on Oracle, because 12c and PostgreSQL both support it, so the change ships and is proven long before cutover; JPA queries need nothing, since the dialect handles it. Dates need the most care.

Oracle DATE has a time part, so it becomes timestamp, not date, and SYSDATE is server local time, so each use becomes now, CURRENT_TIMESTAMP or LOCALTIMESTAMP depending on meaning, with explicit time zones.

I would use ora2pg for schema conversion and effort estimates, then replay captured workloads against both databases and compare results row by row, plus table checksums after loading, before cutting over each wave.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Your company is migrating 60 Oracle schemas to PostgreSQL. Applications rely on Oracle...": https://scrivio-interview-library.netlify.app/databases/oracle-and-sql-server/1a82393525/