mirror of
https://github.com/github/awesome-copilot.git
synced 2026-08-17 14:46:56 +00:00
2.1 KiB
2.1 KiB
Oracle to PostgreSQL: ROWNUM Pagination vs LIMIT/OFFSET
Problem
Oracle uses ROWNUM pseudo-column for pagination and row-limiting. PostgreSQL uses standard LIMIT / OFFSET syntax. ROWNUM is also fundamentally different in when it is assigned, which affects filtering behavior.
Behavior Comparison
Oracle:
ROWNUMis assigned beforeORDER BY— filtering withROWNUMon an unordered result set is non-deterministic- Common pattern to get "top N ordered rows" requires a subquery:
SELECT * FROM (SELECT * FROM t ORDER BY col) WHERE ROWNUM <= 10 ROWNUM BETWEEN n AND mrequires a double-wrapped subquery
PostgreSQL:
LIMIT nrestricts result rows afterORDER BYis applied — straightforward and deterministicOFFSET nskips rows; combine withLIMITfor pagination- No
ROWNUMpseudo-column exists
Code Example
-- Oracle: top 10 rows by date
SELECT * FROM (
SELECT * FROM orders ORDER BY created_at DESC
) WHERE ROWNUM <= 10;
-- PostgreSQL equivalent
SELECT * FROM orders ORDER BY created_at DESC LIMIT 10;
-- Oracle: rows 11–20 (keyset pagination via ROWNUM)
SELECT * FROM (
SELECT t.*, ROWNUM rn FROM (
SELECT * FROM orders ORDER BY created_at DESC
) t WHERE ROWNUM <= 20
) WHERE rn > 10;
-- PostgreSQL equivalent
SELECT * FROM orders ORDER BY created_at DESC LIMIT 10 OFFSET 10;
Migration Actions
1. Stored Procedures
Replace all ROWNUM-based limiting patterns with LIMIT/OFFSET:
-- Oracle
WHERE ROWNUM = 1
WHERE ROWNUM <= :n
-- PostgreSQL
LIMIT 1
LIMIT :n -- note: use $n parameter style in PL/pgSQL
For subquery wrapping patterns:
-- Oracle
SELECT * FROM (SELECT ... ORDER BY col) WHERE ROWNUM <= :n
-- PostgreSQL
SELECT ... ORDER BY col LIMIT :n
2. Application Code (inline SQL strings)
Search for ROWNUM in C# string literals, StringBuilder, and query-builder methods. Apply the same replacement patterns above.
3. Tests
Ensure integration tests validate that result set sizes are correct and that ordering is preserved (i.e., the correct n rows are returned, not just any n rows).