PostgreSQL error code
PostgreSQL error 2201X
2201X is the SQLSTATE PostgreSQL reports for invalid row count in result offset clause, in class 22, Data Exception.
| SQLSTATE | 2201X |
|---|---|
| Condition name | invalid_row_count_in_result_offset_clause |
| Class | 22 — Data Exception |
Reproduce it, then fix it
The offset came out negative, which usually means page arithmetic like (page - 1) * size ran with page 0. Clamp it with greatest(..., 0). Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
SELECT id, name FROM products ORDER BY id LIMIT 5 OFFSET (0 - 1) * 5;
SELECT id, name FROM products ORDER BY id LIMIT 5 OFFSET greatest(0 - 1, 0) * 5;